]> git.mxchange.org Git - jcontacts-core.git/blob - src/org/mxchange/jcontacts/model/utils/GenderUtils.java
db3b339a5560d5625f30b26db98db6c8f287a724
[jcontacts-core.git] / src / org / mxchange / jcontacts / model / utils / GenderUtils.java
1 /*
2  * Copyright (C) 2016 - 2022 Free Software Foundation
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.jcontacts.model.utils;
18
19 import java.io.Serializable;
20 import java.text.MessageFormat;
21 import org.mxchange.jcontacts.model.contact.gender.Gender;
22
23 /**
24  * Gender utilities class
25  * <p>
26  * @author Roland Häder<roland@mxchange.org>
27  */
28 public class GenderUtils implements Serializable {
29
30         /**
31          * Serial number
32          */
33         private static final long serialVersionUID = 185_683_479_107L;
34
35         /**
36          * Cache for valid chars
37          */
38         private static char[] validChars;
39
40         /**
41          * Getter for Gender enumeration from given character
42          * <p>
43          * @param c Gender character
44          * <p>
45          * @return Gender enumeration
46          */
47         public static Gender fromChar (final char c) {
48                 // Init variable
49                 Gender found = null;
50
51                 // Loop through all
52                 for (final Gender gender : Gender.values()) {
53                         // Does the char match?
54                         if (c == gender.getAccessChar()) {
55                                 // Found it
56                                 found = gender;
57                                 break;
58                         }
59                 }
60
61                 // Still null?
62                 if (null == found) {
63                         // Didn't found a valid one
64                         throw new IllegalArgumentException(MessageFormat.format("Gender {0} is invalid.", c)); //NOI18N
65                 }
66
67                 // Return it
68                 return found;
69         }
70
71         /**
72          * Valid chars (mostly for console client)
73          * <p>
74          * @return Valid chars
75          */
76         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
77         public static char[] validChars () {
78                 // Is cache set?
79                 if (validChars != null) {
80                         // Return it
81                         return validChars;
82                 }
83
84                 // Init array, only 2 are valid.
85                 char[] valid = new char[2];
86
87                 // Get values
88                 int i = 0;
89                 for (final Gender gender : Gender.values()) {
90                         // Get access key as this is also the access
91                         valid[i] = gender.getAccessChar();
92
93                         // Increment index
94                         i++;
95                 }
96
97                 // Set it here
98                 validChars = valid;
99
100                 // Return finialized array
101                 return valid;
102         }
103
104         /**
105          * Private constructor as this is an utility class
106          */
107         private GenderUtils () {
108         }
109
110 }