001/** 002 * Copyright (c) 2012, 2014, 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.format; 017 018import java.text.DecimalFormatSymbols; 019import java.util.Currency; 020import java.util.Locale; 021import java.util.Objects; 022 023import javax.money.CurrencyUnit; 024import javax.money.Monetary; 025import javax.money.format.MonetaryAmountFormat; 026 027/** 028 * This class represents symbols to be used on {@link MonetaryAmountFormatSymbols}, this class decorate the 029 * {@link DecimalFormatSymbols} 030 * @see {@link DecimalFormatSymbols} 031 * @see {@link MonetaryAmountFormat} 032 * @see {@link MonetaryAmountFormatSymbols} 033 * @author Otavio Santana 034 * @deprecated 035 */ 036@Deprecated 037public final class MonetaryAmountSymbols { 038 039 private final DecimalFormatSymbols formatSymbols; 040 /** 041 * Create a MonetaryAmountFormatSymbols object for the given locale 042 * @see {@link DecimalFormatSymbols#DecimalFormatSymbols(Locale)} 043 * @param locale 044 */ 045 public MonetaryAmountSymbols(Locale locale) { 046 this.formatSymbols = new DecimalFormatSymbols(locale); 047 } 048 /** 049 * Create a MonetaryAmountFormatSymbols object for the default FORMAT locale. 050 * @see {@link Locale.getDefault(Locale.Category.FORMAT)} 051 * {@link DecimalFormatSymbols#DecimalFormatSymbols()} 052 */ 053 public MonetaryAmountSymbols() { 054 this.formatSymbols = new DecimalFormatSymbols(); 055 } 056 /** 057 * 058 * @return 059 */ 060 public CurrencyUnit getCurrency() { 061 return Monetary.getCurrency(formatSymbols.getCurrency().getCurrencyCode()); 062 } 063 064 /** 065 * Sets the currency of these MonetaryAmountFormatSymbols. This also sets the currency symbol attribute to the 066 * currency's symbol in the MonetaryAmountFormatSymbols' locale, and the international currency symbol attribute 067 * to the currency's ISO 4217 currency code. 068 * @param currency 069 * @throws NullPointerException if currency is null 070 */ 071 public void setCurrency(CurrencyUnit currency) { 072 Objects.requireNonNull(currency); 073 formatSymbols.setCurrency(Currency.getInstance(currency.getCurrencyCode())); 074 } 075 /** 076 * @return Returns the currency symbol for the currency of these MonetaryAmountFormatSymbols in their locale. 077 */ 078 public String getCurrencySymbol() { 079 return formatSymbols.getCurrencySymbol(); 080 } 081 /** 082 * Sets the currency symbol for the currency of these MonetaryAmountFormatSymbols in their locale. 083 * @param currencySymbol 084 */ 085 public void setCurrencySymbol(String currencySymbol) { 086 formatSymbols.setCurrencySymbol(currencySymbol); 087 } 088 /** 089 * Gets the character used for decimal sign. 090 * @return 091 */ 092 public char getDecimalSeparator() { 093 return formatSymbols.getDecimalSeparator(); 094 } 095 096 /** 097 * Sets the character used for decimal sign. 098 * @param decimalSeparator 099 */ 100 public void setDecimalSeparator(char decimalSeparator) { 101 formatSymbols.setDecimalSeparator(decimalSeparator); 102 } 103 /** 104 * @return Gets the character used for a digit in a pattern. 105 */ 106 public char getDigit() { 107 return formatSymbols.getDigit(); 108 } 109 /** 110 * Sets the character used for a digit in a pattern. 111 * @param digit 112 */ 113 public void setDigit(char digit) { 114 formatSymbols.setDigit(digit); 115 } 116 /** 117 * @return Returns the string used to separate the mantissa from the exponent. Examples: "x10^" for 1.23x10^4, 118 * "E" for 1.23E4. 119 */ 120 public String getExponentSeparator() { 121 return formatSymbols.getExponentSeparator(); 122 } 123 /** 124 * Sets the string used to separate the mantissa from the exponent. Examples: "x10^" for 1.23x10^4, "E" for 1.23E4. 125 * @param exponentSeparator 126 */ 127 public void setExponentSeparator(String exponentSeparator) { 128 formatSymbols.setExponentSeparator(exponentSeparator); 129 } 130 /** 131 * @return Gets the character used for thousands separator. 132 */ 133 public char getGroupingSeparator() { 134 return formatSymbols.getGroupingSeparator(); 135 } 136 /** 137 * Sets the character used for thousands separator. 138 * @param groupingSeparator 139 */ 140 public void setGroupingSeparator(char groupingSeparator) { 141 formatSymbols.setGroupingSeparator(groupingSeparator); 142 } 143 /** 144 * @return Gets the string used to represent infinity. Almost always left unchanged. 145 */ 146 public String getInfinity() { 147 return formatSymbols.getInfinity(); 148 } 149 /** 150 * Sets the string used to represent infinity. Almost always left unchanged. 151 * @param infinity 152 */ 153 public void setInfinity(String infinity) { 154 formatSymbols.setInfinity(infinity); 155 } 156 /** 157 * @return the ISO 4217 currency code of the currency of these MonetaryAmountFormatSymbols. 158 */ 159 public String getInternationalCurrencySymbol() { 160 return formatSymbols.getInternationalCurrencySymbol(); 161 } 162 /** 163 * Sets the ISO 4217 currency code of the currency of these MonetaryAmountFormatSymbols. 164 * @param internationalCurrencySymbol 165 */ 166 public void setInternationalCurrencySymbol(String internationalCurrencySymbol) { 167 Objects.requireNonNull(internationalCurrencySymbol); 168 Currency.getInstance(internationalCurrencySymbol); 169 formatSymbols.setInternationalCurrencySymbol(internationalCurrencySymbol); 170 } 171 /** 172 * Gets the character used to represent minus sign. If no explicit negative format is specified, one is 173 * formed by prefixing minusSign to the positive format. 174 * @return 175 */ 176 public char getMinusSign() { 177 return formatSymbols.getMinusSign(); 178 } 179 /** 180 * Sets the character used to represent minus sign. If no explicit negative format is specified, one is 181 * formed by prefixing minusSign to the positive format. 182 * @param minusSign 183 */ 184 public void setMinusSign(char minusSign) { 185 formatSymbols.setMinusSign(minusSign); 186 } 187 /** 188 * @return the monetary decimal separator. 189 */ 190 public char getMonetaryDecimalSeparator() { 191 return formatSymbols.getMonetaryDecimalSeparator(); 192 } 193 /** 194 * Sets the monetary decimal separator. 195 * @param monetaryDecimalSeparator 196 */ 197 public void setMonetaryDecimalSeparator(char monetaryDecimalSeparator) { 198 formatSymbols.setMonetaryDecimalSeparator(monetaryDecimalSeparator); 199 } 200 /** 201 * @return the string used to represent "not a number". Almost always left unchanged. 202 */ 203 public String getNaN() { 204 return formatSymbols.getNaN(); 205 } 206 /** 207 * Sets the string used to represent "not a number". Almost always left unchanged. 208 * @param naN 209 */ 210 public void setNaN(String naN) { 211 formatSymbols.setNaN(naN); 212 } 213 /** 214 * @return the character used to separate positive and negative subpatterns in a pattern. 215 */ 216 public char getPatternSeparator() { 217 return formatSymbols.getPatternSeparator(); 218 } 219 /** 220 * Sets the character used to separate positive and negative subpatterns in a pattern. 221 * @param patternSeparator 222 */ 223 public void setPatternSeparator(char patternSeparator) { 224 formatSymbols.setPatternSeparator(patternSeparator); 225 } 226 /** 227 * @return the character used for percent sign. 228 */ 229 public char getPercent() { 230 return formatSymbols.getPercent(); 231 } 232 /** 233 * Sets the character used for percent sign. 234 * @param percent 235 */ 236 public void setPercent(char percent) { 237 formatSymbols.setPercent(percent); 238 } 239 /** 240 * @return the character used for per mille sign. 241 */ 242 public char getPerMill() { 243 return formatSymbols.getPerMill(); 244 } 245 /** 246 * Sets the character used for per mille sign. 247 * @param perMill 248 */ 249 public void setPerMill(char perMill) { 250 formatSymbols.setPerMill(perMill); 251 } 252 /** 253 * @return Gets the character used for zero. 254 */ 255 public char getZeroDigit() { 256 return formatSymbols.getZeroDigit(); 257 } 258 /** 259 * Sets the character used for zero. 260 * @param zeroDigit 261 */ 262 public void setZeroDigit(char zeroDigit) { 263 formatSymbols.setZeroDigit(zeroDigit); 264 } 265 266 @Override 267 public boolean equals(Object obj) { 268 if(obj == this) { 269 return true; 270 } 271 if(MonetaryAmountSymbols.class.isInstance(obj)) { 272 MonetaryAmountSymbols other = MonetaryAmountSymbols.class.cast(obj); 273 return Objects.equals(other.formatSymbols, formatSymbols); 274 } 275 return false; 276 } 277 @Override 278 public int hashCode() { 279 return Objects.hash(formatSymbols); 280 } 281 282 DecimalFormatSymbols getFormatSymbol() { 283 return formatSymbols; 284 } 285 286 @Override 287 public String toString() { 288 StringBuilder sb = new StringBuilder(); 289 sb.append(this.getClass().getName()).append('{'); 290 sb.append(" Currency: ").append(formatSymbols.getCurrency()).append(','); 291 sb.append(" currencySymbol: ").append(formatSymbols.getCurrencySymbol()).append(','); 292 sb.append(" decimalSeparator: ").append(formatSymbols.getDecimalSeparator()).append(','); 293 sb.append(" digit: ").append(formatSymbols.getDigit()).append(','); 294 sb.append(" exponentSeparator: ").append(formatSymbols.getExponentSeparator()).append(','); 295 sb.append(" groupingSeparator: ").append(formatSymbols.getGroupingSeparator()).append(','); 296 sb.append(" infinity: ").append(formatSymbols.getInfinity()).append(','); 297 sb.append(" internationalCurrencySymbol: ").append(formatSymbols.getInternationalCurrencySymbol()).append(','); 298 sb.append(" minusSign: ").append(formatSymbols.getMinusSign()).append(','); 299 sb.append(" monetaryDecimalSeparator: ").append(formatSymbols.getMonetaryDecimalSeparator()).append(','); 300 sb.append(" naN: ").append(formatSymbols.getNaN()).append(','); 301 sb.append(" patternSeparator: ").append(formatSymbols.getPatternSeparator()).append(','); 302 sb.append(" percent: ").append(formatSymbols.getPercent()).append(','); 303 sb.append(" perMill: ").append(formatSymbols.getPerMill()).append(','); 304 sb.append(" zeroDigit: ").append(formatSymbols.getZeroDigit()).append('}'); 305 return sb.toString(); 306 } 307}