]> git.mxchange.org Git - jcontacts-core.git/commitdiff
Continued:
authorRoland Häder <roland@mxchange.org>
Sat, 12 Nov 2022 00:42:06 +0000 (01:42 +0100)
committerRoland Häder <roland@mxchange.org>
Sat, 12 Nov 2022 02:08:14 +0000 (03:08 +0100)
- moved PersonalTitleUtils to proper package
- added unit test for method PersonalTitleUtils.compare()
- used own BooleanUtils class for null-safe comparison, Boolean.compare() seems
  to be not null-safe

src/org/mxchange/jcontacts/model/contact/UserContact.java
src/org/mxchange/jcontacts/model/contact/title/PersonalTitleUtils.java [deleted file]
src/org/mxchange/jcontacts/model/utils/PersonalTitleUtils.java [new file with mode: 0644]
test/org/mxchange/jcontacts/model/contact/title/PersonalTitleUtilsTest.java [new file with mode: 0644]

index a34c684fbd7cead6ad62b3b40f0829b23d0dccfa..69bbe975519e68132bcd28a2256ff31964edb048 100644 (file)
@@ -40,6 +40,8 @@ import javax.persistence.TemporalType;
 import javax.persistence.Transient;
 import org.apache.commons.lang3.StringUtils;
 import org.mxchange.jcontacts.model.contact.title.PersonalTitle;
+import org.mxchange.jcontacts.model.utils.PersonalTitleUtils;
+import org.mxchange.jcoreutils.bool.BooleanUtils;
 import org.mxchange.jcoreutils.comparable.ComparableUtils;
 import org.mxchange.jcoreutils.dates.DateUtils;
 import org.mxchange.jcoreutils.number.SafeNumberUtils;
@@ -365,7 +367,7 @@ public class UserContact implements Contact {
                        // ... extension
                        StringUtils.compareIgnoreCase(this.getContactHouseNumberExtension(), contact.getContactHouseNumberExtension()),
                        // ... now it is sure that address is different/same, continue with personal title
-                       this.getContactPersonalTitle().compareTo(contact.getContactPersonalTitle()),
+                       PersonalTitleUtils.compare(this.getContactPersonalTitle(), contact.getContactPersonalTitle()),
                        // ... academical title
                        StringUtils.compareIgnoreCase(this.getContactTitle(), contact.getContactTitle()),
                        // .. family name ...
@@ -377,7 +379,7 @@ public class UserContact implements Contact {
                        // ... next birthday - year
                        DateUtils.compareYearMonthDay(this.getContactBirthday(), contact.getContactBirthday()),
                        // ... next "is own" flag
-                       Boolean.compare(this.isOwnContact(), contact.isOwnContact()),
+                       BooleanUtils.compare(this.isOwnContact(), contact.isOwnContact()),
                        // ... next comment
                        StringUtils.compare(this.getContactComment(), contact.getContactComment())
                };
diff --git a/src/org/mxchange/jcontacts/model/contact/title/PersonalTitleUtils.java b/src/org/mxchange/jcontacts/model/contact/title/PersonalTitleUtils.java
deleted file mode 100644 (file)
index 419fb2a..0000000
+++ /dev/null
@@ -1,111 +0,0 @@
-/*
- * Copyright (C) 2016 - 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.jcontacts.model.contact.title;
-
-import java.io.Serializable;
-import java.text.MessageFormat;
-import java.util.LinkedList;
-import java.util.List;
-
-/**
- * Title utilities class
- * <p>
- * @author Roland Häder<roland@mxchange.org>
- */
-public class PersonalTitleUtils implements Serializable {
-
-       /**
-        * Serial number
-        */
-       private static final long serialVersionUID = 185_683_479_107L;
-
-       /**
-        * Cache for valid chars
-        */
-       private static char[] validChars;
-
-       /**
-        * Getter for personal title enumeration from given character
-        * <p>
-        * @param c PersonalTitle character
-        * <p>
-        * @return PersonalTitle enumeration
-        */
-       public static PersonalTitle getPersonalTitleFromChar (final char c) {
-               // Init variable
-               PersonalTitle found = null;
-
-               // Loop through all
-               for (final PersonalTitle title : PersonalTitle.values()) {
-                       // Does the char match?
-                       if (c == title.getAccessChar()) {
-                               // Found it
-                               found = title;
-                               break;
-                       }
-               }
-
-               // Still null?
-               if (null == found) {
-                       // Didn't found a valid one
-                       throw new IllegalArgumentException(MessageFormat.format("Gender {0} is invalid.", c)); //NOI18N
-               }
-
-               // Return it
-               return found;
-       }
-
-       /**
-        * Valid chars (mostly for console client)
-        * <p>
-        * @return Valid chars
-        */
-       @SuppressWarnings ("ReturnOfCollectionOrArrayField")
-       public static char[] validChars () {
-               // Is cache set?
-               if (validChars != null) {
-                       // Return it
-                       return validChars;
-               }
-
-               // Init array, only 2 are valid.
-               char[] valid = new char[2];
-
-               // Get values
-               int i = 0;
-               for (final PersonalTitle title : PersonalTitle.values()) {
-                       // Get access key as this is also the access
-                       valid[i] = title.getAccessChar();
-
-                       // Increment index
-                       i++;
-               }
-
-               // Set it here
-               validChars = valid;
-
-               // Return finialized array
-               return valid;
-       }
-
-       /**
-        * Private constructor as this is an utility class
-        */
-       private PersonalTitleUtils () {
-       }
-
-}
diff --git a/src/org/mxchange/jcontacts/model/utils/PersonalTitleUtils.java b/src/org/mxchange/jcontacts/model/utils/PersonalTitleUtils.java
new file mode 100644 (file)
index 0000000..eac8094
--- /dev/null
@@ -0,0 +1,136 @@
+/*
+ * Copyright (C) 2016 - 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.jcontacts.model.utils;
+
+import java.io.Serializable;
+import java.text.MessageFormat;
+import java.util.Objects;
+import org.mxchange.jcontacts.model.contact.title.PersonalTitle;
+
+/**
+ * Title utilities class
+ * <p>
+ * @author Roland Häder<roland@mxchange.org>
+ */
+public class PersonalTitleUtils implements Serializable {
+
+       /**
+        * Serial number
+        */
+       private static final long serialVersionUID = 185_683_479_107L;
+
+       /**
+        * Cache for valid chars
+        */
+       private static char[] validChars;
+
+       /**
+        * Compares two PersonalTitle instances
+        * <p>
+        * @param personalTitle1 PersonalTitle instance 1
+        * @param personalTitle2 PersonalTitle instance 2
+        * <p>
+        * @return Comparison value
+        */
+       public static int compare (final PersonalTitle personalTitle1, final PersonalTitle personalTitle2) {
+               // Check conditions
+               if (Objects.equals(personalTitle1, personalTitle2)) {
+                       // Same instance
+                       return 0;
+               } else if (null == personalTitle1) {
+                       // Left side null
+                       return -1;
+               } else if (null == personalTitle2) {
+                       // Right side null
+                       return 1;
+               }
+
+               // Compare
+               return personalTitle1.compareTo(personalTitle2);
+       }
+
+       /**
+        * Getter for personal title enumeration from given character
+        * <p>
+        * @param c PersonalTitle character
+        * <p>
+        * @return PersonalTitle enumeration
+        */
+       public static PersonalTitle getPersonalTitleFromChar (final char c) {
+               // Init variable
+               PersonalTitle found = null;
+
+               // Loop through all
+               for (final PersonalTitle title : PersonalTitle.values()) {
+                       // Does the char match?
+                       if (c == title.getAccessChar()) {
+                               // Found it
+                               found = title;
+                               break;
+                       }
+               }
+
+               // Still null?
+               if (null == found) {
+                       // Didn't found a valid one
+                       throw new IllegalArgumentException(MessageFormat.format("Gender {0} is invalid.", c)); //NOI18N
+               }
+
+               // Return it
+               return found;
+       }
+
+       /**
+        * Valid chars (mostly for console client)
+        * <p>
+        * @return Valid chars
+        */
+       @SuppressWarnings ("ReturnOfCollectionOrArrayField")
+       public static char[] validChars () {
+               // Is cache set?
+               if (validChars != null) {
+                       // Return it
+                       return validChars;
+               }
+
+               // Init array, only 2 are valid.
+               char[] valid = new char[2];
+
+               // Get values
+               int i = 0;
+               for (final PersonalTitle title : PersonalTitle.values()) {
+                       // Get access key as this is also the access
+                       valid[i] = title.getAccessChar();
+
+                       // Increment index
+                       i++;
+               }
+
+               // Set it here
+               validChars = valid;
+
+               // Return finialized array
+               return valid;
+       }
+
+       /**
+        * Private constructor as this is an utility class
+        */
+       private PersonalTitleUtils () {
+       }
+
+}
diff --git a/test/org/mxchange/jcontacts/model/contact/title/PersonalTitleUtilsTest.java b/test/org/mxchange/jcontacts/model/contact/title/PersonalTitleUtilsTest.java
new file mode 100644 (file)
index 0000000..aca2600
--- /dev/null
@@ -0,0 +1,121 @@
+/*
+ * Copyright (C) 2022 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.jcontacts.model.contact.title;
+
+import org.mxchange.jcontacts.model.utils.PersonalTitleUtils;
+import org.testng.Assert;
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+
+/**
+ * Test cases for PersonalTitleUtilsTest class
+ * <p>
+ * @author Roland Häder<roland@mxchange.org>
+ */
+public class PersonalTitleUtilsTest {
+
+       private static final PersonalTitle PERSONAL_TITLE1 = PersonalTitle.MR;
+
+       private static final PersonalTitle PERSONAL_TITLE2 = PersonalTitle.MRS;
+
+       /**
+        * Default constructor
+        */
+       public PersonalTitleUtilsTest () {
+       }
+
+       @DataProvider (name = "different-personal-title-provider")
+       public PersonalTitle[][] createDifferentPersonalTitles () {
+               // Same instances
+               return new PersonalTitle[][]{
+                       {
+                               PERSONAL_TITLE1,
+                               PERSONAL_TITLE2
+                       }, {
+                               PERSONAL_TITLE2,
+                               PERSONAL_TITLE1
+                       }
+               };
+       }
+
+       @DataProvider (name = "left-null-personal-title-provider")
+       public PersonalTitle[][] createLeftNullPersonalTitles () {
+               // Same instances
+               return new PersonalTitle[][]{
+                       {
+                               null,
+                               PERSONAL_TITLE1
+                       }, {
+                               null,
+                               PERSONAL_TITLE2
+                       }
+               };
+       }
+
+       @DataProvider (name = "right-null-personal-title-provider")
+       public PersonalTitle[][] createRightNullPersonalTitles () {
+               // Same instances
+               return new PersonalTitle[][]{
+                       {
+                               PERSONAL_TITLE1,
+                               null
+                       }, {
+                               PERSONAL_TITLE2,
+                               null
+                       }
+               };
+       }
+
+       @DataProvider (name = "same-personal-title-provider")
+       public PersonalTitle[][] createSamePersonalTitles () {
+               // Same instances
+               return new PersonalTitle[][]{
+                       {
+                               PERSONAL_TITLE1,
+                               PERSONAL_TITLE1
+                       }, {
+                               PERSONAL_TITLE2,
+                               PERSONAL_TITLE2
+                       }
+               };
+       }
+
+       @Test (description = "Tests method PersonalTitleUtils.compare() with different instances", dataProvider = "different-personal-title-provider")
+       public void testDifferentPersonalTitle (final PersonalTitle personalTitle1, final PersonalTitle personalTitle2) {
+               // Should always return not zero
+               Assert.assertNotEquals(PersonalTitleUtils.compare(personalTitle1, personalTitle2), 0);
+       }
+
+       @Test (description = "Tests method PersonalTitleUtils.compare() with left-null instances", dataProvider = "left-null-personal-title-provider")
+       public void testLeftNullPersonalTitle (final PersonalTitle personalTitle1, final PersonalTitle personalTitle2) {
+               // Should never return -1
+               Assert.assertEquals(PersonalTitleUtils.compare(personalTitle1, personalTitle2), -1);
+       }
+
+       @Test (description = "Tests method PersonalTitleUtils.compare() with right-null instances", dataProvider = "right-null-personal-title-provider")
+       public void testRightNullPersonalTitle (final PersonalTitle personalTitle1, final PersonalTitle personalTitle2) {
+               // Should never return 1
+               Assert.assertEquals(PersonalTitleUtils.compare(personalTitle1, personalTitle2), 1);
+       }
+
+       @Test (description = "Tests method PersonalTitleUtils.compare() with same instances", dataProvider = "same-personal-title-provider")
+       public void testSamePersonalTitle (final PersonalTitle personalTitle1, final PersonalTitle personalTitle2) {
+               // Should never return zero
+               Assert.assertEquals(PersonalTitleUtils.compare(personalTitle1, personalTitle2), 0);
+       }
+
+}