]> git.mxchange.org Git - jcontacts-core.git/blob - src/org/mxchange/jcontacts/contact/gender/Gender.java
initial import (was in wrong project, sorry folks)
[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          * Company enum
46          */
47         COMPANY('C', "GENDER_COMPANY"); //NOI18N
48
49         /**
50          * Cache for valid chars
51          */
52         private static char[] validChars;
53
54         /**
55          * Access key being entered by ConsoleClient
56          */
57         private final char accessChar;
58
59         /**
60          * Output value (for messages)
61          */
62         private final String messageKey;
63
64         /**
65          * Getter for Gender enum from given character
66          * <p>
67          * @param c Gender character
68          * @return Gender enum
69          */
70         public static Gender fromChar (final char c) {
71                 // Init variable
72                 Gender g = null;
73
74                 // Loop through all
75                 for (final Gender gender : GenderUtils.selectableGenders()) {
76                         // Does the char match?
77                         if (c == gender.getAccessChar()) {
78                                 // Found it
79                                 g = gender;
80                                 break;
81                         }
82                 }
83
84                 // Still null?
85                 if (null == g) {
86                         // Didn't found a valid one
87                         throw new IllegalArgumentException(MessageFormat.format("Gender {0} is invalid.", c)); //NOI18N
88                 }
89
90                 // Return it
91                 //* NOISY-DEBUG: */ System.out.println("gender=" + g.getClass().getName());
92                 return g;
93         }
94
95         /**
96          * Valid chars (mostly for console client)
97          * <p>
98          * @return Valid chars
99          */
100         public static char[] validChars () {
101                 // Is cache set?
102                 if (validChars != null) {
103                         // Return it
104                         return validChars;
105                 }
106
107                 // Init array, only 3 are valid as 'U' is UNKNOWN and is not valid.
108                 char[] valid = new char[3];
109
110                 // Get values
111                 int i = 0;
112                 for (final Gender gender : GenderUtils.selectableGenders()) {
113                         // Debug message
114                         //* NOISY-DEBUG: */ System.out.println("gender=" + gender);
115
116                         // Debug message
117                         //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("gender={0} - adding at pos {1} ...", gender, i));
118                         // Get access key as this is also the access
119                         valid[i] = gender.getAccessChar();
120
121                         // Increment index
122                         i++;
123                 }
124
125                 // Set it here
126                 validChars = valid;
127
128                 // Return finialized array
129                 return valid;
130         }
131
132         /**
133          * Constructor
134          * <p>
135          * @param accessChar Value being entered by ConsoleClient
136          * @param messageKey Message key for resource file
137          */
138         private Gender (final char accessChar, final String messageKey) {
139                 // Set both
140                 this.accessChar = accessChar;
141                 this.messageKey = messageKey;
142         }
143
144         /**
145          * Acces key (console client mostly)
146          * <p>
147          * @return the accessChar
148          */
149         public char getAccessChar () {
150                 return this.accessChar;
151         }
152
153         /**
154          * Output value (for messages)
155          * <p>
156          * @return the messageKey
157          */
158         public String getMessageKey () {
159                 return this.messageKey;
160         }
161 }