001package org.andromda.utils.beans.comparators;
002
003import java.io.Serializable;
004import java.text.Collator;
005import java.util.Comparator;
006
007/**
008 * Used to sort by Character values
009 *
010 * @author Chad Brandon
011 */
012class CharacterComparator
013    implements Comparator,
014        Serializable
015{
016    private static final long serialVersionUID = 34L;
017
018    private Collator collator = null;
019
020    /**
021     * Used to sort Character values, both objects are assumed to be assignable
022     * to Character
023     * @param objectA
024     * @param objectB
025     * @return comparison result
026     */
027    public int compare(
028        Object objectA,
029        Object objectB)
030    {
031        Character aAsCharacter = (Character)objectA;
032        Character bAsCharacter = (Character)objectB;
033        this.initializeCollator();
034        return collator.compare(
035            Character.toString(aAsCharacter.charValue()),
036            Character.toString(bAsCharacter.charValue()));
037    }
038
039    /**
040     * Initializes the Collator
041     */
042    private void initializeCollator()
043    {
044        if (this.collator == null)
045        {
046            this.collator = Collator.getInstance();
047
048            //set to ignore case
049            collator.setStrength(Collator.PRIMARY);
050        }
051    }
052}