001    /* JAPI - (Yet another (hopefully) useful) Java API
002     *
003     * Copyright (C) 2004-2006 Christian Hujer
004     *
005     * This program is free software; you can redistribute it and/or
006     * modify it under the terms of the GNU General Public License as
007     * published by the Free Software Foundation; either version 2 of the
008     * License, or (at your option) any later version.
009     *
010     * This program is distributed in the hope that it will be useful, but
011     * WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013     * General Public License for more details.
014     *
015     * You should have received a copy of the GNU General Public License
016     * along with this program; if not, write to the Free Software
017     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
018     * 02111-1307, USA.
019     */
020    
021    package net.sf.japi.swing;
022    
023    import java.text.Collator;
024    import java.util.Comparator;
025    import java.util.Locale;
026    
027    /** Implementation of {@link Comparator} that is able to compare {@link Locale} instances by their names (allowing <code>null</code>).
028     * The Locale for sorting the Locales is determined at creation time.
029     * @author <a href="mailto:chris@riedquat.de">Christian Hujer</a>
030     */
031    public final class LocaleComparator implements Comparator<Locale> {
032    
033        /** Collator for comparing the names. */
034        private final Collator collator = Collator.getInstance();
035    
036        /** Create a LocaleComparator. */
037        public LocaleComparator() {
038        }
039    
040        /** {@inheritDoc} */
041        public int compare(final Locale o1, final Locale o2) {
042            if (o1 == null && o2 == null) {
043                return 0;
044            } else if (o1 == null) {
045                return -1;
046            } else if (o2 == null) {
047                return 1;
048            } else {
049                return collator.compare(o1.getDisplayName(), o2.getDisplayName());
050            }
051        }
052    
053    } // class LocaleComparator