1    /*
2     * Copyright 2009 :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.text.Collator;
21   import java.util.Comparator;
22   import java.util.Locale;
23   
24   /**
25    * a flexible {@code Comparator} for {@code INamed}, internally using
26    * a {@code Collator}.
27    * 
28    * @see Collator
29    * @author Thomas Weber
30    * @version $Revision: 1405 $
31    */
32   public class NamedComparator implements Comparator<INamed> {
33   
34       /**
35        * the locale used in the comparison.
36        */
37       private final Collator collator;
38   
39       /**
40        * creates a {@code NamedComparator} in the default locale with
41        * secondary strength ({@link Collator#SECONDARY}) and canonical
42        * decomposition ({@link Collator#CANONICAL_DECOMPOSITION}).
43        */
44       public NamedComparator() {
45           super();
46           this.collator = Collator.getInstance();
47           configureCollator();
48       }
49   
50       /**
51        * creates a {@code NamedComparator} in the given locale with secondary
52        * strength ({@link Collator#SECONDARY}) and canonical decomposition (
53        * {@link Collator#CANONICAL_DECOMPOSITION}).
54        * 
55        * @param locale
56        *            the locale for the {@code Collator}
57        */
58       public NamedComparator(final Locale locale) {
59           super();
60           this.collator = Collator.getInstance(locale);
61           configureCollator();
62       }
63   
64       /**
65        * creates a {@code NamedComparator} in the given locale, strength and
66        * decomposition mode.
67        * 
68        * @param locale
69        *            the locale for the {@code Collator}
70        * @param strength
71        *            the strength for the {@code Collator}
72        * @param decompositionMode
73        *            the decomposition mode for the {@code Collator}
74        */
75       public NamedComparator(final Locale locale, final int strength,
76               final int decompositionMode) {
77           super();
78           this.collator = Collator.getInstance(locale);
79           this.collator.setStrength(strength);
80           this.collator.setDecomposition(decompositionMode);
81       }
82   
83       /**
84        * sets the default values for the {@code Collator}.
85        */
86       private void configureCollator() {
87           this.collator.setStrength(Collator.SECONDARY);
88           this.collator.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
89       }
90   
91       /**
92        * compares the two named objects.
93        * 
94        * @param o1
95        *            the first object to be compared.
96        * @param o2
97        *            the second object to be compared.
98        * @return a negative integer, zero, or a positive integer as the first
99        *         argument is less than, equal to, or greater than the second
100       * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
101       */
102      public final int compare(final INamed o1, final INamed o2) {
103          return this.collator.compare(o1.getName(), o2.getName());
104      }
105  }
106