]> git.mxchange.org Git - jcore-utils.git/blobdiff - src/org/mxchange/jcoreee/utils/StringUtils.java
Continued:
[jcore-utils.git] / src / org / mxchange / jcoreee / utils / StringUtils.java
diff --git a/src/org/mxchange/jcoreee/utils/StringUtils.java b/src/org/mxchange/jcoreee/utils/StringUtils.java
new file mode 100644 (file)
index 0000000..7a3d6c2
--- /dev/null
@@ -0,0 +1,100 @@
+/*
+ * Copyright (C) 2018 Roland Häder<roland@mxchange.org>
+ *
+ * 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.jcoreee.utils;
+
+import java.io.Serializable;
+
+/**
+ * String utilities class
+ * <p>
+ * @author Roland Häder<roland@mxchange.org>
+ */
+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
+        * <p>
+        * @param str0 First string
+        * @param str1 Second string
+        * <p>
+        * @return Comparison value, 0 means equals, -1 means str0 smaller str2 and
+        *         2 means str0 bigger str2
+        */
+       @SuppressWarnings ("null")
+       public static int comareTo (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
+        * <p>
+        * @param str0 First string
+        * @param str1 Second string
+        * <p>
+        * @return Comparison value, 0 means equals, -1 means str0 smaller str2 and
+        *         2 means str0 bigger str2
+        */
+       @SuppressWarnings ("null")
+       public static int comareToIgnoreCase (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
+       }
+
+}