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.stocks;
024:
025: import java.text.NumberFormat;
026: import java.text.ParseException;
027:
028: import net.sf.jmoney.model2.Commodity;
029: import net.sf.jmoney.model2.Currency;
030: import net.sf.jmoney.model2.IObjectKey;
031: import net.sf.jmoney.model2.IValues;
032: import net.sf.jmoney.model2.ListKey;
033:
034: public class Bond extends Commodity {
035:
036: private static final int MAX_DECIMALS = 4;
037: private static final short[] SCALE_FACTOR = { 1, 10, 100, 1000,
038: 10000 };
039: private static NumberFormat[] numberFormat = null;
040:
041: /**
042: * Guaranteed non-null because the session default currency is
043: * set by default.
044: */
045: protected IObjectKey currencyKey;
046:
047: private long redemptionValue;
048: private int interestRate;
049:
050: private static void initNumberFormat() {
051: numberFormat = new NumberFormat[MAX_DECIMALS + 1];
052: for (int i = 0; i < numberFormat.length; i++) {
053: numberFormat[i] = NumberFormat.getNumberInstance();
054: numberFormat[i].setMaximumFractionDigits(i);
055: numberFormat[i].setMinimumFractionDigits(i);
056: }
057: }
058:
059: /**
060: * Constructor used by datastore plug-ins to create
061: * a stock object.
062: */
063: public Bond(IObjectKey objectKey, ListKey parentKey, String name,
064: IObjectKey currencyKey, long redemptionValue,
065: int interestRate, IValues extensionValues) {
066: super (objectKey, parentKey, name, extensionValues);
067:
068: /*
069: * The currency for this account is not allowed to be null, because
070: * users of this class may assume it to be non-null and would not know
071: * how to handle this account if it were null.
072: *
073: * If null is passed, set to the default currency for the session.
074: * This is guaranteed to be never null.
075: */
076: if (currencyKey != null) {
077: this .currencyKey = currencyKey;
078: } else {
079: this .currencyKey = objectKey.getSession()
080: .getDefaultCurrency().getObjectKey();
081: }
082:
083: this .redemptionValue = redemptionValue;
084: this .interestRate = interestRate;
085: }
086:
087: /**
088: * Constructor used by datastore plug-ins to create
089: * a stock object.
090: */
091: public Bond(IObjectKey objectKey, ListKey parentKey) {
092: super (objectKey, parentKey);
093:
094: // Set the currency to the session default currency.
095: this .currencyKey = objectKey.getSession().getDefaultCurrency()
096: .getObjectKey();
097:
098: this .redemptionValue = 0;
099: this .interestRate = 0;
100: }
101:
102: @Override
103: protected String getExtendablePropertySetId() {
104: return "net.sf.jmoney.stocks.bond";
105: }
106:
107: public Currency getCurrency() {
108: return (Currency) currencyKey.getObject();
109: }
110:
111: public void setCurrency(Currency aCurrency) {
112: if (aCurrency == null)
113: throw new IllegalArgumentException();
114: Currency oldCurrency = getCurrency();
115: currencyKey = aCurrency.getObjectKey();
116:
117: // Notify the change manager.
118: processPropertyChange(BondInfo.getCurrencyAccessor(),
119: oldCurrency, aCurrency);
120: }
121:
122: /**
123: * @return the redemption value.
124: */
125: public long getRedemptionValue() {
126: return redemptionValue;
127: }
128:
129: public void setRedemptionValue(long redemptionValue) {
130: long oldRedemptionValue = this .redemptionValue;
131: this .redemptionValue = redemptionValue;
132:
133: // Notify the change manager.
134: processPropertyChange(BondInfo.getRedemptionValueAccessor(),
135: new Long(oldRedemptionValue), new Long(redemptionValue));
136: }
137:
138: /**
139: * @return the redemption value.
140: */
141: public int getInterestRate() {
142: return interestRate;
143: }
144:
145: public void setInterestRate(int interestRate) {
146: int oldInterestRate = this .interestRate;
147: this .interestRate = interestRate;
148:
149: // Notify the change manager.
150: processPropertyChange(BondInfo.getInterestRateAccessor(),
151: new Integer(oldInterestRate), new Integer(interestRate));
152: }
153:
154: /**
155: * @return a number format instance for this currency.
156: */
157:
158: private NumberFormat getNumberFormat() {
159: if (numberFormat == null)
160: initNumberFormat();
161: return numberFormat[2];
162: }
163:
164: @Override
165: public long parse(String amountString) {
166: Number amount;
167: try {
168: amount = getNumberFormat().parse(amountString);
169: } catch (ParseException pex) {
170: amount = new Double(0);
171: }
172: return Math.round(amount.doubleValue() * getScaleFactor());
173: }
174:
175: @Override
176: public String format(long amount) {
177: double a = ((double) amount) / getScaleFactor();
178: return getNumberFormat().format(a);
179: }
180:
181: /**
182: * @return the scale factor for this currency (10 to the number of decimals)
183: */
184: @Override
185: public short getScaleFactor() {
186: return SCALE_FACTOR[2];
187: }
188: }
|