]> git.mxchange.org Git - jcore-utils.git/blobdiff - src/org/mxchange/jcoreutils/number/SafeNumberUtils.java
Continued:
[jcore-utils.git] / src / org / mxchange / jcoreutils / number / SafeNumberUtils.java
diff --git a/src/org/mxchange/jcoreutils/number/SafeNumberUtils.java b/src/org/mxchange/jcoreutils/number/SafeNumberUtils.java
new file mode 100644 (file)
index 0000000..6929de2
--- /dev/null
@@ -0,0 +1,146 @@
+/*
+ * Copyright (C) 2022 Free Software Foundation
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.mxchange.jcoreutils.number;
+
+import java.io.Serializable;
+import java.math.BigDecimal;
+import java.util.Objects;
+
+/**
+ * Null-safe utilities class for numbers
+ * <p>
+ * @author Roland Häder<roland@mxchange.org>
+ */
+public class SafeNumberUtils implements Serializable {
+
+       /**
+        * Serial number
+        */
+       private static final long serialVersionUID = 153_746_766_826_120L;
+
+       /**
+        * Compares both Integer instances null-safe following the rules of
+        * Comparable.compareTo().
+        * <p>
+        * @param integer1 Integer instance 1
+        * @param integer2 Integer instance 2
+        * <p>
+        * @return Comparison value
+        */
+       public static int compare (final Integer integer1, final Integer integer2) {
+               // Check instances
+               if (Objects.equals(integer1, integer2)) {
+                       // Both objects are equal
+                       return 0;
+               } else if (null == integer1) {
+                       // First number is null (means is smaller than 0)
+                       return -1;
+               } else if (null == integer2) {
+                       // Second number is null (means is bigger than 0)
+                       return 1;
+               }
+
+               // Compare vaules null-safe
+               return Integer.compare(integer1, integer2);
+       }
+
+       /**
+        * Compares both Long instances null-safe following the rules of
+        * Comparable.compareTo().
+        * <p>
+        * @param long1 Long instance 1
+        * @param long2 Long instance 2
+        * <p>
+        * @return Comparison value
+        */
+       public static int compare (final Long long1, final Long long2) {
+               // Check instances
+               if (Objects.equals(long1, long2)) {
+                       // Both objects are equal
+                       return 0;
+               } else if (null == long1) {
+                       // First number is null (means is smaller than 0)
+                       return -1;
+               } else if (null == long2) {
+                       // Second number is null (means is smaller than 0)
+                       return 1;
+               }
+
+               // Compare vaules null-safe
+               return Long.compare(long1, long2);
+       }
+
+       /**
+        * Compares both Short instances null-safe following the rules of
+        * Comparable.compareTo().
+        * <p>
+        * @param short1 Short instance 1
+        * @param short2 Short instance 2
+        * <p>
+        * @return Comparison value
+        */
+       public static int compare (final Short short1, final Short short2) {
+               // Check instances
+               if (Objects.equals(short1, short2)) {
+                       // Both objects are equal
+                       return 0;
+               } else if (null == short1) {
+                       // First number is null (means is smaller than 0)
+                       return -1;
+               } else if (null == short2) {
+                       // Second number is null (means is smaller than 0)
+                       return 1;
+               }
+
+               // Compare vaules null-safe
+               return Short.compare(short1, short2);
+       }
+
+       /**
+        * Compares both BigDecimal instances null-safe following the rules of
+        * Comparable.compareTo().
+        * <p>
+        * @param decimal1 BigDecimal instance 1
+        * @param decimal2 BigDecimal instance 2
+        * <p>
+        * @return Comparison value
+        */
+       public static int compare (final BigDecimal decimal1, final BigDecimal decimal2) {
+               // Check instances
+               if (Objects.equals(decimal1, decimal2)) {
+                       // Both objects are equal
+                       return 0;
+               } else if (null == decimal1) {
+                       // First number is null (means is smaller than 0)
+                       return -1;
+               } else if (null == decimal2) {
+                       // Second number is null (means is smaller than 0)
+                       return 1;
+               }
+
+               // Compare vaules null-safe
+               return decimal1.compareTo(decimal2);
+       }
+
+       /**
+        * Utilities classes should not have instances
+        */
+       private SafeNumberUtils () {
+               // Private constructor
+       }
+
+}