1 package org.neuclear.ledger.prevalent;
2
3 import gnu.trove.TObjectDoubleHashMap;
4
5 import java.io.Serializable;
6 import java.util.HashMap;
7 import java.util.Iterator;
8
9 import org.neuclear.ledger.PostedHeldTransaction;
10 import org.neuclear.ledger.TransactionItem;
11 import org.neuclear.ledger.TransactionExistsException;
12
13 /***
14 * This contains the balances of all the accounts
15 */
16 public final class HoldTable implements Serializable{
17 private final HashMap accounts=new HashMap();
18 private final HashMap transactions=new HashMap();
19
20 double getHeldBalance(final String id){
21 if (!accounts.containsKey(id))
22 return 0;
23 return ((AccountHeld)accounts.get(id)).getBalance();
24
25 }
26
27 public boolean exists(String id){
28 return transactions.containsKey(id);
29 }
30 public PostedHeldTransaction get(String id){
31 return (PostedHeldTransaction) transactions.get(id);
32 }
33 void add(PostedHeldTransaction tran) throws TransactionExistsException {
34 if (transactions.containsKey(tran.getId()))
35 throw new TransactionExistsException(null,tran.getId());
36 Iterator items=tran.getItems();
37 while (items.hasNext()) {
38 TransactionItem item = (TransactionItem) items.next();
39 AccountHeld held=(AccountHeld) accounts.get(item.getBook());
40 if (held==null){
41 held=new AccountHeld(this,item.getBook());
42 accounts.put(item.getBook(),held);
43 }
44 held.add(tran);
45 }
46 transactions.put(tran.getId(),tran);
47 }
48
49 public void expire(PostedHeldTransaction tran) {
50 if (!transactions.containsKey(tran.getId()))
51 return;
52 System.out.println("expire");
53 Iterator items=tran.getItems();
54 while (items.hasNext()) {
55 TransactionItem item = (TransactionItem) items.next();
56 AccountHeld held=(AccountHeld) accounts.get(item.getBook());
57 if (held!=null){
58 held.expire(tran);
59 if (held.getHoldCount()==0)
60 accounts.remove(item.getBook());
61 }
62 }
63 transactions.remove(tran.getId());
64
65 }
66 }
This page was automatically generated by Maven