001: /*
002: *
003: * JMoney - A Personal Finance Manager
004: * Copyright (c) 2002 Johann Gyger <johann.gyger@switzerland.org>
005: *
006: *
007: * This program is free software; you can redistribute it and/or modify
008: * it under the terms of the GNU General Public License as published by
009: * the Free Software Foundation; either version 2 of the License, or
010: * (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
020: *
021: */
022:
023: package net.sf.jmoney.model2;
024:
025: import java.text.NumberFormat;
026: import java.text.ParseException;
027:
028: /**
029: * This class was created because the currency support wich comes with the Java
030: * SDK is to complicated. Therefore we provide a simpler model which is
031: * not based upon locales but upon the ISO 4217 currencies.
032: */
033: public class Currency extends Commodity {
034:
035: private static final int MAX_DECIMALS = 4;
036: private static final short[] SCALE_FACTOR = { 1, 10, 100, 1000,
037: 10000 };
038: private static NumberFormat[] numberFormat = null;
039:
040: private String code; // ISO 4217 Code
041: private int decimals;
042:
043: private static void initNumberFormat() {
044: numberFormat = new NumberFormat[MAX_DECIMALS + 1];
045: for (int i = 0; i < numberFormat.length; i++) {
046: numberFormat[i] = NumberFormat.getNumberInstance();
047: numberFormat[i].setMaximumFractionDigits(i);
048: numberFormat[i].setMinimumFractionDigits(i);
049: }
050: }
051:
052: /**
053: * Constructor used by datastore plug-ins to create
054: * a currency object.
055: */
056: public Currency(IObjectKey objectKey, ListKey parentKey,
057: String name, String code, int decimals,
058: IValues extensionValues) {
059: super (objectKey, parentKey, name, extensionValues);
060:
061: if (decimals < 0 || decimals > MAX_DECIMALS)
062: throw new IllegalArgumentException(
063: "Number of decimals not supported");
064:
065: this .code = code;
066: this .decimals = decimals;
067: }
068:
069: /**
070: * Constructor used by datastore plug-ins to create
071: * a currency object.
072: */
073: public Currency(IObjectKey objectKey, ListKey parentKey) {
074: super (objectKey, parentKey);
075: this .code = null;
076: this .decimals = 2;
077: }
078:
079: @Override
080: protected String getExtendablePropertySetId() {
081: return "net.sf.jmoney.currency";
082: }
083:
084: /**
085: * @return the currency code.
086: */
087: public String getCode() {
088: return code;
089: }
090:
091: public void setCode(String code) {
092: String oldCode = this .code;
093: this .code = code;
094:
095: if (oldCode != null) {
096: getSession().currencies.remove(oldCode);
097: }
098: if (code != null) {
099: getSession().currencies.put(code, this );
100: }
101:
102: // Notify the change manager.
103: processPropertyChange(CurrencyInfo.getCodeAccessor(), oldCode,
104: code);
105: }
106:
107: /**
108: * @return the number of decimals that this currency has.
109: */
110: public int getDecimals() {
111: return decimals;
112: }
113:
114: /**
115: * set the number of decimals that this currency has.
116: */
117: public void setDecimals(int decimals) {
118: int oldDecimals = this .decimals;
119: this .decimals = decimals;
120:
121: // Notify the change manager.
122: processPropertyChange(CurrencyInfo.getDecimalsAccessor(),
123: new Integer(oldDecimals), new Integer(decimals));
124: }
125:
126: @Override
127: public String toString() {
128: return getName() + " (" + getCode() + ")";
129: }
130:
131: /**
132: * @return a number format instance for this currency.
133: */
134:
135: private NumberFormat getNumberFormat() {
136: if (numberFormat == null)
137: initNumberFormat();
138: return numberFormat[getDecimals()];
139: }
140:
141: @Override
142: public long parse(String amountString) {
143: Number amount = new Double(0);
144: try {
145: amount = getNumberFormat().parse(amountString);
146: } catch (ParseException pex) {
147: }
148: return Math.round(amount.doubleValue() * getScaleFactor());
149: }
150:
151: @Override
152: public String format(long amount) {
153: double a = ((double) amount) / getScaleFactor();
154: return getNumberFormat().format(a);
155: }
156:
157: /**
158: * @return the scale factor for this currency (10 to the number of decimals)
159: */
160: @Override
161: public short getScaleFactor() {
162: return SCALE_FACTOR[decimals];
163: }
164: }
|