1    /*
2     * Copyright 2009 :torweg free software group
3     *
4     * This program is free software: you can redistribute it and/or modify
5     * it under the terms of the GNU General Public License as published by
6     * the Free Software Foundation, either version 3 of the License, or
7     * (at your option) any later version.
8     * 
9     * This program is distributed in the hope that it will be useful,
10    * but WITHOUT ANY WARRANTY; without even the implied warranty of
11    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    * GNU General Public License for more details.
13    * 
14    * You should have received a copy of the GNU General Public License
15    * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16    *
17    */
18   package org.torweg.pulse.component.statistics.util;
19   
20   import java.util.HashMap;
21   import java.util.HashSet;
22   import java.util.Map;
23   import java.util.Set;
24   
25   import javax.xml.bind.annotation.XmlAccessOrder;
26   import javax.xml.bind.annotation.XmlAccessType;
27   import javax.xml.bind.annotation.XmlAccessorOrder;
28   import javax.xml.bind.annotation.XmlAccessorType;
29   import javax.xml.bind.annotation.XmlElement;
30   import javax.xml.bind.annotation.XmlElementWrapper;
31   import javax.xml.bind.annotation.XmlRootElement;
32   import javax.xml.bind.annotation.XmlTransient;
33   
34   import org.torweg.pulse.component.statistics.model.AbstractRegexVersioned;
35   import org.torweg.pulse.component.statistics.model.aggregation.AbstractRegexVersionedCounterAggregation;
36   
37   /**
38    * The default row data implementation for aggregation of
39    * {@code AbstractRegexVersionedCounterAggregation<? extends AbstractRegexVersioned, ?&<}
40    * .
41    * 
42    * @param <T>
43    *            the
44    *            {@code AbstractRegexVersionedCounterAggregation<? extends AbstractRegexVersioned, ?>}
45    * 
46    * @author Daniel Dietz
47    * @version $Revision: 1562 $
48    */
49   @XmlRootElement(name = "abstract-regex-versioned-counter-aggregation-row-data")
50   @XmlAccessorOrder(XmlAccessOrder.UNDEFINED)
51   @XmlAccessorType(XmlAccessType.FIELD)
52   public final class AbstractRegexVersionedCounterAggregationRowData<T extends AbstractRegexVersionedCounterAggregation<? extends AbstractRegexVersioned, ?>>
53           extends AbstractRowData<T> {
54   
55       /**
56        * The serialVersionUID.
57        */
58       private static final long serialVersionUID = -8472054163624797954L;
59   
60       /**
61        * The counter.
62        */
63       @XmlElement(name = "counter")
64       private Integer counter;
65   
66       /**
67        * The counted totals.
68        */
69       @XmlElementWrapper(name = "counted-totals")
70       private final Map<String, CountedTotal<T>> countedTotals = new HashMap<String, CountedTotal<T>>();
71   
72       /**
73        * The underlying data.
74        */
75       @XmlTransient
76       private final Set<T> dataObjects = new HashSet<T>();
77   
78       /**
79        * Default constructor.
80        */
81       @Deprecated
82       protected AbstractRegexVersionedCounterAggregationRowData() {
83           super();
84       }
85   
86       /**
87        * Creates a new
88        * {@code AbstractRegexVersionedCounterAggregationRowData} with the
89        * data of the given
90        * {@code AbstractRegexVersionedCounterAggregation&lt;? extends AbstractRegexVersioned, ?&gt;}
91        * .
92        * 
93        * @param dataObject
94        *            {@code AbstractRegexVersionedCounterAggregation&lt;? extends AbstractRegexVersioned, ?&gt;}
95        */
96       public AbstractRegexVersionedCounterAggregationRowData(final T dataObject) {
97           super();
98           aggregate(dataObject);
99       }
100  
101      /**
102       * Returns the underlying dataObjects.
103       * 
104       * @return the dataObjects
105       * 
106       * @see org.torweg.pulse.component.statistics.util.AbstractRowData#getDataObjects
107       *      ()
108       */
109      @Override
110      public Set<T> getDataObjects() {
111          return this.dataObjects;
112      }
113  
114      /**
115       * Factory method.
116       * <p>
117       * Creates a new
118       * {@code AbstractRegexVersionedCounterAggregationRowData} from the
119       * given data object.
120       * </p>
121       * 
122       * @param dataObject
123       *            {@code AbstractRegexVersionedCounterAggregation<? extends AbstractRegexVersioned, ?>}
124       * 
125       * @return a new
126       *         {@code AbstractRegexVersionedCounterAggregationRowData}
127       * 
128       */
129      @Override
130      protected AbstractRegexVersionedCounterAggregationRowData<T> create(
131              final T dataObject) {
132          return new AbstractRegexVersionedCounterAggregationRowData<T>(
133                  dataObject);
134      }
135  
136      /**
137       * Aggregates the data from the given data object.
138       * 
139       * @param dataObject
140       *            {@code AbstractRegexVersionedCounterAggregation<? extends AbstractRegexVersioned, ?>}
141       * 
142       * @see org.torweg.pulse.component.statistics.util.AbstractRowData#aggregate(org.torweg.pulse.util.time.IHasDuration)
143       */
144      @Override
145      protected void aggregate(final T dataObject) {
146  
147          if (this.counter == null) {
148              this.counter = dataObject.getCount();
149          } else {
150              this.counter += dataObject.getCount();
151          }
152  
153          if (this.countedTotals.containsKey(dataObject.getCounted()
154                  .getCategory())) {
155              this.countedTotals.get(dataObject.getCounted().getCategory())
156                      .aggregate(dataObject);
157          } else {
158              this.countedTotals.put(dataObject.getCounted().getCategory(),
159                      new CountedTotal<T>(dataObject));
160          }
161  
162          this.dataObjects.add(dataObject);
163  
164      }
165  
166  }
167