]> git.mxchange.org Git - jaddressbook-lib.git/blob - Addressbook/src/org/mxchange/addressbook/contact/Gender.java
Introduced Gender enum which replaces the old char
[jaddressbook-lib.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                 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 (Object value : values()) {
100                         // First cast
101                         Gender gender = (Gender) value;
102
103                         // Debug message
104                         //* NOISY-DEBUG: */ System.out.println("gender=" + gender);
105
106                         // Is it "U"?
107                         if (gender.getDatabaseValue().equals("U")) {
108                                 // Skip this
109                                 continue;
110                         }
111
112                         // Debug message
113                         //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("gender={0} - adding at pos {1} ...", gender, i));
114
115                         // Get database value as this is also the access
116                         valid[i] = gender.getDatabaseValue().charAt(0);
117
118                         // Increment index
119                         i++;
120                 }
121
122                 // Set it here
123                 validChars = valid;
124
125                 // Return finialized array
126                 return valid;
127         }
128
129         /**
130          * Database value
131          */
132         private final String databaseValue;
133
134         /**
135          * Output value (for messages)
136          */
137         private final String messageKey;
138
139         /**
140          * Constructor
141          * 
142          * @param databaseValue Value being stored in database
143          * @param messageKey Message key for resource file
144          */
145         private Gender (final String databaseValue, final String messageKey) {
146                 // Set both
147                 this.databaseValue = databaseValue;
148                 this.messageKey = messageKey;
149         }
150
151         /**
152          * Database value
153          * 
154          * @return the databaseValue
155          */
156         protected String getDatabaseValue () {
157                 return this.databaseValue;
158         }
159
160         /**
161          * Output value (for messages)
162          * 
163          * @return the messageKey
164          */
165         protected String getMessageKey () {
166                 return this.messageKey;
167         }
168 }