View Javadoc

1   package org.neuclear.ledger.simple;
2   
3   import org.neuclear.ledger.Book;
4   import org.neuclear.ledger.PostedTransaction;
5   
6   import java.util.*;
7   
8   /*
9    *  The NeuClear Project and it's libraries are
10   *  (c) 2002-2004 Antilles Software Ventures SA
11   *  For more information see: http://neuclear.org
12   *
13   *  This library is free software; you can redistribute it and/or
14   *  modify it under the terms of the GNU Lesser General Public
15   *  License as published by the Free Software Foundation; either
16   *  version 2.1 of the License, or (at your option) any later version.
17   *
18   *  This library is distributed in the hope that it will be useful,
19   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
20   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21   *  Lesser General Public License for more details.
22   *
23   *  You should have received a copy of the GNU Lesser General Public
24   *  License along with this library; if not, write to the Free Software
25   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26   */
27  
28  /***
29   * User: pelleb
30   * Date: Apr 19, 2004
31   * Time: 10:59:44 AM
32   */
33  public class SimpleBook extends Book {
34  
35      public SimpleBook(SimpleBook orig, String nickname, String type, Date updated, String source, String registrationid) {
36          super(orig.getId(), nickname, type, source, orig.getRegistered(), updated, registrationid);
37          transactions = orig.transactions;
38          balances = orig.balances;
39      }
40  
41      public SimpleBook(String id, String nickname, String type, String source, Date registered, Date updated, String registrationid) {
42          super(id, nickname, type, source, registered, updated, registrationid);
43          transactions = new LinkedList();
44          balances = new HashMap();
45      }
46  
47      public SimpleBook(String id, Date registered) {
48          super(id, registered);
49          transactions = new LinkedList();
50          balances = new HashMap();
51      }
52  
53      void add(PostedTransaction tran) {
54          transactions.add(tran);
55      }
56  
57      public synchronized void updateBalance(final String ledger, final double amount) {
58          Double balance = (Double) balances.get(ledger);
59          if (balance == null)
60              balances.put(ledger, new Double(amount));
61          else
62              balances.put(ledger, new Double(amount + balance.doubleValue()));
63      }
64  
65      public double getBalance(final String ledger) {
66          if (balances.containsKey(ledger))
67              return ((Double) balances.get(ledger)).doubleValue();
68          return 0.0;
69      }
70  
71      public Iterator iterator() {
72          return transactions.iterator();
73      }
74  
75      public Iterator ledgerIterator() {
76          return balances.keySet().iterator();
77      }
78  
79      private final List transactions;
80      private final Map balances;
81  //    private double held=0;
82  }