| 1 | package net.sourceforge.finmodel.account; |
| 2 | |
| 3 | import java.io.Serializable; |
| 4 | import java.math.BigDecimal; |
| 5 | import java.util.Calendar; |
| 6 | import java.util.HashSet; |
| 7 | import java.util.Set; |
| 8 | import java.util.SortedMap; |
| 9 | import java.util.TreeMap; |
| 10 | import java.util.logging.Level; |
| 11 | import java.util.logging.Logger; |
| 12 | |
| 13 | import net.sourceforge.finmodel.asset.AssetImpl; |
| 14 | import net.sourceforge.finmodel.rules.Rule; |
| 15 | |
| 16 | import org.jfin.date.Period; |
| 17 | import org.jfin.date.ScheduleException; |
| 18 | |
| 19 | public class AccountImpl implements Account, Serializable { |
| 20 | private static Logger log = Logger.getLogger("net.sourceforge.finmodel.account.AccountImpl"); |
| 21 | // private static DateFormat dateFormat = new SimpleDateFormat( "MM/dd/yyyy" ); |
| 22 | |
| 23 | private String name = ""; |
| 24 | private AssetImpl asset = null; |
| 25 | private Set<Rule> sourceRules = new HashSet<Rule>(); |
| 26 | private Set<Rule> destRules = new HashSet<Rule>(); |
| 27 | |
| 28 | // transient private MathContext dollarCtx = new MathContext(2, RoundingMode.HALF_DOWN); |
| 29 | private long id = 0L; |
| 30 | static final long serialVersionUID = 1L; |
| 31 | |
| 32 | public AccountImpl() { |
| 33 | } |
| 34 | |
| 35 | public AccountImpl( String name, AssetImpl asset ) { |
| 36 | this.name = name; |
| 37 | this.asset = asset; |
| 38 | } |
| 39 | |
| 40 | public long getId() { |
| 41 | return id; |
| 42 | } |
| 43 | |
| 44 | public void setId( long id ) { |
| 45 | this.id = id; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * @TODO: Implement this properly. |
| 50 | */ |
| 51 | public BigDecimal getAverageDailyBalance(Period period) { |
| 52 | return getBalance(period.getEndCalendar()); |
| 53 | } |
| 54 | |
| 55 | public BigDecimal getBalance(Calendar calendar) { |
| 56 | BigDecimal shares = getShares(calendar); |
| 57 | BigDecimal cost = getAsset().getEstimatedValue( calendar ); |
| 58 | BigDecimal rc = shares.multiply(cost); |
| 59 | |
| 60 | return rc; |
| 61 | } |
| 62 | |
| 63 | private SortedMap<Calendar, BigDecimal> getTransfers(Calendar when) |
| 64 | { |
| 65 | SortedMap<Calendar, BigDecimal> transfers = new TreeMap<Calendar, BigDecimal>(); |
| 66 | for (Rule source: getSourceRules()) { |
| 67 | try { |
| 68 | for (Calendar cal: source.getSchedule()) { |
| 69 | System.err.println("Comparing " + cal + " to " + when); |
| 70 | if (cal.before(when)) { |
| 71 | transfers.put( cal, source.createTransaction(cal).getAmount().negate()); |
| 72 | } |
| 73 | } |
| 74 | } catch (ScheduleException sched) { |
| 75 | log.log(Level.WARNING, "ScheduleException for rule: ", sched); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | for (Rule dest: getDestinationRules()) { |
| 80 | try { |
| 81 | for (Calendar cal: dest.getSchedule()) { |
| 82 | if (cal.before(when)) { |
| 83 | transfers.put( cal, dest.createTransaction(cal).getAmount()); |
| 84 | } |
| 85 | } |
| 86 | } catch (ScheduleException sched) { |
| 87 | log.log(Level.WARNING, "ScheduleException for rule: ", sched); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | return transfers; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * @TODO getShares should start at zero, and apply transactions until the |
| 96 | * calendar date. Although, now that I think about it, we may have trouble |
| 97 | * with some of the percentage rules as their values change depending on what |
| 98 | * the account says it has. |
| 99 | */ |
| 100 | public BigDecimal getShares( Calendar calendar ) { |
| 101 | SortedMap<Calendar, BigDecimal> transfers = getTransfers( calendar ); |
| 102 | BigDecimal shares = BigDecimal.ZERO; |
| 103 | |
| 104 | for (Calendar date: transfers.keySet()) { |
| 105 | shares = shares.add(transfers.get(date).divide(getAsset().getEstimatedValue(date))); |
| 106 | } |
| 107 | |
| 108 | return shares; |
| 109 | } |
| 110 | |
| 111 | public void setName(String name) { |
| 112 | this.name = name; |
| 113 | } |
| 114 | |
| 115 | public String getName() { |
| 116 | return name; |
| 117 | } |
| 118 | |
| 119 | public void setAsset(AssetImpl asset) { |
| 120 | this.asset = asset; |
| 121 | } |
| 122 | |
| 123 | public AssetImpl getAsset() { |
| 124 | return this.asset; |
| 125 | } |
| 126 | |
| 127 | public Set<Rule> getSourceRules() { |
| 128 | return sourceRules; |
| 129 | } |
| 130 | |
| 131 | public void addSourceRule( Rule rule ) { |
| 132 | sourceRules.add( rule ); |
| 133 | } |
| 134 | |
| 135 | public void removeSourceRule( Rule rule ) { |
| 136 | sourceRules.remove(rule); |
| 137 | } |
| 138 | |
| 139 | public Set<Rule> getDestinationRules() { |
| 140 | return destRules; |
| 141 | } |
| 142 | |
| 143 | public void addDestinationRule( Rule rule ) { |
| 144 | destRules.add( rule ); |
| 145 | } |
| 146 | |
| 147 | public void removeDestinationRule( Rule rule ) { |
| 148 | destRules.remove( rule ); |
| 149 | } |
| 150 | } |