]> git.mxchange.org Git - jcore-utils.git/blob - src/org/mxchange/jcoreutils/SafeNumberUtils.java
e7914080609e8058fd76d738e62448716773d0b7
[jcore-utils.git] / src / org / mxchange / jcoreutils / SafeNumberUtils.java
1 /*
2  * Copyright (C) 2020 Free Software Foundation
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.jcoreutils;
18
19 import java.io.Serializable;
20 import java.math.BigDecimal;
21 import java.util.Objects;
22
23 /**
24  * Null-safe utilities class for numbers
25  * <p>
26  * @author Roland Häder<roland@mxchange.org>
27  */
28 public class SafeNumberUtils implements Serializable {
29
30         /**
31          * Serial number
32          */
33         private static final long serialVersionUID = 153_746_766_826_120L;
34
35         /**
36          * Compares both Integer instances null-safe following the rules of
37          * Comparable.compareTo().
38          * <p>
39          * @param integer1 Integer instance 1
40          * @param integer2 Integer instance 2
41          * <p>
42          * @return Comparison value
43          */
44         public static int compare (final Integer integer1, final Integer integer2) {
45                 // Check instances
46                 if (Objects.equals(integer1, integer2)) {
47                         // Both objects are equal
48                         return 0;
49                 } else if (null == integer1) {
50                         // First number is null (means is smaller than 0)
51                         return -1;
52                 } else if (null == integer2) {
53                         // Second number is null (means is bigger than 0)
54                         return 1;
55                 }
56
57                 // Compare vaules null-safe
58                 return Integer.compare(integer1, integer2);
59         }
60
61         /**
62          * Compares both Long instances null-safe following the rules of
63          * Comparable.compareTo().
64          * <p>
65          * @param long1 Long instance 1
66          * @param long2 Long instance 2
67          * <p>
68          * @return Comparison value
69          */
70         public static int compare (final Long long1, final Long long2) {
71                 // Check instances
72                 if (Objects.equals(long1, long2)) {
73                         // Both objects are equal
74                         return 0;
75                 } else if (null == long1) {
76                         // First number is null (means is smaller than 0)
77                         return -1;
78                 } else if (null == long2) {
79                         // Second number is null (means is smaller than 0)
80                         return 1;
81                 }
82
83                 // Compare vaules null-safe
84                 return Long.compare(long1, long2);
85         }
86
87         /**
88          * Compares both Short instances null-safe following the rules of
89          * Comparable.compareTo().
90          * <p>
91          * @param short1 Short instance 1
92          * @param short2 Short instance 2
93          * <p>
94          * @return Comparison value
95          */
96         public static int compare (final Short short1, final Short short2) {
97                 // Check instances
98                 if (Objects.equals(short1, short2)) {
99                         // Both objects are equal
100                         return 0;
101                 } else if (null == short1) {
102                         // First number is null (means is smaller than 0)
103                         return -1;
104                 } else if (null == short2) {
105                         // Second number is null (means is smaller than 0)
106                         return 1;
107                 }
108
109                 // Compare vaules null-safe
110                 return Short.compare(short1, short2);
111         }
112
113         /**
114          * Compares both BigDecimal instances null-safe following the rules of
115          * Comparable.compareTo().
116          * <p>
117          * @param decimal1 BigDecimal instance 1
118          * @param decimal2 BigDecimal instance 2
119          * <p>
120          * @return Comparison value
121          */
122         public static int compare (final BigDecimal decimal1, final BigDecimal decimal2) {
123                 // Check instances
124                 if (Objects.equals(decimal1, decimal2)) {
125                         // Both objects are equal
126                         return 0;
127                 } else if (null == decimal1) {
128                         // First number is null (means is smaller than 0)
129                         return -1;
130                 } else if (null == decimal2) {
131                         // Second number is null (means is smaller than 0)
132                         return 1;
133                 }
134
135                 // Compare vaules null-safe
136                 return decimal1.compareTo(decimal2);
137         }
138
139         /**
140          * Utilities classes should not have instances
141          */
142         private SafeNumberUtils () {
143                 // Private constructor
144         }
145
146 }