1
2
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
16
17
18
19
20
21
22 public final class Language implements Comparable<Language>, IValueObject
23 {
24
25
26
27
28
29
30
31
32 @SuppressWarnings("java:S5867")
33 private static final Pattern LANGUAGE_REGEXP = Pattern.compile("^[a-z]{2}$");
34
35
36
37
38 private final String code;
39
40
41
42
43
44
45
46
47
48 private Language(final String code)
49 {
50 super();
51 Objects.requireNonNull(code, "code");
52 if (code.length() != 2)
53 {
54 throw new IllegalArgumentException("Length is not 2");
55 }
56 if (!Language.LANGUAGE_REGEXP.matcher(code).matches())
57 {
58 throw new IllegalArgumentException("code contains illegal character");
59 }
60 if (!GeneratedISO6391.contains(code))
61 {
62 throw new IllegalArgumentException("Unknown ISO639-1 code: " + code);
63 }
64 this.code = code;
65 }
66
67
68
69
70
71
72
73
74 public static Language of(final String code)
75 {
76
77
78
79
80
81
82
83
84
85
86
87
88
89 return new Language(code);
90 }
91
92
93
94
95
96
97
98 @Override
99 public String stringValue()
100 {
101 return this.code;
102 }
103
104
105
106
107
108
109
110
111 @Override
112 public int hashCode()
113 {
114 return this.code.hashCode();
115 }
116
117
118
119
120
121
122
123
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
143
144
145
146
147
148
149
150
151 @Override
152 public String toString()
153 {
154 final var builder = new StringBuilder();
155 builder.append("Language[code=").append(this.code).append(']');
156 return builder.toString();
157 }
158
159
160
161
162
163
164
165
166
167 @Override
168 public int compareTo(final Language obj)
169 {
170 Objects.requireNonNull(obj, "obj");
171 return this.code.compareTo(obj.code);
172 }
173
174 }