1 /*
2 * Copyright (C) 2020-2023 Dipl.-Inform. Kai Hofmann. All rights reserved!
3 */
4 package de.powerstat.validation.values;
5
6
7 import java.util.Objects;
8 import java.util.regex.Pattern;
9
10 import de.powerstat.validation.generated.GeneratedISO6391;
11 import de.powerstat.validation.interfaces.IValueObject;
12
13
14 /**
15 * Language ISO 639-1.
16 *
17 * Not DSGVO relevant.
18 *
19 * TODO Languages names in english
20 * TODO Translations
21 */
22 public final class Language implements Comparable<Language>, IValueObject
23 {
24 /* *
25 * Cache for singletons.
26 */
27 // private static final Map<String, Language> CACHE = new WeakHashMap<>();
28
29 /**
30 * Language regexp.
31 */
32 @SuppressWarnings("java:S5867")
33 private static final Pattern LANGUAGE_REGEXP = Pattern.compile("^[a-z]{2}$"); //$NON-NLS-1$
34
35 /**
36 * ISO 639-1 language code.
37 */
38 private final String code;
39
40
41 /**
42 * Constructor.
43 *
44 * @param code ISO 639-1 code
45 * @throws NullPointerException if code is null
46 * @throws IllegalArgumentException if code is not a known 639-1 code
47 */
48 private Language(final String code)
49 {
50 super();
51 Objects.requireNonNull(code, "code"); //$NON-NLS-1$
52 if (code.length() != 2)
53 {
54 throw new IllegalArgumentException("Length is not 2"); //$NON-NLS-1$
55 }
56 if (!Language.LANGUAGE_REGEXP.matcher(code).matches())
57 {
58 throw new IllegalArgumentException("code contains illegal character"); //$NON-NLS-1$
59 }
60 if (!GeneratedISO6391.contains(code))
61 {
62 throw new IllegalArgumentException("Unknown ISO639-1 code: " + code); //$NON-NLS-1$
63 }
64 this.code = code;
65 }
66
67
68 /**
69 * Language factory.
70 *
71 * @param code 639-1 code
72 * @return Language object
73 */
74 public static Language of(final String code)
75 {
76 /*
77 synchronized (Language.class)
78 {
79 Language obj = Language.CACHE.get(code);
80 if (obj != null)
81 {
82 return obj;
83 }
84 obj = new Language(code);
85 Language.CACHE.put(code, obj);
86 return obj;
87 }
88 */
89 return new Language(code);
90 }
91
92
93 /**
94 * Returns the value of this Language as a string.
95 *
96 * @return The text value represented by this object after conversion to type string (ISO 639-1).
97 */
98 @Override
99 public String stringValue()
100 {
101 return this.code;
102 }
103
104
105 /**
106 * Calculate hash code.
107 *
108 * @return Hash
109 * @see java.lang.Object#hashCode()
110 */
111 @Override
112 public int hashCode()
113 {
114 return this.code.hashCode();
115 }
116
117
118 /**
119 * Is equal with another object.
120 *
121 * @param obj Object
122 * @return true when equal, false otherwise
123 * @see java.lang.Object#equals(java.lang.Object)
124 */
125 @Override
126 public boolean equals(final Object obj)
127 {
128 if (this == obj)
129 {
130 return true;
131 }
132 if (!(obj instanceof Language))
133 {
134 return false;
135 }
136 final Language other = (Language)obj;
137 return this.code.equals(other.code);
138 }
139
140
141 /**
142 * Returns the string representation of this Language.
143 *
144 * The exact details of this representation are unspecified and subject to change, but the following may be regarded as typical:
145 *
146 * "Language[code=de]"
147 *
148 * @return String representation of this Language
149 * @see java.lang.Object#toString()
150 */
151 @Override
152 public String toString()
153 {
154 final var builder = new StringBuilder();
155 builder.append("Language[code=").append(this.code).append(']'); //$NON-NLS-1$
156 return builder.toString();
157 }
158
159
160 /**
161 * Compare with another object.
162 *
163 * @param obj Object to compare with
164 * @return 0: equal; 1: greater; -1: smaller
165 * @see java.lang.Comparable#compareTo(java.lang.Object)
166 */
167 @Override
168 public int compareTo(final Language obj)
169 {
170 Objects.requireNonNull(obj, "obj"); //$NON-NLS-1$
171 return this.code.compareTo(obj.code);
172 }
173
174 }