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 String values
009 *
010 * @author Chad Brandon
011 */
012class StringComparator
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 String values, both objects are assumed
022     * to be String instances.
023     * @param objectA
024     * @param objectB
025     * @return compare result
026     */
027    public int compare(
028        final Object objectA,
029        final Object objectB)
030    {
031        this.initializeCollator();
032        return collator.compare(
033            objectA,
034            objectB);
035    }
036
037    /**
038     * Initializes the Collator
039     */
040    private void initializeCollator()
041    {
042        if (this.collator == null)
043        {
044            this.collator = Collator.getInstance();
045
046            //set to ignore case
047            collator.setStrength(Collator.PRIMARY);
048        }
049    }
050}