author | Jaroslav Tulach <jtulach@netbeans.org> |
Sat, 25 Oct 2008 20:53:00 +0200 | |
changeset 84 | 2ae6e4aa7aef |
parent 61 | 58ec6da75f6f |
permissions | -rw-r--r-- |
japod@23 | 1 |
|
japod@23 | 2 |
package org.apidesign.apifest08.currency; |
japod@23 | 3 |
|
japod@23 | 4 |
import java.util.Currency; |
japod@23 | 5 |
|
japod@23 | 6 |
/** |
japod@23 | 7 |
* Desription of currency. |
japod@23 | 8 |
* |
japod@23 | 9 |
* Java has similar class {@link java.util.Currency}, but original class is not flexible |
japod@23 | 10 |
* enough, we use our own implementation of currency. |
japod@23 | 11 |
* |
japod@23 | 12 |
* @author arnostvalicek |
japod@23 | 13 |
*/ |
japod@23 | 14 |
public class ConvertorCurrency { |
jaroslav@63 | 15 |
private String currencyCode; |
jaroslav@63 | 16 |
private int fractionDigits; |
japod@23 | 17 |
|
japod@23 | 18 |
|
japod@23 | 19 |
/** |
jaroslav@63 | 20 |
* Static method providing instance of <code>ConvertorCurrency</code> based on currency code. |
jaroslav@63 | 21 |
* <p> |
jaroslav@63 | 22 |
* <code>currencyCode</code> is one of codes supported by Java class {@link java.util.Currency}. |
japod@23 | 23 |
* |
japod@23 | 24 |
* @param currencyCode Code of required currency. |
japod@23 | 25 |
* @return Returns required <code>ConvertorCurrency</code> |
japod@23 | 26 |
*/ |
japod@23 | 27 |
public static ConvertorCurrency getInstance(String currencyCode) { |
japod@23 | 28 |
ConvertorCurrency convertorCurrency = new ConvertorCurrency(); |
jaroslav@63 | 29 |
Currency currency = Currency.getInstance(currencyCode); |
jaroslav@63 | 30 |
convertorCurrency.setCurrencyCode(currency.getCurrencyCode()); |
jaroslav@63 | 31 |
convertorCurrency.setDefaultFractionDigits(currency.getDefaultFractionDigits()); |
jaroslav@63 | 32 |
return convertorCurrency; |
jaroslav@63 | 33 |
} |
jaroslav@63 | 34 |
|
jaroslav@63 | 35 |
/** |
jaroslav@63 | 36 |
* Static method providing instance of <code>ConvertorCurrency</code> base of currency code. |
jaroslav@63 | 37 |
* |
jaroslav@63 | 38 |
* @param currencyCode Code of required currency. |
jaroslav@63 | 39 |
* @param fractionDigits Number of fraction digits for currency. |
jaroslav@63 | 40 |
* @return Returns required <code>ConvertorCurrency</code> |
jaroslav@63 | 41 |
* @since version4 |
jaroslav@63 | 42 |
*/ |
jaroslav@63 | 43 |
public static ConvertorCurrency getInstance(String currencyCode, int fractionDigits) { |
jaroslav@63 | 44 |
ConvertorCurrency convertorCurrency = new ConvertorCurrency(); |
jaroslav@63 | 45 |
convertorCurrency.setCurrencyCode(currencyCode); |
jaroslav@63 | 46 |
convertorCurrency.setDefaultFractionDigits(fractionDigits); |
japod@23 | 47 |
return convertorCurrency; |
japod@23 | 48 |
} |
japod@23 | 49 |
|
japod@23 | 50 |
/** |
japod@23 | 51 |
* Gets the default number of fraction digits used with this currency. For example, the default number of fraction digits for the Euro is 2, while for the Japanese Yen it's 0. |
japod@23 | 52 |
* @return Returns the default number of fraction digits used with this currency. |
japod@23 | 53 |
*/ |
japod@23 | 54 |
public int getDefaultFractionDigits() { |
jaroslav@63 | 55 |
return fractionDigits; |
jaroslav@63 | 56 |
} |
jaroslav@63 | 57 |
|
jaroslav@63 | 58 |
private void setDefaultFractionDigits(int value) { |
jaroslav@63 | 59 |
this.fractionDigits = value; |
jaroslav@63 | 60 |
} |
jaroslav@63 | 61 |
|
jaroslav@63 | 62 |
/** |
jaroslav@63 | 63 |
* Get currency code. |
jaroslav@63 | 64 |
* @return Returns currency code. |
jaroslav@63 | 65 |
*/ |
jaroslav@63 | 66 |
public String getCurrencyCode() { |
jaroslav@63 | 67 |
return currencyCode; |
jaroslav@63 | 68 |
} |
jaroslav@63 | 69 |
|
jaroslav@63 | 70 |
private void setCurrencyCode(String value) { |
jaroslav@63 | 71 |
currencyCode = value; |
japod@23 | 72 |
} |
japod@23 | 73 |
|
japod@23 | 74 |
@Override |
japod@41 | 75 |
public boolean equals(Object obj) { |
japod@41 | 76 |
boolean result; |
japod@41 | 77 |
if (obj instanceof ConvertorCurrency) { |
japod@41 | 78 |
ConvertorCurrency that = (ConvertorCurrency) obj; |
jaroslav@63 | 79 |
result = getCurrencyCode().equals(that.getCurrencyCode()) && getDefaultFractionDigits()==that.getDefaultFractionDigits(); |
japod@41 | 80 |
} else { |
japod@41 | 81 |
result = false; |
japod@41 | 82 |
} |
japod@41 | 83 |
return result; |
japod@41 | 84 |
} |
japod@58 | 85 |
|
japod@58 | 86 |
@Override |
japod@58 | 87 |
public int hashCode() { |
jaroslav@63 | 88 |
return currencyCode==null ? 47/*??*/ : currencyCode.hashCode(); |
japod@58 | 89 |
} |
jaroslav@63 | 90 |
|
japod@41 | 91 |
|
japod@41 | 92 |
@Override |
japod@23 | 93 |
public String toString() { |
jaroslav@63 | 94 |
return "ConvertorCurrency[" + (currencyCode != null ? currencyCode : "NO-BASE-CURRENCY")+"]"; |
japod@41 | 95 |
} |
jaroslav@63 | 96 |
|
jaroslav@63 | 97 |
} |