From: Roland Haeder Date: Tue, 11 Aug 2015 06:45:14 +0000 (+0200) Subject: Rewrite of fromChar() X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=7645b14dc201e8e538cfe2d6d00976a0e5949741;p=jcore.git Rewrite of fromChar() Signed-off-by:Roland Häder --- diff --git a/src/org/mxchange/jcore/contact/Gender.java b/src/org/mxchange/jcore/contact/Gender.java index e28734c..2b7e112 100644 --- a/src/org/mxchange/jcore/contact/Gender.java +++ b/src/org/mxchange/jcore/contact/Gender.java @@ -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