2 * Copyright (C) 2018 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.jcoreee.utils;
19 import java.io.Serializable;
20 import java.util.Objects;
23 * Null-safe utilities class for numbers
25 * @author Roland Häder<roland@mxchange.org>
27 public class CoreNumberUtils implements Serializable {
32 private static final long serialVersionUID = 153_746_766_826_120L;
35 * Compares both Integer instances null-safe following the rules of
36 * Comparable.compareTo().
38 * @param integer1 Integer instance 1
39 * @param integer2 Integer instance 2
41 * @return Comparison value
43 public static int compare (final Integer integer1, final Integer integer2) {
45 if (Objects.equals(integer1, integer2)) {
46 // Both objects are equal
48 } else if (null == integer1) {
49 // First number is null (means is smaller than 0)
51 } else if (null == integer2) {
52 // Second number is null (means is smaller than 0)
56 // Compare vaules null-safe
57 return Integer.compare(integer1, integer2);
61 * Compares both Long instances null-safe following the rules of
62 * Comparable.compareTo().
64 * @param long1 Long instance 1
65 * @param long2 Long instance 2
67 * @return Comparison value
69 public static int compare (final Long long1, final Long long2) {
71 if (Objects.equals(long1, long2)) {
72 // Both objects are equal
74 } else if (null == long1) {
75 // First number is null (means is smaller than 0)
77 } else if (null == long2) {
78 // Second number is null (means is smaller than 0)
82 // Compare vaules null-safe
83 return Long.compare(long1, long2);
87 * Compares both Short instances null-safe following the rules of
88 * Comparable.compareTo().
90 * @param short1 Short instance 1
91 * @param short2 Short instance 2
93 * @return Comparison value
95 public static int compare (final Short short1, final Short short2) {
97 if (Objects.equals(short1, short2)) {
98 // Both objects are equal
100 } else if (null == short1) {
101 // First number is null (means is smaller than 0)
103 } else if (null == short2) {
104 // Second number is null (means is smaller than 0)
108 // Compare vaules null-safe
109 return Short.compare(short1, short2);
113 * Utilities classes should not have instances
115 private CoreNumberUtils () {
116 // Private constructor