1 package fr.ifremer.tutti.type;
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 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
36
37
38
39
40
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
107 result = 1;
108 } else if (delta < -rawPrecision) {
109
110 result = -1;
111 } else {
112
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
148
149
150
151
152 public abstract Float fromEntity(Float weight);
153
154
155
156
157
158
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 }