]> git.mxchange.org Git - jcore.git/commitdiff
Rewrite of fromChar()
authorRoland Haeder <roland@mxchange.org>
Tue, 11 Aug 2015 06:45:14 +0000 (08:45 +0200)
committerRoland Haeder <roland@mxchange.org>
Tue, 11 Aug 2015 06:45:14 +0000 (08:45 +0200)
Signed-off-by:Roland Häder <roland@mxchange.org>

src/org/mxchange/jcore/contact/Gender.java

index e28734ce36558a2217f56eae059b269b810e33d8..2b7e11248395d6d90f80368347adb59bbdcce87a 100644 (file)
@@ -16,6 +16,7 @@
  */
 package org.mxchange.jcore.contact;
 
+import java.text.MessageFormat;
 import org.mxchange.jcore.BaseFrameworkSystem;
 
 /**
@@ -50,30 +51,27 @@ public enum Gender {
        /**
         * Getter for Gender enum from given character
         *
-        * @param gender Gender character
+        * @param c Gender character
         * @return Gender enum
         */
-       public static Gender fromChar (final char gender) {
+       public static Gender fromChar (final char c) {
+               // Init variable
                Gender g = null;
-               switch (gender) {
-                       case 'U': // Unknown
-                               g = UNKNOWN;
-                               break;
-
-                       case 'M': // Male
-                               g = MALE;
-                               break;
-
-                       case 'F': // Female
-                               g = FEMALE;
-                               break;
 
-                       case 'C': // Company
-                               g = COMPANY;
+               // Loop through all
+               for (final Gender gender : Gender.values()) {
+                       // Does the char match?
+                       if (c == gender.getAccessChar()) {
+                               // Found it
+                               g = gender;
                                break;
+                       }
+               }
 
-                       default: // Unsupported
-                               throw new IllegalArgumentException("gender " + gender + " is invalid.");
+               // Still null?
+               if (g == null) {
+                       // Didn't found a valid one
+                       throw new IllegalArgumentException(MessageFormat.format("Gender {0} is invalid.", c));
                }
 
                // Return it
@@ -98,12 +96,10 @@ public enum Gender {
 
                // Get values
                int i = 0;
-               for (Object value : values()) {
-                       // First cast
-                       Gender gender = (Gender) value;
-
+               for (Gender gender : values()) {
                        // Debug message
                        //* NOISY-DEBUG: */ System.out.println("gender=" + gender);
+
                        // Is it UNKNOWN?
                        if (gender.equals(Gender.UNKNOWN)) {
                                // Skip this
@@ -112,7 +108,8 @@ public enum Gender {
 
                        // Debug message
                        //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("gender={0} - adding at pos {1} ...", gender, i));
-                       // Get database value as this is also the access
+
+                       // Get access key as this is also the access
                        valid[i] = gender.getAccessChar();
 
                        // Increment index