001    /*--------------------------------------------------------------------------+
002    $Id: AOptionComparator.java 26268 2010-02-18 10:44:30Z juergens $
003    |                                                                          |
004    | Copyright 2005-2010 Technische Universitaet Muenchen                     |
005    |                                                                          |
006    | Licensed under the Apache License, Version 2.0 (the "License");          |
007    | you may not use this file except in compliance with the License.         |
008    | You may obtain a copy of the License at                                  |
009    |                                                                          |
010    |    http://www.apache.org/licenses/LICENSE-2.0                            |
011    |                                                                          |
012    | Unless required by applicable law or agreed to in writing, software      |
013    | distributed under the License is distributed on an "AS IS" BASIS,        |
014    | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
015    | See the License for the specific language governing permissions and      |
016    | limitations under the License.                                           |
017    +--------------------------------------------------------------------------*/
018    package edu.tum.cs.commons.options;
019    
020    import java.util.Comparator;
021    
022    /**
023     * A comparator for ordering options in a way used for usage messages. Sorting
024     * is performed on the short name and then on the long name (if present). If the
025     * short name is missing, the long name is sorted into the short names.
026     * <p>
027     * Basically we just concatenate the short and long name and compare the
028     * resulting string.
029     * 
030     * @author Benjamin Hummel
031     * @author $Author: juergens $
032     * 
033     * @version $Rev: 26268 $
034     * @levd.rating GREEN Hash: AE24BBB44D7ED7CD6DB789074D7E87C3
035     */
036    public class AOptionComparator implements Comparator<AOption> {
037    
038            /** {@inheritDoc} */
039            public int compare(AOption o1, AOption o2) {
040                    return concatenatedName(o1).compareTo(concatenatedName(o2));
041            }
042    
043            /**
044             * Returns the concatenation of the short and the long name, omitting those
045             * parts missing.
046             * 
047             * @param option
048             *            the option to take the names from.
049             * @return the concatenated names.
050             */
051            private static String concatenatedName(AOption option) {
052                    if (option.shortName() == 0) {
053                            return option.longName();
054                    }
055                    return option.shortName() + option.longName();
056            }
057    }