From: Roland Häder Date: Sun, 18 Mar 2018 21:46:46 +0000 (+0100) Subject: Continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=d29b245028c1f666665eae822b45b96369c30dd4;p=jcore-utils.git Continued: - removed StringUtils class as Apache Lang3 is around ... (ops) - added CoreNumberUtils class which is a null-safe (no NPE will be thrown) alternative to Apache's Common Lang3's NumberUtils class Signed-off-by: Roland Häder --- diff --git a/src/org/mxchange/jcoreee/utils/CoreNumberUtils.java b/src/org/mxchange/jcoreee/utils/CoreNumberUtils.java new file mode 100644 index 0000000..956660e --- /dev/null +++ b/src/org/mxchange/jcoreee/utils/CoreNumberUtils.java @@ -0,0 +1,119 @@ +/* + * Copyright (C) 2018 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 . + */ +package org.mxchange.jcoreee.utils; + +import java.io.Serializable; +import java.util.Objects; + +/** + * Null-safe utilities class for numbers + *

+ * @author Roland Häder + */ +public class CoreNumberUtils 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(). + *

+ * @param integer1 Integer instance 1 + * @param integer2 Integer instance 2 + *

+ * @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 smaller 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(). + *

+ * @param long1 Long instance 1 + * @param long2 Long instance 2 + *

+ * @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(). + *

+ * @param short1 Short instance 1 + * @param short2 Short instance 2 + *

+ * @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); + } + + /** + * Utilities classes should not have instances + */ + private CoreNumberUtils () { + // Private constructor + } + +} diff --git a/src/org/mxchange/jcoreee/utils/StringUtils.java b/src/org/mxchange/jcoreee/utils/StringUtils.java deleted file mode 100644 index e69535a..0000000 --- a/src/org/mxchange/jcoreee/utils/StringUtils.java +++ /dev/null @@ -1,100 +0,0 @@ -/* - * Copyright (C) 2018 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 . - */ -package org.mxchange.jcoreee.utils; - -import java.io.Serializable; - -/** - * String utilities class - *

- * @author Roland Häder - */ -public class StringUtils implements Serializable { - - /** - * Serial number - */ - private static final long serialVersionUID = 30_894_676_712_650L; - - /** - * Compares both string with null-safety. This method is based on the - * example from - * https://codereview.stackexchange.com/questions/20191/comparing-two-strings-which-could-be-null-or-blank-in-a-comparator - *

- * @param str0 First string - * @param str1 Second string - *

- * @return Comparison value, 0 means equals, -1 means str0 smaller str2 and - * 2 means str0 bigger str2 - */ - @SuppressWarnings ("null") - public static int compareTo (final String str0, final String str1) { - // Check both strings for null and empty - boolean isStr0Empty = (str0 == null || str0.isEmpty()); - boolean isStr1Empty = (str1 == null || str1.isEmpty()); - - // Check conditions - if (isStr0Empty && isStr1Empty) { - return 0; - } else if (isStr0Empty) { - return -1; - } else if (isStr1Empty) { - return 1; - } - - // Compare both - return str0.compareTo(str1); - } - - /** - * Compares both string with null-safety, ignoring case-sensitivity. This - * method is based on the example from - * https://codereview.stackexchange.com/questions/20191/comparing-two-strings-which-could-be-null-or-blank-in-a-comparator - *

- * @param str0 First string - * @param str1 Second string - *

- * @return Comparison value, 0 means equals, -1 means str0 smaller str2 and - * 2 means str0 bigger str2 - */ - @SuppressWarnings ("null") - public static int compareToIgnoreCase (final String str0, final String str1) { - // Check both strings for null and empty - boolean isStr0Empty = (str0 == null || str0.isEmpty()); - boolean isStr1Empty = (str1 == null || str1.isEmpty()); - - // Check conditions - if (isStr0Empty && isStr1Empty) { - return 0; - } else if (isStr0Empty) { - return -1; - } else if (isStr1Empty) { - return 1; - } - - // Compare both - return str0.compareToIgnoreCase(str1); - } - - /** - * Utility classes don't have instances - */ - private StringUtils () { - // Empty constructor - } - -}