2 * Copyright (C) 2020 Free Software Foundation
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.
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.
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/>.
17 package org.mxchange.jcoreutils;
19 import java.io.Serializable;
20 import java.math.BigDecimal;
21 import java.util.Objects;
24 * Null-safe utilities class for numbers
26 * @author Roland Häder<roland@mxchange.org>
28 public class SafeNumberUtils implements Serializable {
33 private static final long serialVersionUID = 153_746_766_826_120L;
36 * Compares both Integer instances null-safe following the rules of
37 * Comparable.compareTo().
39 * @param integer1 Integer instance 1
40 * @param integer2 Integer instance 2
42 * @return Comparison value
44 public static int compare (final Integer integer1, final Integer integer2) {
46 if (Objects.equals(integer1, integer2)) {
47 // Both objects are equal
49 } else if (null == integer1) {
50 // First number is null (means is smaller than 0)
52 } else if (null == integer2) {
53 // Second number is null (means is bigger than 0)
57 // Compare vaules null-safe
58 return Integer.compare(integer1, integer2);
62 * Compares both Long instances null-safe following the rules of
63 * Comparable.compareTo().
65 * @param long1 Long instance 1
66 * @param long2 Long instance 2
68 * @return Comparison value
70 public static int compare (final Long long1, final Long long2) {
72 if (Objects.equals(long1, long2)) {
73 // Both objects are equal
75 } else if (null == long1) {
76 // First number is null (means is smaller than 0)
78 } else if (null == long2) {
79 // Second number is null (means is smaller than 0)
83 // Compare vaules null-safe
84 return Long.compare(long1, long2);
88 * Compares both Short instances null-safe following the rules of
89 * Comparable.compareTo().
91 * @param short1 Short instance 1
92 * @param short2 Short instance 2
94 * @return Comparison value
96 public static int compare (final Short short1, final Short short2) {
98 if (Objects.equals(short1, short2)) {
99 // Both objects are equal
101 } else if (null == short1) {
102 // First number is null (means is smaller than 0)
104 } else if (null == short2) {
105 // Second number is null (means is smaller than 0)
109 // Compare vaules null-safe
110 return Short.compare(short1, short2);
114 * Compares both BigDecimal instances null-safe following the rules of
115 * Comparable.compareTo().
117 * @param decimal1 BigDecimal instance 1
118 * @param decimal2 BigDecimal instance 2
120 * @return Comparison value
122 public static int compare (final BigDecimal decimal1, final BigDecimal decimal2) {
124 if (Objects.equals(decimal1, decimal2)) {
125 // Both objects are equal
127 } else if (null == decimal1) {
128 // First number is null (means is smaller than 0)
130 } else if (null == decimal2) {
131 // Second number is null (means is smaller than 0)
135 // Compare vaules null-safe
136 return decimal1.compareTo(decimal2);
140 * Utilities classes should not have instances
142 private SafeNumberUtils () {
143 // Private constructor