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