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