View Javadoc
1   package org.andromda.utils.beans.comparators;
2   
3   import java.io.Serializable;
4   import java.text.Collator;
5   import java.util.Comparator;
6   
7   /**
8    * Used to sort by Character values
9    *
10   * @author Chad Brandon
11   */
12  class CharacterComparator
13      implements Comparator,
14          Serializable
15  {
16      private static final long serialVersionUID = 34L;
17  
18      private Collator collator = null;
19  
20      /**
21       * Used to sort Character values, both objects are assumed to be assignable
22       * to Character
23       * @param objectA
24       * @param objectB
25       * @return comparison result
26       */
27      public int compare(
28          Object objectA,
29          Object objectB)
30      {
31          Character aAsCharacter = (Character)objectA;
32          Character bAsCharacter = (Character)objectB;
33          this.initializeCollator();
34          return collator.compare(
35              Character.toString(aAsCharacter.charValue()),
36              Character.toString(bAsCharacter.charValue()));
37      }
38  
39      /**
40       * Initializes the Collator
41       */
42      private void initializeCollator()
43      {
44          if (this.collator == null)
45          {
46              this.collator = Collator.getInstance();
47  
48              //set to ignore case
49              collator.setStrength(Collator.PRIMARY);
50          }
51      }
52  }