View Javadoc
1   package fr.ifremer.tutti.service.export;
2   
3   /*
4    * #%L
5    * Tutti :: Service
6    * %%
7    * Copyright (C) 2012 - 2014 Ifremer
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as
11   * published by the Free Software Foundation, either version 3 of the 
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public 
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/gpl-3.0.html>.
22   * #L%
23   */
24  
25  import fr.ifremer.tutti.persistence.entities.data.SpeciesBatch;
26  import org.apache.commons.lang3.mutable.MutableInt;
27  
28  import java.util.Map;
29  import java.util.TreeMap;
30  
31  /**
32   * To store a species or batch entry within his species informations,
33   * his sorted weight, total weight and optional his total number.
34   *
35   * @since 3.0-rc-1
36   */
37  public class ExportBatchEntry {
38  
39      protected final SpeciesBatch batch;
40  
41      protected float sortedWeight;
42  
43      protected float totalWeight;
44  
45      protected int totalNumber;
46  
47      /**
48       * Frequencies (key = step, value = number).
49       *
50       * @since 3.3
51       */
52      protected Map<Float, MutableInt> frequencies;
53  
54      public ExportBatchEntry(SpeciesBatch batch) {
55          this.batch = batch;
56          this.frequencies = new TreeMap<>();
57      }
58  
59      public SpeciesBatch getBatch() {
60          return batch;
61      }
62  
63      public void addSortedWeight(float weight) {
64          sortedWeight += weight;
65      }
66  
67      public void addTotalWeight(float weight) {
68          totalWeight += weight;
69      }
70  
71      public void addNumber(int number) {
72          totalNumber += number;
73      }
74  
75      public void addFrequency(float lengthStep, int number) {
76          MutableInt totalNumber = frequencies.get(lengthStep);
77          if (totalNumber == null) {
78              totalNumber = new MutableInt();
79              frequencies.put(lengthStep, totalNumber);
80          }
81          totalNumber.add(number);
82      }
83  
84      public float getSortedWeight() {
85          return sortedWeight;
86      }
87  
88      public float getTotalWeight() {
89          return totalWeight;
90      }
91  
92      public int getTotalNumber() {
93          return totalNumber;
94      }
95  
96      public Float getAverageFrequency() {
97          int totNumber = 0;
98          float totSize = 0;
99          for (Map.Entry<Float, MutableInt> entry : frequencies.entrySet()) {
100             float size = entry.getKey();
101             int number = entry.getValue().intValue();
102             totSize += (size * number);
103             totNumber += number;
104         }
105         return totNumber == 0 ? null : totSize / (float) totNumber;
106     }
107 }