View Javadoc
1   package fr.ifremer.tutti.util;
2   
3   /*
4    * #%L
5    * Tutti :: Persistence
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2012 - 2014 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU General Public License as
13   * published by the Free Software Foundation, either version 3 of the
14   * License, or (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU General Public
22   * License along with this program.  If not, see
23   * <http://www.gnu.org/licenses/gpl-3.0.html>.
24   * #L%
25   */
26  
27  import java.math.BigDecimal;
28  
29  /**
30   * Created on 8/26/14.
31   *
32   * @author Tony Chemit - chemit@codelutin.com
33   * @since 3.7
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       * Round the given value to max 5 digits.
71       *
72       * @param value the float to round.
73       * @return the rounded value
74       * @since 4.2
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              // pourquoi abs() ?
82  //                    .abs();
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              // measurement in cm asked
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              // measurement in mm asked
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             // measurement in cm asked
107             source = source / 10;
108         }
109         return source;
110     }
111 
112 }