001/** 002 * Copyright (c) 2012, 2015, Credit Suisse (Anatole Tresch), Werner Keil and others by the @author tag. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 005 * use this file except in compliance with the License. You may obtain a copy of 006 * the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 013 * License for the specific language governing permissions and limitations under 014 * the License. 015 */ 016package org.javamoney.moneta; 017 018import org.javamoney.moneta.function.DefaultMonetaryRoundedFactory; 019import org.javamoney.moneta.function.PrecisionContextRoundedOperator; 020import org.javamoney.moneta.function.PrecisionScaleRoundedOperator; 021import org.javamoney.moneta.function.ScaleRoundedOperator; 022 023import java.math.MathContext; 024import java.math.RoundingMode; 025 026/** 027 * Builder to {@link MonetaryRoundedFactory} once the {@link RoundingMode}, is possible 028 * choose the <b>scale</b>, the number of digits to the right of the decimal point, and the <b>precision</b>, the total number of digits in a number or both. 029 * @author Otavio Santana 030 * @see {@link org.javamoney.moneta.function.MonetaryRoundedFactoryBuilder#withScale(int)} 031 * @see {@link org.javamoney.moneta.function.MonetaryRoundedFactoryBuilder#withPrecision(int)} 032 * @since 1.0.1 033 * @deprecated Moved to function package. 034 */ 035public final class MonetaryRoundedFactoryBuilder { 036 037 private final RoundingMode roundingMode; 038 039 MonetaryRoundedFactoryBuilder(RoundingMode roundingMode) { 040 this.roundingMode = roundingMode; 041 } 042 043 /** 044 * Set the number of digits to the right of the decimal point 045 * @param scale 046 * @return {@link MonetaryRoundedFactoryWithScaleBuilder} 047 */ 048 public MonetaryRoundedFactoryWithScaleBuilder withScale(int scale) { 049 return new MonetaryRoundedFactoryWithScaleBuilder(roundingMode, scale); 050 } 051 052 /** 053 * Set the total number of digits in a number 054 * @param precision 055 * @return @{@link MonetaryRoundedFactoryWithPrecisionBuilder} 056 */ 057 public MonetaryRoundedFactoryWithPrecisionBuilder withPrecision(int precision) { 058 return new MonetaryRoundedFactoryWithPrecisionBuilder(roundingMode, precision); 059 } 060 061 /** 062 * Once the {@link RoundingMode} and scale informed, is possible create a {@link MonetaryRoundedFactory} 063 * or set the number of precision. 064 * @author Otavio Santana 065 *@see {@link MonetaryRoundedFactoryWithScaleBuilder#withPrecision(int)} 066 *@see {@link MonetaryRoundedFactoryWithScaleBuilder#build()} 067 */ 068 public static class MonetaryRoundedFactoryWithScaleBuilder { 069 070 private final RoundingMode roundingMode; 071 072 private final int scale; 073 074 private MonetaryRoundedFactoryWithScaleBuilder(RoundingMode roundingMode, int scale) { 075 this.roundingMode = roundingMode; 076 this.scale = scale; 077 } 078 079 /** 080 * Make the {@link MonetaryRoundedFactory} using the {@link ScaleRoundedOperator} as rounding operator. 081 * @return {@link MonetaryRoundedFactory} with {@link ScaleRoundedOperator} 082 * @see {@link ScaleRoundedOperator} 083 * @see {@link MonetaryRoundedFactory} 084 */ 085 public MonetaryRoundedFactory build() { 086 return new DefaultMonetaryRoundedFactory(ScaleRoundedOperator.of(scale, roundingMode)); 087 } 088 089 /** 090 * Set the total number of digits in a number 091 * @param precision 092 * @return {@link MonetaryRoundedFactoryWithPrecisionBuilder} 093 */ 094 public MonetaryRoundedFactoryWithPrecisionScaleBuilder withPrecision(int precision) { 095 MonetaryRoundedFactoryWithPrecisionScaleBuilder builder = new MonetaryRoundedFactoryWithPrecisionScaleBuilder(roundingMode); 096 builder.scale = this.scale; 097 builder.precision = precision; 098 return builder; 099 } 100 101 } 102 103 /** 104 * Once the {@link RoundingMode} and precision informed, is possible create a {@link MonetaryRoundedFactory} 105 * or set the number of scale. 106 * @author Otavio Santana 107 *@see {@link MonetaryRoundedFactoryWithPrecisionBuilder#withScale(int)} 108 *@see {@link MonetaryRoundedFactoryWithPrecisionBuilder#build()} 109 */ 110 public static class MonetaryRoundedFactoryWithPrecisionBuilder { 111 112 private final int precision; 113 114 private final RoundingMode roundingMode; 115 116 private MonetaryRoundedFactoryWithPrecisionBuilder(RoundingMode roundingMode, int precision) { 117 this.roundingMode = roundingMode; 118 this.precision = precision; 119 } 120 /** 121 * Set the number of digits to the right of the decimal point 122 * @param scale 123 * @return {@link MonetaryRoundedFactoryWithPrecisionScaleBuilder} 124 */ 125 public MonetaryRoundedFactoryWithPrecisionScaleBuilder withScale(int scale) { 126 MonetaryRoundedFactoryWithPrecisionScaleBuilder builder = new MonetaryRoundedFactoryWithPrecisionScaleBuilder(roundingMode); 127 builder.precision = this.precision; 128 builder.scale = scale; 129 return builder; 130 } 131 132 /** 133 * Make the {@link MonetaryRoundedFactory} using the {@link PrecisionContextRoundedOperator} as rounding operator. 134 * @return {@link MonetaryRoundedFactory} with {@link PrecisionContextRoundedOperator} 135 * @see {@link PrecisionContextRoundedOperator} 136 * @see {@link MonetaryRoundedFactory} 137 */ 138 public MonetaryRoundedFactory build() { 139 MathContext mathContext = new MathContext(precision, roundingMode); 140 return new DefaultMonetaryRoundedFactory(PrecisionContextRoundedOperator.of(mathContext)); 141 } 142 143 } 144 145 /** 146 * Once the {@link RoundingMode}, precision and scale informed, the next step will build a {@link MonetaryRoundedFactory} 147 * with all these information. 148 * @author Otavio Santana 149 */ 150 public static class MonetaryRoundedFactoryWithPrecisionScaleBuilder { 151 152 private int scale; 153 154 private int precision; 155 156 private final RoundingMode roundingMode; 157 158 public MonetaryRoundedFactoryWithPrecisionScaleBuilder( 159 RoundingMode roundingMode) { 160 this.roundingMode = roundingMode; 161 } 162 163 /** 164 * Make the {@link MonetaryRoundedFactory} using the {@link PrecisionScaleRoundedOperator} as rounding operator. 165 * @return {@link MonetaryRoundedFactory} with {@link PrecisionScaleRoundedOperator} 166 * @see {@link PrecisionContextRoundedOperator} 167 * @see {@link PrecisionScaleRoundedOperator} 168 */ 169 public MonetaryRoundedFactory build() { 170 MathContext mathContext = new MathContext(precision, roundingMode); 171 return new DefaultMonetaryRoundedFactory(PrecisionScaleRoundedOperator.of(scale, mathContext)); 172 } 173 174 } 175 176 @Override 177 public String toString() { 178 StringBuilder sb = new StringBuilder(); 179 sb.append(MonetaryRoundedFactoryBuilder.class.getName()).append('{') 180 .append("roundingMode: ").append(roundingMode).append('}'); 181 return sb.toString(); 182 } 183 184}