1 package fr.ifremer.tutti.service.export;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
33
34
35
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
49
50
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 }