View Javadoc
1   package pl.matsuo.core.util;
2   
3   import java.math.BigDecimal;
4   import java.util.function.BinaryOperator;
5   import java.util.function.Function;
6   
7   import static java.math.BigDecimal.*;
8   
9   
10  /**
11   * Pomocnicze metody przy operacjach na liczbach
12   * @author Marek Romanowski
13   * @since Jul 22, 2013
14   */
15  public class NumberUtil {
16  
17  
18    public static final BinaryOperator<BigDecimal> sumBigDecimal = (sum, add) -> sum.add(add);
19  
20  
21    /**
22     * Tworzy nowego {@link BigDecimal}a.
23     */
24    public static final <E> Function<String, E> createObject(Function<String, E> fn) {
25      return (String value) -> {
26        if (value == null) {
27          return null;
28        }
29  
30        return fn.apply(value);
31      };
32    }
33  
34  
35    /**
36     * Tworzy nowego {@link BigDecimal}a.
37     */
38    public static final BigDecimal bd(String value) {
39      return createObject(BigDecimal::new).apply(value);
40    }
41  
42  
43    /**
44     * Tworzy nowego {@link BigDecimal}a.
45     */
46    public static final BigDecimal bd(Integer value) {
47      return BigDecimal.valueOf(value);
48    }
49  
50  
51    /**
52     * Tworzy nowego {@link Integer}a.
53     */
54    public static final Integer i(String value) {
55      return createObject(Integer::valueOf).apply(value);
56    }
57  
58  
59    /**
60     * Tworzy nowego {@link Integer}a.
61     */
62    public static final Integer i(int value) {
63      return value;
64    }
65  
66  
67    public static BigDecimal safeAddBD(BigDecimal bd1, BigDecimal bd2) {
68      if (bd1 == null && bd2 == null) {
69        return ZERO;
70      } else if (bd2 == null) {
71        return bd1;
72      } else if (bd1 == null) {
73        return bd2;
74      } else {
75        return bd1.add(bd2);
76      }
77    }
78  }
79