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