001: /*
002: *
003: * JMoney - A Personal Finance Manager
004: * Copyright (c) 2002 Johann Gyger <johann.gyger@switzerland.org>
005: * Copyright (c) 2004 Nigel Westbury <westbury@users.sourceforge.net>
006: *
007: *
008: * This program is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License as published by
010: * the Free Software Foundation; either version 2 of the License, or
011: * (at your option) any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: *
022: */
023:
024: package net.sf.jmoney.model2;
025:
026: import java.util.Collection;
027: import java.util.Collections;
028: import java.util.Vector;
029:
030: import net.sf.jmoney.JMoneyPlugin;
031:
032: /**
033: * An implementation of the Account interface
034: */
035: public abstract class Account extends ExtendableObject implements
036: Comparable<Account> {
037:
038: protected String name;
039:
040: protected Account(IObjectKey objectKey, ListKey parentKey,
041: String name, IValues extensionValues) {
042: super (objectKey, parentKey, extensionValues);
043: this .name = name;
044: }
045:
046: protected Account(IObjectKey objectKey, ListKey parentKey) {
047: super (objectKey, parentKey);
048: this .name = null;
049: }
050:
051: /**
052: * @return the name of this account.
053: */
054: public String getName() {
055: return name;
056: }
057:
058: /**
059: * @param aName the name of this account.
060: */
061:
062: public void setName(String newName) {
063: String oldName = name;
064: name = newName;
065:
066: // Notify the change manager.
067: processPropertyChange(AccountInfo.getNameAccessor(), oldName,
068: newName);
069: }
070:
071: public String getFullAccountName() {
072: return getName();
073: }
074:
075: public Account getParent() {
076: ExtendableObject parent = parentKey.getParentKey().getObject();
077: if (parent instanceof Account) {
078: return (Account) parent;
079: } else {
080: return null;
081: }
082: }
083:
084: /**
085: * Returns the commodity that the amount in an entry
086: * represents. If the account for the entry is an account
087: * that can store only one commodity (usually a currency)
088: * then the commodity is a property of the account. If,
089: * however, the account can hold multiple commodities (such
090: * as a stock account) then information from the entry is
091: * required in order to get the commodity involved.
092: *
093: * @return Commodity for the given entry
094: */
095: public abstract Commodity getCommodity(Entry entry);
096:
097: public abstract ObjectCollection<? extends Account> getSubAccountCollection();
098:
099: public Collection<? extends Account> getAllSubAccounts() {
100: Collection<Account> all = new Vector<Account>();
101: for (Account a : getSubAccountCollection()) {
102: all.add(a);
103: all.addAll(a.getAllSubAccounts());
104: }
105: return all;
106: }
107:
108: boolean deleteSubAccount(Account subAccount) {
109: return getSubAccountCollection().remove(subAccount);
110: }
111:
112: /**
113: * Get the entries in the account.
114: *
115: * @return A read-only collection with elements of
116: * type <code>Entry</code>
117: */
118: public Collection<Entry> getEntries() {
119: Collection<Entry> accountEntries = getDataManager().getEntries(
120: this );
121: return Collections.unmodifiableCollection(accountEntries);
122: }
123:
124: /**
125: * @return true if there are any entries in this account,
126: * false if no entries are in this account
127: */
128: public boolean hasEntries() {
129: return getDataManager().hasEntries(this );
130: }
131:
132: /**
133: * This method is used for debugging purposes only.
134: */
135: @Override
136: public String toString() {
137: return getName();
138: }
139:
140: public int compareTo(Account other) {
141: return getName().compareTo(other.getName());
142: }
143:
144: public int getLevel() {
145: int level;
146: if (getParent() == null)
147: level = 0;
148: else
149: level = getParent().getLevel() + 1;
150: if (JMoneyPlugin.DEBUG)
151: System.out.println("Level from " + this .name
152: + ", child of " + getParent() + " is " + level);
153: return level;
154: }
155: }
|