View Javadoc
1   package fr.ifremer.tutti.persistence.entities.data;
2   
3   /*
4    * #%L
5    * Tutti :: Persistence
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 org.apache.commons.lang3.builder.ReflectionToStringBuilder;
26  
27  import java.io.Serializable;
28  
29  /**
30   * Represents a sample category value in the species / benthos batch.
31   *
32   * @author Tony Chemit - chemit@codelutin.com
33   * @since 0.3
34   */
35  public class SampleCategory<C extends Serializable> implements Serializable {
36  
37      private static final long serialVersionUID = 1L;
38  
39      public static final String PROPERTY_CATEGORY_WEIGHT = "categoryWeight";
40  
41      public static final String PROPERTY_CATEGORY_VALUE = "categoryValue";
42  
43      /**
44       * Sample category definition.
45       *
46       * @since 2.4
47       */
48      protected SampleCategoryModelEntry categoryDef;
49  
50      /**
51       * Sample category value.
52       *
53       * @since 0.3
54       */
55      protected C categoryValue;
56  
57      /**
58       * Sample category weight.
59       *
60       * @since 0.3
61       */
62      protected Float categoryWeight;
63  
64      /**
65       * Sample computed weight.
66       *
67       * @since 1.0
68       */
69      protected Float computedWeight;
70  
71      /**
72       * Is this sample a subsample ?
73       * Available only if the category is the finest category of the row
74       *
75       * @since 1.0
76       */
77      protected boolean subSample;
78  
79      /**
80       * Has the row only one frequency ?
81       * Available only if the category is the finest category of the row
82       *
83       * @since 1.0
84       */
85      protected boolean onlyOneFrequency;
86  
87      public static <C extends Serializable> SampleCategory<C> newSample(SampleCategoryModelEntry categoryDef) {
88          SampleCategory<C> result = new SampleCategory<>();
89          result.setCategoryDef(categoryDef);
90          return result;
91      }
92  
93      protected SampleCategory() {
94      }
95  
96      public SampleCategoryModelEntry getCategoryDef() {
97          return categoryDef;
98      }
99  
100     public void setCategoryDef(SampleCategoryModelEntry categoryDef) {
101         this.categoryDef = categoryDef;
102     }
103 
104     public Integer getCategoryId() {
105         return categoryDef.getCategoryId();
106     }
107 
108     public C getCategoryValue() {
109         return categoryValue;
110     }
111 
112     public void setCategoryValue(C categoryValue) {
113         this.categoryValue = categoryValue;
114     }
115 
116     public Float getCategoryWeight() {
117         return categoryWeight;
118     }
119 
120     public void setCategoryWeight(Float categoryWeight) {
121         this.categoryWeight = categoryWeight;
122     }
123 
124     public Float getComputedWeight() {
125         return computedWeight;
126     }
127 
128     public void setComputedWeight(Float computedWeight) {
129         this.computedWeight = computedWeight;
130     }
131 
132     public boolean isSubSample() {
133         return subSample;
134     }
135 
136     public void setSubSample(boolean subSample) {
137         this.subSample = subSample;
138     }
139 
140     public boolean hasOnlyOneFrequency() {
141         return onlyOneFrequency;
142     }
143 
144     public void setOnlyOneFrequency(boolean onlyOneFrequency) {
145         this.onlyOneFrequency = onlyOneFrequency;
146     }
147 
148     public boolean isValid() {
149         return categoryValue != null;
150     }
151 
152     public boolean isEmpty() {
153         return categoryValue == null
154                && categoryWeight == null
155                && computedWeight == null;
156     }
157 
158     public boolean isEmptyOrValid() {
159         return isEmpty() || isValid();
160     }
161 
162     public Float getNotNullWeight() {
163         Float result = categoryWeight;
164         if (result == null) {
165             result = computedWeight;
166         }
167         return result;
168     }
169 
170     @Override
171     public String toString() {
172         return new ReflectionToStringBuilder(this).
173                 appendSuper(super.toString()).
174                 toString();
175     }
176 }