1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.codec.language.bm;
19
20 import java.io.InputStream;
21 import java.util.Collections;
22 import java.util.EnumMap;
23 import java.util.HashSet;
24 import java.util.Map;
25 import java.util.NoSuchElementException;
26 import java.util.Scanner;
27 import java.util.Set;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 public class Languages {
54
55
56
57
58
59
60
61 public static abstract class LanguageSet {
62
63 public static LanguageSet from(final Set<String> langs) {
64 return langs.isEmpty() ? NO_LANGUAGES : new SomeLanguages(langs);
65 }
66
67 public abstract boolean contains(String language);
68
69 public abstract String getAny();
70
71 public abstract boolean isEmpty();
72
73 public abstract boolean isSingleton();
74
75 public abstract LanguageSet restrictTo(LanguageSet other);
76 }
77
78
79
80
81 public static final class SomeLanguages extends LanguageSet {
82 private final Set<String> languages;
83
84 private SomeLanguages(final Set<String> languages) {
85 this.languages = Collections.unmodifiableSet(languages);
86 }
87
88 @Override
89 public boolean contains(final String language) {
90 return this.languages.contains(language);
91 }
92
93 @Override
94 public String getAny() {
95 return this.languages.iterator().next();
96 }
97
98 public Set<String> getLanguages() {
99 return this.languages;
100 }
101
102 @Override
103 public boolean isEmpty() {
104 return this.languages.isEmpty();
105 }
106
107 @Override
108 public boolean isSingleton() {
109 return this.languages.size() == 1;
110 }
111
112 @Override
113 public LanguageSet restrictTo(final LanguageSet other) {
114 if (other == NO_LANGUAGES) {
115 return other;
116 } else if (other == ANY_LANGUAGE) {
117 return this;
118 } else {
119 final SomeLanguages sl = (SomeLanguages) other;
120 final Set<String> ls = new HashSet<String>(Math.min(languages.size(), sl.languages.size()));
121 for (String lang : languages) {
122 if (sl.languages.contains(lang)) {
123 ls.add(lang);
124 }
125 }
126 return from(ls);
127 }
128 }
129
130 @Override
131 public String toString() {
132 return "Languages(" + languages.toString() + ")";
133 }
134
135 }
136
137 public static final String ANY = "any";
138
139 private static final Map<NameType, Languages> LANGUAGES = new EnumMap<NameType, Languages>(NameType.class);
140
141 static {
142 for (final NameType s : NameType.values()) {
143 LANGUAGES.put(s, getInstance(langResourceName(s)));
144 }
145 }
146
147 public static Languages getInstance(final NameType nameType) {
148 return LANGUAGES.get(nameType);
149 }
150
151 public static Languages getInstance(final String languagesResourceName) {
152
153 final Set<String> ls = new HashSet<String>();
154 final InputStream langIS = Languages.class.getClassLoader().getResourceAsStream(languagesResourceName);
155
156 if (langIS == null) {
157 throw new IllegalArgumentException("Unable to resolve required resource: " + languagesResourceName);
158 }
159
160 final Scanner lsScanner = new Scanner(langIS, ResourceConstants.ENCODING);
161 try {
162 boolean inExtendedComment = false;
163 while (lsScanner.hasNextLine()) {
164 final String line = lsScanner.nextLine().trim();
165 if (inExtendedComment) {
166 if (line.endsWith(ResourceConstants.EXT_CMT_END)) {
167 inExtendedComment = false;
168 }
169 } else {
170 if (line.startsWith(ResourceConstants.EXT_CMT_START)) {
171 inExtendedComment = true;
172 } else if (line.length() > 0) {
173 ls.add(line);
174 }
175 }
176 }
177 } finally {
178 lsScanner.close();
179 }
180
181 return new Languages(Collections.unmodifiableSet(ls));
182 }
183
184 private static String langResourceName(final NameType nameType) {
185 return String.format("org/apache/commons/codec/language/bm/%s_languages.txt", nameType.getName());
186 }
187
188 private final Set<String> languages;
189
190
191
192
193 public static final LanguageSet NO_LANGUAGES = new LanguageSet() {
194 @Override
195 public boolean contains(final String language) {
196 return false;
197 }
198
199 @Override
200 public String getAny() {
201 throw new NoSuchElementException("Can't fetch any language from the empty language set.");
202 }
203
204 @Override
205 public boolean isEmpty() {
206 return true;
207 }
208
209 @Override
210 public boolean isSingleton() {
211 return false;
212 }
213
214 @Override
215 public LanguageSet restrictTo(final LanguageSet other) {
216 return this;
217 }
218
219 @Override
220 public String toString() {
221 return "NO_LANGUAGES";
222 }
223 };
224
225
226
227
228 public static final LanguageSet ANY_LANGUAGE = new LanguageSet() {
229 @Override
230 public boolean contains(final String language) {
231 return true;
232 }
233
234 @Override
235 public String getAny() {
236 throw new NoSuchElementException("Can't fetch any language from the any language set.");
237 }
238
239 @Override
240 public boolean isEmpty() {
241 return false;
242 }
243
244 @Override
245 public boolean isSingleton() {
246 return false;
247 }
248
249 @Override
250 public LanguageSet restrictTo(final LanguageSet other) {
251 return other;
252 }
253
254 @Override
255 public String toString() {
256 return "ANY_LANGUAGE";
257 }
258 };
259
260 private Languages(final Set<String> languages) {
261 this.languages = languages;
262 }
263
264 public Set<String> getLanguages() {
265 return this.languages;
266 }
267 }