1 package fr.ifremer.tutti.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 import java.math.BigDecimal;
28
29
30
31
32
33
34
35 public class Numbers {
36
37 public static final String MM_UNIT = "mm";
38
39 public static final String CM_UNIT = "cm";
40
41 public static float getRoundedLengthStep(float lengthStep, boolean aroundUp) {
42 int intValue = (int) ((lengthStep + (aroundUp ? 0.001f : 0f)) * 10);
43 return intValue / 10f;
44 }
45
46 public static <N extends Number> N getValueOrComputedValue(N value, N computedValue) {
47 return value == null ? computedValue : value;
48 }
49
50 public static <N extends Number> Boolean getValueOrComputedValueComputed(N value, N computedValue) {
51 Boolean result;
52 if (value == null) {
53
54 result = computedValue == null ? null : true;
55 } else {
56 result = false;
57 }
58 return result;
59 }
60
61 public static int roundToInt(float floatValue) {
62 int intValue = (int) floatValue;
63 if (floatValue - (float) intValue >= 0.5) {
64 intValue++;
65 }
66 return intValue;
67 }
68
69
70
71
72
73
74
75
76 public static Float roundDecimalCoordinateComponent(Float value) {
77 Float result = null;
78 if (value != null) {
79 BigDecimal sumB = new BigDecimal(String.valueOf(value))
80 .setScale(4, BigDecimal.ROUND_HALF_UP);
81
82
83 result = sumB.floatValue();
84 }
85 return result;
86 }
87
88 public static float convertToCm(float source, String sourceUnit) {
89 if (MM_UNIT.equals(sourceUnit)) {
90
91 source = source / 10;
92 }
93 return source;
94 }
95
96 public static int convertToMm(float source, String sourceUnit) {
97 if (CM_UNIT.equals(sourceUnit)) {
98
99 source = source * 10;
100 }
101 return (int) source;
102 }
103
104 public static float convertFromMm(float source, String targetUnit) {
105 if (CM_UNIT.equals(targetUnit)) {
106
107 source = source / 10;
108 }
109 return source;
110 }
111
112 }