1    /*
2     * Copyright 2007 :torweg free software group
3     * 
4     * This program is free software: you can redistribute it and/or modify
5     * it under the terms of the GNU General Public License as published by
6     * the Free Software Foundation, either version 3 of the License, or
7     * (at your option) any later version.
8     * 
9     * This program is distributed in the hope that it will be useful,
10    * but WITHOUT ANY WARRANTY; without even the implied warranty of
11    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    * GNU General Public License for more details.
13    * 
14    * You should have received a copy of the GNU General Public License
15    * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16    *
17    */
18   package org.torweg.pulse.util;
19   
20   import java.io.Serializable;
21   import java.util.Comparator;
22   import java.util.Locale;
23   
24   /**
25    * utility methods for working with {@code Locale}s.
26    * 
27    * @author Thomas Weber
28    * @version $Revision: 1405 $
29    */
30   public final class LocaleUtils {
31   
32       /**
33        * hidden constructor.
34        */
35       private LocaleUtils() {
36           super();
37       }
38   
39       /**
40        * builds a {@code Locale} from the {@code String} returned by
41        * {@link Locale#toString()}.
42        * 
43        * @param l
44        *            the locale string
45        * @return the {@code Locale} or {@code null}, if no
46        *         {@code Locale} could be built from the given
47        *         {@code String}
48        */
49       public static Locale localeFromString(final String l) {
50           String[] parts = l.split("_");
51           if (parts.length == 1) {
52               return new Locale(parts[0]);
53           } else if (parts.length == 2) {
54               return new Locale(parts[0], parts[1]);
55           } else if (parts.length == 3) {
56               return new Locale(parts[0], parts[1], parts[2]);
57           }
58           return null;
59       }
60   
61       /**
62        * matches the source {@code Locale} to the target {@code Locale}.
63        * <p>
64        * The source matches the target, if it is a subset of the target's
65        * attributes.
66        * </p>
67        * 
68        * @param source
69        *            the locale to check
70        * @param target
71        *            the locale to check against.
72        * @return {@code true}, if the locale matches the given language,
73        *         country and variant
74        */
75       public static boolean matches(final Locale source, final Locale target) {
76           String language = target.getLanguage();
77           String country = target.getCountry();
78           String variant = target.getVariant();
79           if (language.equalsIgnoreCase(source.getLanguage())) {
80               if (source.getCountry().equals("")) {
81                   return true;
82               } else if ((country.equals(""))
83                       || (!country.equalsIgnoreCase(source.getCountry()))) {
84                   return false;
85               }
86               return checkVariant(source, variant);
87           }
88           return false;
89       }
90   
91       /**
92        * checks the variant.
93        * 
94        * @param source
95        *            the source
96        * @param variant
97        *            the variant
98        * @return {@code true}, if the variant matches the variant of the
99        *         locale
100       */
101      private static boolean checkVariant(final Locale source,
102              final String variant) {
103          if (source.getVariant().equals("")) {
104              return true;
105          } else if ((variant.equals(""))
106                  || (!variant.equalsIgnoreCase(source.getVariant()))) {
107              return false;
108          }
109          return true;
110      }
111  
112      /**
113       * Comparator comparing locales by their String representations.
114       * 
115       * @author Thomas Weber
116       */
117      public static final class LocaleComparator implements Comparator<Locale>,
118              Serializable {
119  
120          /**
121           * serialVersionUID.
122           */
123          private static final long serialVersionUID = 981828887110324397L;
124  
125          /**
126           * compares the given locales by their String representations.
127           * 
128           * @param arg0
129           *            the first locale in the comparison
130           * @param arg1
131           *            the second locale in the comparison
132           * @return {@code 0} (zero) if the strings are equal; compareTo
133           *         returns {@code 0} (zero) exactly when the
134           *         {@link Object#equals(Object)} method would return
135           *         {@code true}.
136           */
137          public int compare(final Locale arg0, final Locale arg1) {
138              return arg0.toString().compareTo(arg1.toString());
139          }
140  
141      }
142  }
143