1 package com.trendmicro.grid.acl.ds.trivial;
2
3 import com.trendmicro.grid.acl.commons.LocaleXmlAdapter;
4
5 import javax.xml.bind.annotation.*;
6 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
7 import java.util.*;
8
9
10
11
12
13
14
15 @XmlAccessorType(XmlAccessType.FIELD)
16 @XmlType(namespace = I18NAwareDefinition.NS)
17 public abstract class I18NAwareDefinition {
18
19 public static final String NS = "urn:com.trendmicro.grid.acl.ds.trivial.Categories";
20
21 @XmlElement(name = "locale")
22 @XmlElementWrapper(name = "availableLocales")
23 @XmlJavaTypeAdapter(LocaleXmlAdapter.class)
24 private List<Locale> availableLocales;
25
26 private transient Set<String> availableCountryCodes;
27
28 public I18NAwareDefinition() {
29 }
30
31 protected I18NAwareDefinition(List<Locale> availableLocales) {
32 this.availableLocales = availableLocales;
33 }
34
35
36
37
38
39
40 public abstract String getName();
41
42
43
44
45
46
47 public List<Locale> getAvailableLocales() {
48 return availableLocales == null ? Collections.<Locale>emptyList() : availableLocales;
49 }
50
51
52
53
54
55
56
57 public boolean isAvailableInLocale(Locale locale) {
58 if (availableCountryCodes == null) {
59 Set<String> countries = new HashSet<String>();
60 for (Locale l : getAvailableLocales())
61 countries.add(l.getCountry());
62 availableCountryCodes = countries;
63 }
64
65 final String countryCode = locale.getCountry();
66 return availableCountryCodes.isEmpty() || availableCountryCodes.contains(countryCode);
67 }
68 }