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