]> git.mxchange.org Git - jcontacts-core.git/blobdiff - src/org/mxchange/jcontacts/model/utils/PersonalTitleUtils.java
Continued:
[jcontacts-core.git] / src / org / mxchange / jcontacts / model / utils / PersonalTitleUtils.java
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 () {
+       }
+
+}