/
BaseResource.java
445 lines (393 loc) · 17.1 KB
/
BaseResource.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
package eu.dzim.shared.resource;
import eu.dzim.shared.disposable.Disposable;
import eu.dzim.shared.util.BaseEnumType;
import javafx.beans.Observable;
import javafx.beans.binding.*;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.ReadOnlyObjectProperty;
import javafx.beans.property.ReadOnlyObjectWrapper;
import java.util.*;
/**
* Implementation for the {@link Resource} interface.<br>
* <br>
* Intened to be used as a singleton instance via Guice.
*
* @author bgmf
*/
public abstract class BaseResource implements Resource, Disposable {
private static final String DEFAULT_PACKAGE_NAME = BaseResource.class.getPackage().getName();
private static final String DEFAULT_PROPERTIES_NAME = "strings";
/**
* Parent resource which is also checked for localizations
*/
private Resource parentResource;
/**
* The path, where the property file - containing translated strings - can be found. Be aware, that in the moment that means: inside the
* {@link ClassLoader}s hierarchy. Be also aware, that the {@link ResourceBundle} mechanism doesn't need neither the underscore-language part nor
* the dot-properties file ending to be attached.
*/
private final String bundleName;
/**
* A reference to the current active {@link Locale}. Per default the System locale will be used, but is likly overwritten on the constructor.
*/
private final ReadOnlyObjectWrapper<Locale> locale = new ReadOnlyObjectWrapper<>(this, "locale", Locale.getDefault());
/**
* A reference to the current active language bundle. Per default the bundle for the English locale will be loaded.
*/
private final ReadOnlyObjectWrapper<ResourceBundle> resourceBundle = new ReadOnlyObjectWrapper<>(this, "resourceBundle");
private final Map<Locale, ResourceBundle> localizedResourceBundles = new HashMap<>();
/**
* a container for fast access to <b>unparameterized</b> {@link Binding}s - {@link StringBinding}s with parameters are created on the fly
*/
private final Map<String, StringBinding> keyStringBindings = new HashMap<>();
private final Map<String, BooleanBinding> keyBooleanBindings = new HashMap<>();
private final Map<String, IntegerBinding> keyIntegerBindings = new HashMap<>();
private final Map<String, LongBinding> keyLongBindings = new HashMap<>();
private final Map<String, DoubleBinding> keyDoubleBindings = new HashMap<>();
protected BaseResource(String stringsContainerPackage, String resourceFilePrefix, Locale defaultLocale) {
if (stringsContainerPackage == null || stringsContainerPackage.isEmpty())
throw new IllegalArgumentException("The package with the strings.properties file must not be empty!");
if (resourceFilePrefix == null || resourceFilePrefix.isEmpty())
throw new IllegalArgumentException("The resource file prefix (e.g. 'strings' for 'strings.properties') must not be empty!");
bundleName = String.format("%s.%s", stringsContainerPackage, resourceFilePrefix);
Locale locale = defaultLocale != null ?
new Locale(defaultLocale.getLanguage().toLowerCase()) :
new Locale(Locale.getDefault().getLanguage().toLowerCase());
this.locale.set(locale);
if (this.localizedResourceBundles.get(locale) != null) {
this.resourceBundle.set(this.localizedResourceBundles.get(locale));
} else {
this.resourceBundle.set(ResourceBundle.getBundle(bundleName, locale, getClass().getClassLoader()));
this.localizedResourceBundles.put(locale, this.resourceBundle.get());
}
this.parentResource = null;
}
protected BaseResource(String stringsContainerPackage, Locale defaultLocale) {
this(stringsContainerPackage, DEFAULT_PROPERTIES_NAME, defaultLocale);
}
protected BaseResource(String stringsContainerPackage, String resourceFilePrefix) {
this(stringsContainerPackage, resourceFilePrefix, Locale.ENGLISH);
}
protected BaseResource(String stringsContainerPackage) {
this(stringsContainerPackage, DEFAULT_PROPERTIES_NAME, Locale.ENGLISH);
}
protected BaseResource(Locale defaultLocale) {
this(DEFAULT_PACKAGE_NAME, DEFAULT_PROPERTIES_NAME, defaultLocale);
}
protected BaseResource() {
this(DEFAULT_PACKAGE_NAME, DEFAULT_PROPERTIES_NAME, Locale.ENGLISH);
}
@Override
public synchronized boolean setLanguage(String language) {
Locale next = new Locale(language.toLowerCase());
return setLocale(next);
}
@Override
public void setParentResource(final Resource resource) {
this.parentResource = resource;
}
@Override
public synchronized boolean setLocale(Locale next) {
Locale current = Locale.getDefault();
if (current == null || !current.equals(next)) {
if (localizedResourceBundles.get(next) != null) {
resourceBundle.set(localizedResourceBundles.get(next));
} else {
resourceBundle.set(ResourceBundle.getBundle(bundleName, next, getClass().getClassLoader()));
localizedResourceBundles.put(next, this.resourceBundle.get());
}
locale.set(next);
}
return false;
}
public synchronized ReadOnlyObjectProperty<Locale> localeProperty() {
return locale.getReadOnlyProperty();
}
@Override
public synchronized Locale getLocale() {
return locale.get();
}
@Override
public synchronized ReadOnlyObjectProperty<ResourceBundle> resourceBundleProperty() {
return resourceBundle.getReadOnlyProperty();
}
@Override
public synchronized ResourceBundle getResourceBundle() {
return resourceBundle.get();
}
@Override
public synchronized String getGuaranteedString(String key) {
try {
return resourceBundle.get().getString(key).trim();
} catch (MissingResourceException e) {
if (parentResource != null) {
return parentResource.getGuaranteedString(key).trim();
} else {
return '!' + key + '!';
}
}
}
@Override
public String getGuaranteedString(Locale locale, String key) {
ResourceBundle rb = localizedResourceBundles.get(locale);
if (rb == null) {
rb = ResourceBundle.getBundle(bundleName, locale, getClass().getClassLoader());
localizedResourceBundles.put(locale, rb);
}
if (localizedResourceBundles.get(locale) != null) {
try {
return rb.getString(key).trim();
} catch (MissingResourceException e) {
if (parentResource != null) {
return parentResource.getGuaranteedString(locale, key).trim();
} else {
return '!' + key + '!';
}
}
} else {
return getGuaranteedString(key);
}
}
@Override
public synchronized String getGuaranteedString(String key, Object... args) {
String value;
try {
value = resourceBundle.get().getString(key).trim();
} catch (MissingResourceException e) {
if (parentResource != null) {
value = parentResource.getGuaranteedString(key, args).trim();
} else {
value = '!' + key + '!';
}
}
if (value.startsWith("!") && value.endsWith("!"))
return value;
return String.format(value, args);
}
@Override
public String getGuaranteedString(Locale locale, String key, Object... args) {
ResourceBundle rb = localizedResourceBundles.get(locale);
if (rb == null) {
rb = ResourceBundle.getBundle(bundleName, locale, getClass().getClassLoader());
localizedResourceBundles.put(locale, rb);
}
if (localizedResourceBundles.get(locale) != null) {
String value;
try {
value = rb.getString(key).trim();
} catch (MissingResourceException e) {
if (parentResource != null) {
value = parentResource.getGuaranteedString(locale, key, args).trim();
} else {
value = '!' + key + '!';
}
}
if (value.startsWith("!") && value.endsWith("!"))
return value;
return String.format(value, args);
} else {
return getGuaranteedString(key, args);
}
}
@Override
public synchronized Optional<String> getString(String key) {
try {
return Optional.of(resourceBundle.get().getString(key).trim());
} catch (MissingResourceException e) {
if (parentResource != null) {
return parentResource.getString(key);
} else {
return Optional.empty();
}
}
}
@Override
public synchronized Optional<String> getString(String key, Object... args) {
String value;
try {
value = resourceBundle.get().getString(key).trim();
} catch (MissingResourceException e) {
if (parentResource != null) {
value = parentResource.getString(key, args).orElse('!' + key + '!');
} else {
value = '!' + key + '!';
}
}
if (value.startsWith("!") && value.endsWith("!"))
return Optional.empty();
return Optional.of(String.format(value, args));
}
@Override
public synchronized Optional<Boolean> getBoolean(String key) {
try {
return Optional.of(Boolean.parseBoolean(resourceBundle.get().getString(key).trim()));
} catch (MissingResourceException e) {
if (parentResource != null) {
return parentResource.getBoolean(key);
} else {
return Optional.empty();
}
}
}
@Override
public synchronized Boolean getBoolean(String key, Boolean defaultValue) {
return getBoolean(key).orElse(defaultValue);
}
@Override
public synchronized Optional<Integer> getInteger(String key) {
try {
return Optional.of(Integer.parseInt(resourceBundle.get().getString(key).trim()));
} catch (MissingResourceException e) {
if (parentResource != null) {
return parentResource.getInteger(key);
} else {
return Optional.empty();
}
} catch (NumberFormatException e) {
return Optional.empty();
}
}
@Override
public synchronized Integer getInteger(String key, Integer defaultValue) {
return getInteger(key).orElse(defaultValue);
}
@Override
public synchronized Optional<Long> getLong(String key) {
try {
return Optional.of(Long.parseLong(resourceBundle.get().getString(key).trim()));
} catch (MissingResourceException e) {
if (parentResource != null) {
return parentResource.getLong(key);
} else {
return Optional.empty();
}
} catch (NumberFormatException e) {
return Optional.empty();
}
}
@Override
public synchronized Long getLong(String key, Long defaultValue) {
return getLong(key).orElse(defaultValue);
}
@Override
public synchronized Optional<Double> getDouble(String key) {
try {
return Optional.of(Double.parseDouble(resourceBundle.get().getString(key).trim()));
} catch (MissingResourceException e) {
if (parentResource != null) {
return parentResource.getDouble(key);
} else {
return Optional.empty();
}
} catch (NumberFormatException e) {
return Optional.empty();
}
}
@Override
public synchronized Double getDouble(String key, Double defaultValue) {
return getDouble(key).orElse(defaultValue);
}
@Override
public Binding<String> getBinding(String key, Object... parameter) {
return createStringBinding(key, null, parameter);
}
@SuppressWarnings("unchecked")
@Override
public <T> Binding<T> getBinding(String key, T defaultValue, Object... parameter) {
if (defaultValue == null)
return (Binding<T>) getBinding(key, parameter);
if (defaultValue instanceof String) {
return (Binding<T>) createStringBinding(key, (String) defaultValue, parameter);
} else if (defaultValue instanceof Boolean) {
return (Binding<T>) createBooleanBinding(key, (Boolean) defaultValue);
} else if (defaultValue instanceof Long) {
return (Binding<T>) createLongBinding(key, (Long) defaultValue);
} else if (defaultValue instanceof Integer) {
return (Binding<T>) createIntegerBinding(key, (Integer) defaultValue);
} else if (defaultValue instanceof Double) {
return (Binding<T>) createDoubleBinding(key, (Double) defaultValue);
}
throw new IllegalArgumentException("Unknown type to create a binding for: " + defaultValue.getClass().getName());
}
@Override
public Binding<String> getBinding(ObjectProperty<? extends BaseEnumType> type, Object... parameter) {
return createStringBindingForBaseEnumType(type, null, null, parameter);
}
@Override
public Binding<String> getBinding(ObjectProperty<? extends BaseEnumType> type, List<Observable> dependencies, Object... parameter) {
return createStringBindingForBaseEnumType(type, null, dependencies, parameter);
}
@Override
public Binding<String> getBinding(ObjectProperty<? extends BaseEnumType> type, String defaultValue, List<Observable> dependencies,
Object... parameter) {
return createStringBindingForBaseEnumType(type, defaultValue, dependencies, parameter);
}
private StringBinding createStringBinding(final String key, final String defaultValue, final Object... parameter) {
if (parameter == null || parameter.length == 0) {
StringBinding binding = keyStringBindings.get(key);
if (binding != null)
return binding;
binding = Bindings.createStringBinding(() -> defaultValue == null ? getGuaranteedString(key).trim() : getString(key).orElse(defaultValue),
resourceBundle);
keyStringBindings.put(key, binding);
return binding;
} else {
return Bindings.createStringBinding(() -> defaultValue == null ?
getGuaranteedString(key, parameter).trim() :
String.format(getLocale(), getString(key).orElse(defaultValue), parameter), resourceBundle);
}
}
private BooleanBinding createBooleanBinding(final String key, final Boolean defaultValue) {
BooleanBinding binding = keyBooleanBindings.get(key);
if (binding != null)
return binding;
binding = Bindings.createBooleanBinding(() -> getBoolean(key, defaultValue), resourceBundle);
keyBooleanBindings.put(key, binding);
return binding;
}
private IntegerBinding createIntegerBinding(final String key, final Integer defaultValue) {
IntegerBinding binding = keyIntegerBindings.get(key);
if (binding != null)
return binding;
binding = Bindings.createIntegerBinding(() -> getInteger(key, defaultValue), resourceBundle);
keyIntegerBindings.put(key, binding);
return binding;
}
private LongBinding createLongBinding(final String key, final Long defaultValue) {
LongBinding binding = keyLongBindings.get(key);
if (binding != null)
return binding;
binding = Bindings.createLongBinding(() -> getLong(key, defaultValue), resourceBundle);
keyLongBindings.put(key, binding);
return binding;
}
private DoubleBinding createDoubleBinding(final String key, final Double defaultValue) {
DoubleBinding binding = keyDoubleBindings.get(key);
if (binding != null)
return binding;
binding = Bindings.createDoubleBinding(() -> getDouble(key, defaultValue), resourceBundle);
keyDoubleBindings.put(key, binding);
return binding;
}
private StringBinding createStringBindingForBaseEnumType(final ObjectProperty<? extends BaseEnumType> type, final String defaultValue,
final List<Observable> dependencies, final Object... parameter) {
final List<Observable> finalDependencies = new ArrayList<>(Arrays.asList(type, resourceBundle));
if (dependencies != null && !dependencies.isEmpty())
finalDependencies.addAll(dependencies);
// @formatter:off
return Bindings.createStringBinding(
() -> defaultValue == null
? getGuaranteedString(type.get().getKey()).trim()
: String.format(getLocale(), getString(type.get().getKey()).orElse(defaultValue), parameter),
finalDependencies.toArray(new Observable[0]));
// @formatter:on
}
@Override
public void dispose() {
keyStringBindings.clear();
keyBooleanBindings.clear();
keyIntegerBindings.clear();
keyLongBindings.clear();
keyDoubleBindings.clear();
}
}