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