View Javadoc

1   package com.google.code.jetm.maven.data;
2   
3   import java.util.Collections;
4   import java.util.Map;
5   
6   import etm.core.aggregation.Aggregate;
7   
8   /**
9    * A bean to store summary of aggregate data.
10   * 
11   * @author jrh3k5
12   * 
13   */
14  
15  public class AggregateSummary implements Aggregate, Comparable<AggregateSummary> {
16      private double min = Double.MAX_VALUE;
17      private double max = Double.MIN_VALUE;
18      private double total;
19      private long measurements;
20      private String name;
21  
22      /**
23       * Create a summary.
24       * 
25       * @param name
26       *            The name of the summary.
27       * @throws IllegalArgumentException
28       *             If the given name is {@code null}.
29       */
30      public AggregateSummary(String name) {
31          if (name == null)
32              throw new IllegalArgumentException("Name cannot be null.");
33  
34          this.name = name;
35      }
36  
37      /**
38       * Add an aggregate to the summary.
39       * 
40       * @param aggregate
41       *            The {@link Aggregate} to be added to the summary.
42       */
43      public void add(Aggregate aggregate) {
44          this.min = Math.min(aggregate.getMin(), getMin());
45          this.max = Math.max(aggregate.getMax(), getMax());
46          this.total += aggregate.getTotal();
47          this.measurements += aggregate.getMeasurements();
48      }
49  
50      /**
51       * {@inheritDoc}
52       */
53      public int compareTo(AggregateSummary o) {
54          return getName().compareTo(o.getName());
55      }
56  
57      @Override
58      public boolean equals(Object o) {
59          if (o == this)
60              return true;
61  
62          if (!(o instanceof AggregateSummary))
63              return false;
64  
65          return getName().equals(((AggregateSummary) o).getName());
66      }
67  
68      /**
69       * {@inheritDoc}
70       */
71      public double getAverage() {
72          return getTotal() / getMeasurements();
73      }
74  
75      /**
76       * {@inheritDoc}
77       */
78      @SuppressWarnings("rawtypes")
79      public Map getChilds() {
80          return Collections.emptyMap();
81      }
82  
83      /**
84       * {@inheritDoc}
85       */
86      public double getMax() {
87          return max;
88      }
89  
90      /**
91       * {@inheritDoc}
92       */
93      public long getMeasurements() {
94          return measurements;
95      }
96  
97      /**
98       * {@inheritDoc}
99       */
100     public double getMin() {
101         return min;
102     }
103 
104     /**
105      * {@inheritDoc}
106      */
107     public String getName() {
108         return name;
109     }
110 
111     /**
112      * {@inheritDoc}
113      */
114     public double getTotal() {
115         return total;
116     }
117 
118     /**
119      * {@inheritDoc}
120      */
121     public boolean hasChilds() {
122         return false;
123     }
124 
125     @Override
126     public int hashCode() {
127         return getName().hashCode();
128     }
129 }