]> git.mxchange.org Git - jcore-utils.git/blob - src/org/mxchange/jcoreee/utils/CoreNumberUtils.java
956660ec6a85608d29c5e5a5c3d936f449c7f775
[jcore-utils.git] / src / org / mxchange / jcoreee / utils / CoreNumberUtils.java
1 /*
2  * Copyright (C) 2018 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.jcoreee.utils;
18
19 import java.io.Serializable;
20 import java.util.Objects;
21
22 /**
23  * Null-safe utilities class for numbers
24  * <p>
25  * @author Roland Häder<roland@mxchange.org>
26  */
27 public class CoreNumberUtils implements Serializable {
28
29         /**
30          * Serial number
31          */
32         private static final long serialVersionUID = 153_746_766_826_120L;
33
34         /**
35          * Compares both Integer instances null-safe following the rules of
36          * Comparable.compareTo().
37          * <p>
38          * @param integer1 Integer instance 1
39          * @param integer2 Integer instance 2
40          * <p>
41          * @return Comparison value
42          */
43         public static int compare (final Integer integer1, final Integer integer2) {
44                 // Check instances
45                 if (Objects.equals(integer1, integer2)) {
46                         // Both objects are equal
47                         return 0;
48                 } else if (null == integer1) {
49                         // First number is null (means is smaller than 0)
50                         return -1;
51                 } else if (null == integer2) {
52                         // Second number is null (means is smaller than 0)
53                         return 1;
54                 }
55
56                 // Compare vaules null-safe
57                 return Integer.compare(integer1, integer2);
58         }
59
60         /**
61          * Compares both Long instances null-safe following the rules of
62          * Comparable.compareTo().
63          * <p>
64          * @param long1 Long instance 1
65          * @param long2 Long instance 2
66          * <p>
67          * @return Comparison value
68          */
69         public static int compare (final Long long1, final Long long2) {
70                 // Check instances
71                 if (Objects.equals(long1, long2)) {
72                         // Both objects are equal
73                         return 0;
74                 } else if (null == long1) {
75                         // First number is null (means is smaller than 0)
76                         return -1;
77                 } else if (null == long2) {
78                         // Second number is null (means is smaller than 0)
79                         return 1;
80                 }
81
82                 // Compare vaules null-safe
83                 return Long.compare(long1, long2);
84         }
85
86         /**
87          * Compares both Short instances null-safe following the rules of
88          * Comparable.compareTo().
89          * <p>
90          * @param short1 Short instance 1
91          * @param short2 Short instance 2
92          * <p>
93          * @return Comparison value
94          */
95         public static int compare (final Short short1, final Short short2) {
96                 // Check instances
97                 if (Objects.equals(short1, short2)) {
98                         // Both objects are equal
99                         return 0;
100                 } else if (null == short1) {
101                         // First number is null (means is smaller than 0)
102                         return -1;
103                 } else if (null == short2) {
104                         // Second number is null (means is smaller than 0)
105                         return 1;
106                 }
107
108                 // Compare vaules null-safe
109                 return Short.compare(short1, short2);
110         }
111
112         /**
113          * Utilities classes should not have instances
114          */
115         private CoreNumberUtils () {
116                 // Private constructor
117         }
118
119 }