View Javadoc

1   // $Id: UserEntryRow.java,v 1.4 2004/06/24 18:10:59 powerpete Exp $
2   // [JMP, 10.03.2004] Created this file.
3   package com.pnpconsult.zeiterfassung.table;
4   
5   import java.util.AbstractCollection;
6   import java.util.ArrayList;
7   import java.util.Collection;
8   import java.util.Date;
9   import java.util.Iterator;
10  import java.util.SortedMap;
11  import java.util.TreeMap;
12  
13  import org.apache.commons.lang.builder.CompareToBuilder;
14  
15  import com.pnpconsult.zeiterfassung.model.AbstractBaseEntry;
16  import com.pnpconsult.zeiterfassung.util.DateUtils;
17  import com.pnpconsult.zeiterfassung.util.MapUtils;
18  import com.pnpconsult.zeiterfassung.util.NumberUtils;
19  
20  /***
21   * @author <a href="mailto:powerpete@users.sf.net">M. Petersen </a>
22   * @version $Id: UserEntryRow.java,v 1.4 2004/06/24 18:10:59 powerpete Exp $
23   */
24  public class UserEntryRow extends AbstractCollection implements Row
25  {
26      private SortedMap rateRows = new TreeMap();
27      private String description;
28      private Date targetDate;
29      private Collection entries = new ArrayList();
30  
31      public boolean add(Object o)
32      {
33          entries.add(o);
34          AbstractBaseEntry entry = (AbstractBaseEntry) o;
35          description = (String) check(description, entry.getBillDescription());
36          targetDate = (Date) check(targetDate, entry.getTargetDate());
37          MapUtils.putMulti(rateRows, new Key(entry), entry, RateRow.class);
38          return true;
39      }
40  
41      public Iterator iterator()
42      {
43          return rateRows.values().iterator();
44      }
45  
46      public String getDescription()
47      {
48          return description;
49      }
50  
51      public String getTargetDate()
52      {
53          return DateUtils.format(targetDate);
54      }
55  
56      public int size()
57      {
58          return rateRows.size();
59      }
60  
61      private Object check(Object obj1, Object obj2)
62      {
63          if (obj1 != null && !obj1.equals(obj2))
64          {
65              throw new IllegalArgumentException("Row contains different object "
66                  + obj1
67                  + " != "
68                  + obj2);
69          }
70          return obj2;
71      }
72  
73      private class Key implements Comparable
74      {
75          private float rate;
76  
77          public Key(AbstractBaseEntry entry)
78          {
79              rate = entry.getRate();
80          }
81  
82          public int compareTo(Object o)
83          {
84              Key k = (Key) o;
85              return new CompareToBuilder().append(rate, k.rate).toComparison();
86          }
87      }
88  
89      public String getTotal()
90      {
91          return NumberUtils.formatLong(computeTotal());
92      }
93  
94      public float computeTotal()
95      {
96          float total = 0.0F;
97          for (Iterator it = rateRows.values().iterator(); it.hasNext();)
98          {
99              RateRow rateRow = (RateRow) it.next();
100             total += rateRow.computeTotal();
101         }
102         return total;
103     }
104 
105     public Collection getEntries()
106     {
107         return entries;
108     }
109 
110     public RateRow getRateRowsHead()
111     {
112         return (RateRow) rateRows.values().iterator().next();
113     }
114 
115     public Iterator getRateRowsTail()
116     {
117         Iterator it = rateRows.values().iterator();
118         it.next();
119         return it;
120     }
121 
122     public int getRateRowsSize()
123     {
124         return rateRows.size();
125     }
126 }