From c1747b1a98e4c94eaf483a1b38f2004233024e01 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Thu, 25 Jan 2018 23:05:34 +0100 Subject: [PATCH] Continued: - added method FacesUtils.comareTo() and FacesUtils.compareToIgnoreCase() which are a null-safe compareTo()/compareToIgnoreCase() methods MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../mxchange/jcoreee/utils/FacesUtils.java | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/src/org/mxchange/jcoreee/utils/FacesUtils.java b/src/org/mxchange/jcoreee/utils/FacesUtils.java index e90b245..f015aa9 100644 --- a/src/org/mxchange/jcoreee/utils/FacesUtils.java +++ b/src/org/mxchange/jcoreee/utils/FacesUtils.java @@ -78,6 +78,66 @@ public class FacesUtils { return baseUrl; } + /** + * 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 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 + *

+ * @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 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); + } + /** * No instances from this class are required */ -- 2.39.5