]> git.mxchange.org Git - jcore.git/blob - src/org/mxchange/jcore/contact/Gender.java
deaa4f3deff89a56276297b89afd73cd7d98d21f
[jcore.git] / src / org / mxchange / jcore / contact / 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.jcore.contact;
18
19 import java.text.MessageFormat;
20 import org.mxchange.jcore.BaseFrameworkSystem;
21
22 /**
23  * Gender enum
24  *
25  * @author Roland Haeder
26  */
27 public enum Gender {
28         /**
29          * Unknown enum
30          */
31         UNKNOWN('U', "BaseContact.gender.unknown.text"), //NOI18N
32
33         /**
34          * Male enum
35          */
36         MALE('M', "BaseContact.gender.male.text"), //NOI18N
37
38         /**
39          * Female enum
40          */
41         FEMALE('F', "BaseContact.gender.female.text"), //NOI18N
42
43         /**
44          * Company enum
45          */
46         COMPANY('C', "BaseContact.gender.company.text"); //NOI18N
47
48         /**
49          * Cache for valid chars
50          */
51         private static char[] validChars;
52
53         /**
54          * Access key being entered by ConsoleClient
55          */
56         private final char accessChar;
57
58         /**
59          * Output value (for messages)
60          */
61         private final String messageKey;
62
63         /**
64          * Getter for Gender enum from given character
65          *
66          * @param c Gender character
67          * @return Gender enum
68          */
69         public static Gender fromChar (final char c) {
70                 // Init variable
71                 Gender g = null;
72
73                 // Loop through all
74                 for (final Gender gender : Gender.values()) {
75                         // Does the char match?
76                         if (c == gender.getAccessChar()) {
77                                 // Found it
78                                 g = gender;
79                                 break;
80                         }
81                 }
82
83                 // Still null?
84                 if (null == g) {
85                         // Didn't found a valid one
86                         throw new IllegalArgumentException(MessageFormat.format("Gender {0} is invalid.", c)); //NOI18N
87                 }
88
89                 // Return it
90                 //* NOISY-DEBUG: */ System.out.println("gender=" + g.getClass().getName());
91                 return g;
92         }
93
94         /**
95          * Valid chars
96          *
97          * @return Valid chars
98          */
99         public static char[] validChars () {
100                 // Is cache set?
101                 if (validChars != null) {
102                         // Return it
103                         return validChars;
104                 }
105
106                 // Init array, only 3 are valid as 'U' is UNKNOWN and is not valid.
107                 char[] valid = new char[3];
108
109                 // Get values
110                 int i = 0;
111                 for (Gender gender : values()) {
112                         // Debug message
113                         //* NOISY-DEBUG: */ System.out.println("gender=" + gender);
114
115                         // Is it UNKNOWN?
116                         if (gender.equals(Gender.UNKNOWN)) {
117                                 // Skip this
118                                 continue;
119                         }
120
121                         // Debug message
122                         //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("gender={0} - adding at pos {1} ...", gender, i));
123
124                         // Get access key as this is also the access
125                         valid[i] = gender.getAccessChar();
126
127                         // Increment index
128                         i++;
129                 }
130
131                 // Set it here
132                 validChars = valid;
133
134                 // Return finialized array
135                 return valid;
136         }
137
138         /**
139          * Constructor
140          *
141          * @param accessChar Value being entered by ConsoleClient
142          * @param messageKey Message key for resource file
143          */
144         private Gender (final char accessChar, final String messageKey) {
145                 // Set both
146                 this.accessChar = accessChar;
147                 this.messageKey = messageKey;
148         }
149
150         /**
151          * Acces key (console client mostly)
152          *
153          * @return the accessChar
154          */
155         public char getAccessChar () {
156                 return this.accessChar;
157         }
158
159         /**
160          * Output value (for messages)
161          *
162          * @return the messageKey
163          */
164         public String getMessageKey () {
165                 return this.messageKey;
166         }
167
168         /**
169          * Overwritten to return human-readable strings
170          *
171          * @return Human-readable strings
172          */
173         @Override
174         public String toString () {
175                 // Get key from bundle and return it
176                 return BaseFrameworkSystem.getInstance().getMessageStringFromKey(this.getMessageKey());
177         }
178 }