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   * Implements an I18N aware definition base class.
11   *
12   * @author juergen_kellerer, 2011-01-19
13   * @version 1.0
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  	 * Returns a unique name identifying this definition.
37  	 *
38  	 * @return a unique name identifying this definition.
39  	 */
40  	public abstract String getName();
41  
42  	/**
43  	 * Returns a list of all locales that this definition should be available in.
44  	 *
45  	 * @return a list of all locales that this definition should be available in.
46  	 */
47  	public List<Locale> getAvailableLocales() {
48  		return availableLocales == null ? Collections.<Locale>emptyList() : availableLocales;
49  	}
50  
51  	/**
52  	 * Returns true is this definition is available in the given locale.
53  	 *
54  	 * @param locale the locale to verify.
55  	 * @return true is this definition is available in the given locale.
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  }