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