]> git.mxchange.org Git - jfinancials-lib.git/blob - Addressbook/src/org/mxchange/addressbook/contact/Gender.java
Moved a lot classes and interfaces (generalized) to new jcore project + added a few...
[jfinancials-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 import org.mxchange.addressbook.application.AddressbookApplication;
20
21 /**
22  * Gender enum
23  *
24  * @author Roland Haeder
25  */
26 public enum Gender {
27
28         /**
29          * Unknown enum
30          */
31         UNKNOWN("U", "BaseContact.gender.unknown.text"),
32         /**
33          * Male enum
34          */
35         MALE("M", "BaseContact.gender.male.text"),
36         /**
37          * Female enum
38          */
39         FEMALE("F", "BaseContact.gender.female.text"),
40         /**
41          * Company enum
42          */
43         COMPANY("C", "BaseContact.gender.company.text");
44
45         /**
46          * Cache for valid chars
47          */
48         private static char[] validChars;
49
50         /**
51          * Getter for Gender enum from given character
52          *
53          * @param gender Gender character
54          * @return Gender enum
55          */
56         public static Gender fromChar (final char gender) {
57                 Gender g = null;
58                 switch (gender) {
59                         case 'U': // Unknown
60                                 g = UNKNOWN;
61                                 break;
62
63                         case 'M': // Male
64                                 g = MALE;
65                                 break;
66
67                         case 'F': // Female
68                                 g = FEMALE;
69                                 break;
70
71                         case 'C': // Company
72                                 g = COMPANY;
73                                 break;
74
75                         default: // Unsupported
76                                 throw new IllegalArgumentException("gender " + gender + " is invalid.");
77                 }
78
79                 // Return it
80                 //* NOISY-DEBUG: */ System.out.println("gender=" + g.getClass().getName());
81                 return g;
82         }
83
84         /**
85          * Valid chars
86          *
87          * @return Valid chars
88          */
89         public static char[] validChars () {
90                 // Is cache set?
91                 if (validChars != null) {
92                         // Return it
93                         return validChars;
94                 }
95
96                 // Init array
97                 char[] valid = new char[3];
98
99                 // Get values
100                 int i = 0;
101                 for (Object value : values()) {
102                         // First cast
103                         Gender gender = (Gender) value;
104
105                         // Debug message
106                         //* NOISY-DEBUG: */ System.out.println("gender=" + gender);
107                         // Is it UNKNOWN?
108                         if (gender.equals(Gender.UNKNOWN)) {
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                         // 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
169         /**
170          * Overwritten to return human-readable strings
171          *
172          * @return Human-readable strings
173          */
174         @Override
175         public String toString () {
176                 // Get key from bundle and return it
177                 return AddressbookApplication.getInstance().getMessageStringFromKey(this.getMessageKey());
178         }
179 }