1 package fr.ifremer.tutti.service.psionimport;
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 com.google.common.collect.Lists;
26 import com.google.common.collect.Maps;
27 import fr.ifremer.tutti.persistence.entities.referential.Species;
28 import org.apache.commons.lang3.builder.ToStringBuilder;
29 import org.apache.commons.lang3.mutable.MutableInt;
30
31 import java.io.Serializable;
32 import java.util.Iterator;
33 import java.util.List;
34 import java.util.Map;
35
36
37
38
39
40
41
42 public class PsionImportBatchModel {
43
44 public static class SampleCategory {
45
46 protected final Integer categoryId;
47
48 protected final Serializable categoryValue;
49
50 public SampleCategory(Integer categoryId, Serializable categoryValue) {
51 this.categoryId = categoryId;
52 this.categoryValue = categoryValue;
53 }
54
55 public Integer getCategoryId() {
56 return categoryId;
57 }
58
59 public Serializable getCategoryValue() {
60 return categoryValue;
61 }
62 }
63
64 protected final Species species;
65
66 protected final Integer lengthStepCaracteristicId;
67
68 protected Float weight;
69
70 protected Float sampleWeight;
71
72 protected final List<SampleCategory> categories;
73
74 protected final Map<Float, MutableInt> frequencies;
75
76 protected String categoryCode;
77
78 protected boolean applyBothWeightOnCategorizedBatch;
79
80 public PsionImportBatchModel(Species species, Integer lengthStepCaracteristicId) {
81 this.species = species;
82 this.lengthStepCaracteristicId = lengthStepCaracteristicId;
83 this.frequencies = Maps.newTreeMap();
84 this.categories = Lists.newArrayList();
85 }
86
87 public void setCategoryCode(String categoryCode) {
88 this.categoryCode = categoryCode;
89 }
90
91 public void setWeight(Float weight) {
92 this.weight = weight;
93 }
94
95 public void setSampleWeight(Float sampleWeight) {
96 this.sampleWeight = sampleWeight;
97 }
98
99 public void setCategory(Integer categoryId, Serializable categoryValue) {
100 SampleCategory category = new SampleCategory(categoryId, categoryValue);
101 categories.add(category);
102 }
103
104 public void addFrequency(Float size, int number) {
105 MutableInt mutableFloat = frequencies.get(size);
106 if (mutableFloat == null) {
107 mutableFloat = new MutableInt(0);
108 frequencies.put(size, mutableFloat);
109 }
110 mutableFloat.add(number);
111 }
112
113 public Species getSpecies() {
114 return species;
115 }
116
117 public String getCategoryCode() {
118 return categoryCode;
119 }
120
121 public Integer getLengthStepCaracteristicId() {
122 return lengthStepCaracteristicId;
123 }
124
125 public Float getWeight() {
126 return weight;
127 }
128
129 public Float getSampleWeight() {
130 return sampleWeight;
131 }
132
133 public Iterator<SampleCategory> getCategoryIterator() {
134 return categories.iterator();
135 }
136
137 public boolean withFrequencies() {
138 return !frequencies.isEmpty();
139 }
140
141 public boolean withCategories() {
142 return !categories.isEmpty();
143 }
144
145 public Map<Float, MutableInt> getFrequencies() {
146 return frequencies;
147 }
148
149 public int getNbFrequencies() {
150 return frequencies.size();
151 }
152
153 public void setApplyBothWeightOnCategorizedBatch(boolean applyBothWeightOnCategorizedBatch) {
154 this.applyBothWeightOnCategorizedBatch = applyBothWeightOnCategorizedBatch;
155 }
156
157 public boolean isApplyBothWeightOnCategorizedBatch() {
158 return applyBothWeightOnCategorizedBatch;
159 }
160
161 void merge(PsionImportBatchModel batchModel) {
162 setWeight(getWeight() + batchModel.getWeight());
163 setSampleWeight(getSampleWeight() + batchModel.getSampleWeight());
164
165 for (Map.Entry<Float, MutableInt> entry : batchModel.getFrequencies().entrySet()) {
166 Float stepClass = entry.getKey();
167 int number = entry.getValue().intValue();
168 addFrequency(stepClass, number);
169 }
170 }
171
172 @Override
173 public String toString() {
174 return new ToStringBuilder(this)
175 .append("species", species.getSurveyCode())
176 .append("categoryCode", categoryCode)
177 .append("weight", weight)
178 .append("sampleWeight", sampleWeight)
179 .append("frequencies", frequencies.size())
180 .toString();
181 }
182 }