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