View Javadoc
1   package fr.ifremer.tutti.type;
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 java.math.BigDecimal;
26  import java.text.DecimalFormat;
27  import java.text.DecimalFormatSymbols;
28  import java.util.Comparator;
29  import java.util.Objects;
30  
31  import static org.nuiton.i18n.I18n.n;
32  import static org.nuiton.i18n.I18n.t;
33  
34  /**
35   * Different types of weight unit.
36   *
37   * Created on 09/22/13.
38   *
39   * @author Tony Chemit - chemit@codelutin.com
40   * @since 3.2
41   */
42  public enum WeightUnit implements Comparator<Float> {
43  
44      G(n("application.common.unit.g"), n("application.common.unit.short.g"), 1, "\\d{0,8}(\\.\\d{0,1})?", 0.01f) {
45          @Override
46          public Float fromEntity(Float weight) {
47              return weight == null ? null : weight * 1000;
48          }
49  
50          @Override
51          public Float toEntity(Float weight) {
52              return weight == null ? null : weight / 1000.0f;
53          }
54  
55      },
56      KG(n("application.common.unit.kg"), n("application.common.unit.short.kg"), 4, "\\d{0,6}(\\.\\d{0,4})?", 0.00001f) {
57          @Override
58          public Float fromEntity(Float weight) {
59              return weight;
60          }
61  
62          @Override
63          public Float toEntity(Float weight) {
64              return weight;
65          }
66  
67      };
68  
69      private final String i18nShortKey;
70  
71      private final String i18nKey;
72  
73      private final int numberDigits;
74  
75      private final String numberEditorPattern;
76  
77      private final float rawPrecision;
78  
79      private final DecimalFormat decimalFormat;
80  
81      WeightUnit(String i18nKey, String i18nShortKey, int numberDigits, String numberEditorPattern, float rawPrecision) {
82          this.i18nKey = i18nKey;
83          this.i18nShortKey = i18nShortKey;
84          this.numberDigits = numberDigits;
85          this.numberEditorPattern = numberEditorPattern;
86          this.rawPrecision = rawPrecision;
87          DecimalFormatSymbols symbols = new DecimalFormatSymbols();
88          symbols.setDecimalSeparator('.');
89          symbols.setGroupingSeparator(' ');
90          this.decimalFormat = new DecimalFormat();
91          this.decimalFormat.setDecimalFormatSymbols(symbols);
92          this.decimalFormat.setGroupingUsed(false);
93          this.decimalFormat.setMinimumFractionDigits(1);
94          this.decimalFormat.setMaximumFractionDigits(numberDigits);
95      }
96  
97      @Override
98      public int compare(Float v0, Float v1) {
99  
100         Objects.requireNonNull(v0, "can not compare null weights");
101         Objects.requireNonNull(v1, "can not compare null weights");
102 
103         float delta = roundWithSign(v0) - roundWithSign(v1);
104         int result;
105         if (delta > rawPrecision) {
106             // v0 > v1
107             result = 1;
108         } else if (delta < -rawPrecision) {
109             // v0 < v1
110             result = -1;
111         } else {
112             // v0 == v1
113             result = 0;
114         }
115         return result;
116 
117     }
118 
119     public String getLabel() {
120         return t(i18nKey);
121     }
122 
123     public String getShortLabel() {
124         return t(i18nShortKey);
125     }
126 
127     public int getNumberDigits() {
128         return numberDigits;
129     }
130 
131     public String getNumberEditorPattern() {
132         return numberEditorPattern;
133     }
134 
135     public String renderWeight(Float weight) {
136         String textValue;
137         if (weight == null) {
138             textValue = "";
139         } else {
140             textValue = decimalFormat.format(weight);
141 
142         }
143         return textValue;
144     }
145 
146     /**
147      * Transform the given {@code weight} coming from db to ui.
148      *
149      * @param weight weigth to transform
150      * @return the ui representation of the given {@code weight}.
151      */
152     public abstract Float fromEntity(Float weight);
153 
154     /**
155      * Transform the given {@code weight} coming from ui to db.
156      *
157      * @param weight weigth to transform
158      * @return the db representation of the given {@code weight}.
159      */
160     public abstract Float toEntity(Float weight);
161 
162     public Float round(Float weight) {
163         Float result;
164         if (weight == null) {
165             result = null;
166         } else {
167 
168             BigDecimal sumB = new BigDecimal(String.valueOf(weight))
169                     .setScale(numberDigits, BigDecimal.ROUND_HALF_UP)
170                     .abs();
171             result = sumB.floatValue();
172         }
173         return result;
174     }
175 
176     public Float roundWithSign(Float weight) {
177         Float result;
178         if (weight == null) {
179             result = null;
180         } else {
181 
182             BigDecimal sumB = new BigDecimal(String.valueOf(weight))
183                     .setScale(numberDigits, BigDecimal.ROUND_HALF_UP);
184             result = sumB.floatValue();
185         }
186         return result;
187     }
188 
189     public String decorateLabel(String label) {
190         return String.format("%s (%s)", label, getShortLabel());
191     }
192 
193     public String renderFromEntityWithShortLabel(float o) {
194         return round(fromEntity(o)) + getShortLabel();
195     }
196 
197     public String decorateTip(String tip) {
198         String unit = t("application.common.unit");
199         return String.format("%s (%s %s)", tip, unit, getLabel());
200     }
201 
202     public boolean isNullOrZero(Float o) {
203         return o == null || isZero(o);
204     }
205 
206     public boolean isNotNullNorZero(Float o) {
207         return o != null && !isZero(o);
208     }
209 
210     public boolean isEquals(float o1, float o2) {
211         return compare(o1, o2) == 0;
212     }
213 
214     public boolean isZero(float o) {
215         return isEquals(o, 0f);
216     }
217 
218     public boolean isNotEquals(float o1, float o2) {
219         return compare(o1, o2) != 0;
220     }
221 
222     public boolean isSmallerThan(float o1, float o2) {
223         return compare(o1, o2) < 0;
224     }
225 
226     public boolean isGreaterThan(float o1, float o2) {
227         return compare(o1, o2) > 0;
228     }
229 
230     public boolean isGreaterThanZero(float o) {
231         return isGreaterThan(o, 0f);
232     }
233 
234     public boolean isSmallerThanZero(float o) {
235         return isSmallerThan(o, 0f);
236     }
237 }