]> git.mxchange.org Git - jcontacts-core.git/blob - src/org/mxchange/jcontacts/contact/gender/Gender.java
Gender.COMPANY removed as this was no real gender anyway
[jcontacts-core.git] / src / org / mxchange / jcontacts / contact / gender / Gender.java
1 /*
2  * Copyright (C) 2015 Roland Haeder
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.jcontacts.contact.gender;
18
19 import java.io.Serializable;
20 import java.text.MessageFormat;
21
22 /**
23  * Gender enum
24  * <p>
25  * @author Roland Haeder<roland@mxchange.org>
26  */
27 public enum Gender implements Serializable {
28
29         /**
30          * Unknown enum
31          */
32         UNKNOWN('U', "GENDER_UNKNOWN"), //NOI18N
33
34         /**
35          * Male enum
36          */
37         MALE('M', "GENDER_MALE"), //NOI18N
38
39         /**
40          * Female enum
41          */
42         FEMALE('F', "GENDER_FEMALE"); //NOI18N
43
44         /**
45          * Cache for valid chars
46          */
47         private static char[] validChars;
48
49         /**
50          * Access key being entered by ConsoleClient
51          */
52         private final char accessChar;
53
54         /**
55          * Output value (for messages)
56          */
57         private final String messageKey;
58
59         /**
60          * Getter for Gender enum from given character
61          * <p>
62          * @param c Gender character
63          * @return Gender enum
64          */
65         public static Gender fromChar (final char c) {
66                 // Init variable
67                 Gender g = null;
68
69                 // Loop through all
70                 for (final Gender gender : GenderUtils.selectableGenders()) {
71                         // Does the char match?
72                         if (c == gender.getAccessChar()) {
73                                 // Found it
74                                 g = gender;
75                                 break;
76                         }
77                 }
78
79                 // Still null?
80                 if (null == g) {
81                         // Didn't found a valid one
82                         throw new IllegalArgumentException(MessageFormat.format("Gender {0} is invalid.", c)); //NOI18N
83                 }
84
85                 // Return it
86                 //* NOISY-DEBUG: */ System.out.println("gender=" + g.getClass().getName());
87                 return g;
88         }
89
90         /**
91          * Valid chars (mostly for console client)
92          * <p>
93          * @return Valid chars
94          */
95         public static char[] validChars () {
96                 // Is cache set?
97                 if (validChars != null) {
98                         // Return it
99                         return validChars;
100                 }
101
102                 // Init array, only 3 are valid as 'U' is UNKNOWN and is not valid.
103                 char[] valid = new char[3];
104
105                 // Get values
106                 int i = 0;
107                 for (final Gender gender : GenderUtils.selectableGenders()) {
108                         // Debug message
109                         //* NOISY-DEBUG: */ System.out.println("gender=" + gender);
110
111                         // Debug message
112                         //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("gender={0} - adding at pos {1} ...", gender, i));
113                         // Get access key as this is also the access
114                         valid[i] = gender.getAccessChar();
115
116                         // Increment index
117                         i++;
118                 }
119
120                 // Set it here
121                 validChars = valid;
122
123                 // Return finialized array
124                 return valid;
125         }
126
127         /**
128          * Constructor
129          * <p>
130          * @param accessChar Value being entered by ConsoleClient
131          * @param messageKey Message key for resource file
132          */
133         private Gender (final char accessChar, final String messageKey) {
134                 // Set both
135                 this.accessChar = accessChar;
136                 this.messageKey = messageKey;
137         }
138
139         /**
140          * Acces key (console client mostly)
141          * <p>
142          * @return the accessChar
143          */
144         public char getAccessChar () {
145                 return this.accessChar;
146         }
147
148         /**
149          * Output value (for messages)
150          * <p>
151          * @return the messageKey
152          */
153         public String getMessageKey () {
154                 return this.messageKey;
155         }
156 }