View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.operation.catches.species.frequency;
2   
3   /*
4    * #%L
5    * Tutti :: UI
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 com.google.common.collect.Lists;
26  import com.google.common.collect.Ordering;
27  import fr.ifremer.tutti.persistence.entities.data.SpeciesBatch;
28  import fr.ifremer.tutti.persistence.entities.data.SpeciesBatchFrequency;
29  import fr.ifremer.tutti.persistence.entities.data.SpeciesBatchFrequencys;
30  import fr.ifremer.tutti.persistence.entities.referential.Caracteristic;
31  import fr.ifremer.tutti.type.WeightUnit;
32  import fr.ifremer.tutti.ui.swing.util.AbstractTuttiBeanUIModel;
33  import org.nuiton.util.beans.Binder;
34  import org.nuiton.util.beans.BinderFactory;
35  
36  import java.util.List;
37  
38  /**
39   * Represents a batch frequency row.
40   *
41   * @author Tony Chemit - chemit@codelutin.com
42   * @since 0.2
43   */
44  public class SpeciesFrequencyRowModel extends AbstractTuttiBeanUIModel<SpeciesBatchFrequency, SpeciesFrequencyRowModel> implements SpeciesBatchFrequency, Comparable<SpeciesFrequencyRowModel> {
45  
46      private static final long serialVersionUID = 1L;
47  
48      public static final String PROPERTY_LENGTH_STEP = "lengthStep";
49  
50      public static final String PROPERTY_NUMBER = "number";
51  
52      public static final String PROPERTY_WEIGHT = "weight";
53  
54      public static final String PROPERTY_RTP_COMPUTED_WEIGHT = "rtpComputedWeight";
55  
56      public static final String PROPERTY_LENGHT_STEP_CARACTERISTIC = "lengthStepCaracteristic";
57  
58      /**
59       * Length step.
60       *
61       * @since 0.2
62       */
63      protected Float lengthStep;
64  
65      /**
66       * Count of fishes for this lengthStep.
67       *
68       * @since 0.2
69       */
70      protected Integer number;
71  
72      /**
73       * Weight of fishes observed.
74       *
75       * @since 4.5
76       */
77      protected Float weight;
78  
79      /**
80       * Weight of fishes computed with RTPs.
81       *
82       * @since 4.5
83       */
84      protected Float rtpComputedWeight;
85  
86      /**
87       * Length step caracteristic.
88       *
89       * @since 0.3
90       */
91      protected Caracteristic lengthStepCaracteristic;
92  
93      /**
94       * Weight unit.
95       *
96       * @since 2.5
97       */
98      protected final WeightUnit weightUnit;
99  
100     protected static final Binder<SpeciesBatchFrequency, SpeciesFrequencyRowModel> fromBeanBinder =
101             BinderFactory.newBinder(SpeciesBatchFrequency.class,
102                                     SpeciesFrequencyRowModel.class);
103 
104     protected static final Binder<SpeciesFrequencyRowModel, SpeciesBatchFrequency> toBeanBinder =
105             BinderFactory.newBinder(SpeciesFrequencyRowModel.class,
106                                     SpeciesBatchFrequency.class);
107 
108     private static final Ordering<Float> ordering = Ordering.natural().nullsFirst();
109 
110     //------------------------------------------------------------------------//
111     //-- Factory methods                                                    --//
112     //------------------------------------------------------------------------//
113 
114     public static List<SpeciesFrequencyRowModel> fromEntity(WeightUnit weightUnit,
115                                                             List<SpeciesBatchFrequency> entities) {
116         List<SpeciesFrequencyRowModel> result = Lists.newArrayList();
117         for (SpeciesBatchFrequency entity : entities) {
118 
119             SpeciesFrequencyRowModel row =
120                     new SpeciesFrequencyRowModel(weightUnit);
121             row.fromEntity(entity);
122             result.add(row);
123         }
124         return result;
125     }
126 
127     public static List<SpeciesBatchFrequency> toEntity(List<SpeciesFrequencyRowModel> rows,
128                                                        SpeciesBatch batch) {
129         List<SpeciesBatchFrequency> result = Lists.newArrayList();
130         for (SpeciesFrequencyRowModel row : rows) {
131 
132             SpeciesBatchFrequency entity = row.toEntity();
133             entity.setBatch(batch);
134             result.add(entity);
135         }
136         return result;
137     }
138 
139     public SpeciesFrequencyRowModel(WeightUnit weightUnit) {
140         super(fromBeanBinder, toBeanBinder);
141         this.weightUnit = weightUnit;
142     }
143 
144     //------------------------------------------------------------------------//
145     //-- AbstractTuttiBeanUIModel                                           --//
146     //------------------------------------------------------------------------//
147 
148     @Override
149     protected SpeciesBatchFrequency newEntity() {
150         return SpeciesBatchFrequencys.newSpeciesBatchFrequency();
151     }
152 
153     @Override
154     public void fromEntity(SpeciesBatchFrequency entity) {
155         super.fromEntity(entity);
156 
157         // convert weight
158         setWeight(weightUnit.fromEntity(getWeight()));
159     }
160 
161     @Override
162     public SpeciesBatchFrequency toEntity() {
163         SpeciesBatchFrequency result = super.toEntity();
164 
165         // convert weight
166         result.setWeight(weightUnit.toEntity(getWeight()));
167         return result;
168     }
169 
170     //------------------------------------------------------------------------//
171     //-- SpeciesBatchFrequency                                              --//
172     //------------------------------------------------------------------------//
173 
174     @Override
175     public Float getLengthStep() {
176         return lengthStep;
177     }
178 
179     @Override
180     public void setLengthStep(Float lengthStep) {
181         Object oldValue = getLengthStep();
182         this.lengthStep = lengthStep;
183         firePropertyChange(PROPERTY_LENGTH_STEP, oldValue, lengthStep);
184     }
185 
186     @Override
187     public Integer getNumber() {
188         return number;
189     }
190 
191     @Override
192     public void setNumber(Integer number) {
193         Object oldValue = getNumber();
194         this.number = number;
195         firePropertyChange(PROPERTY_NUMBER, oldValue, number);
196     }
197 
198     public boolean withNumber() {
199         return number != null && number > 0;
200     }
201 
202     public void incNumber() {
203         if (number == null) {
204             number = 0;
205         }
206         setNumber(number + 1);
207     }
208 
209     public void decNumber() {
210         decNumber(1);
211     }
212 
213     public void decNumber(int nb) {
214         if (number != null && number >= nb) {
215             setNumber(number - nb);
216         }
217     }
218 
219     @Override
220     public Float getWeight() {
221         return weight;
222     }
223 
224     @Override
225     public void setWeight(Float weight) {
226         Object oldValue = getWeight();
227         this.weight = weight;
228         firePropertyChange(PROPERTY_WEIGHT, oldValue, weight);
229     }
230 
231     public boolean withWeight() {
232         Float weight = getWeight();
233         return weightUnit.isNotNullNorZero(weight);
234     }
235 
236     /**
237      * @param weightToAdd weight (cannot be negative) to add
238      */
239     public void addToWeight(float weightToAdd) {
240         if (weightUnit.isSmallerThanZero(weightToAdd)) {
241             throw new IllegalArgumentException("you must add a positive weight");
242         }
243         if (weight == null) {
244             weight = 0f;
245         }
246         setWeight(weightUnit.round(weight + weightToAdd));
247     }
248 
249     /**
250      * @param weightToRemove weight (cannot be negative) to remove
251      */
252     public void removeFromWeight(float weightToRemove) {
253         if (weightUnit.isSmallerThanZero(weightToRemove)) {
254             throw new IllegalArgumentException("you must remove a positive weight");
255         }
256         if (weight == null) {
257             weight = 0f;
258         }
259         if (weightUnit.isSmallerThan(weight, weightToRemove)) {
260             throw new IllegalArgumentException("the weight to remove cannot be greater than the initial weight");
261         }
262         setWeight(weightUnit.round(weight - weightToRemove));
263     }
264 
265     public Float getRtpComputedWeight() {
266         return rtpComputedWeight;
267     }
268 
269     public void setRtpComputedWeight(Float rtpComputedWeight) {
270         Object oldValue = getRtpComputedWeight();
271         this.rtpComputedWeight = rtpComputedWeight;
272         firePropertyChange(PROPERTY_RTP_COMPUTED_WEIGHT, oldValue, rtpComputedWeight);
273     }
274 
275     public boolean withRtpComputedWeight() {
276         return rtpComputedWeight != null;
277     }
278 
279     @Override
280     public Caracteristic getLengthStepCaracteristic() {
281         return lengthStepCaracteristic;
282     }
283 
284     @Override
285     public void setLengthStepCaracteristic(Caracteristic lengthStepCaracteristic) {
286         Object oldValue = getLengthStepCaracteristic();
287         this.lengthStepCaracteristic = lengthStepCaracteristic;
288         firePropertyChange(PROPERTY_LENGHT_STEP_CARACTERISTIC, oldValue, lengthStepCaracteristic);
289     }
290 
291     @Override
292     public SpeciesBatch getBatch() {
293         return null;
294     }
295 
296     @Override
297     public void setBatch(SpeciesBatch batch) {
298     }
299 
300     @Override
301     public int compareTo(SpeciesFrequencyRowModel o) {
302         return ordering.compare(lengthStep, o.lengthStep);
303     }
304 
305     @Override
306     public Integer getRankOrder() {
307         return null;
308     }
309 
310     @Override
311     public void setRankOrder(Integer rankOrder) {
312     }
313 
314     @Override
315     public boolean isBenthosFrequencyBatch() {
316         return false;
317     }
318 
319     @Override
320     public void setBenthosFrequencyBatch(boolean benthosFrequencyBatch) {
321         //Never!
322     }
323 
324     public boolean isEmpty() {
325         return lengthStep == null && (weight == null || number == null);
326     }
327 
328     public Float computeAverageWeight() {
329         Float averageWeight = null;
330         if (withWeight() && withNumber()) {
331             averageWeight = weight / number;
332         }
333         return averageWeight;
334     }
335 
336     public void copy(SpeciesFrequencyRowModel source) {
337         setLengthStepCaracteristic(source.getLengthStepCaracteristic());
338         setLengthStep(source.getLengthStep());
339         setNumber(source.getNumber());
340         setWeight(source.getWeight());
341     }
342 
343     public boolean withLengthStep() {
344         return lengthStep != null;
345     }
346 }