/* * TreeCollator.java * Copyright (C) 1998 dog * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ package dog.util; import java.text.CollationKey; import java.text.Collator; import java.util.Date; import java.util.Locale; /** * A class for comparing objects in a tree. * * @author dog@dog.net.uk * @version 2.0alpha */ public class TreeCollator extends Collator { protected Collator collator; protected boolean descending = false; /** * Constructs a TreeCollator for the default locale. */ public TreeCollator() { collator = Collator.getInstance(); } /** * Constructs a TreeCollator for the specified locale. */ public TreeCollator(Locale locale) { collator = Collator.getInstance(locale); } /** * Indicates whether this collator returns the opposite of any comparison. * @see #setDescending */ public boolean isDescending() { return descending; } /** * Sets whether this collator returns the opposite of any comparison. * @param descending true if this collator returns the opposite of any comparison, false otherwise * @see #setDescending */ public void setDescending(boolean descending) { this.descending = descending; } /** * Utility method to return the opposite of a comparison if descending is true. */ protected int applyDescending(int comparison) { return (!descending ? comparison : -comparison); } /** * Compares the source string to the target string according to the collation rules for this Collator. * Returns an integer less than, equal to or greater than zero depending on whether the source String * is less than, equal to or greater than the target string. * @param source the source string. * @param target the target string. * @see java.text.CollationKey */ public int compare(String source, String target) { return applyDescending(collator.compare(source, target)); } /** * Compares the source object to the target object according to the collation rules for this Collator. * Returns an integer less than, equal to or greater than zero depending on whether the source object * is less than, equal to or greater than the target object. * Objects that can validly be compared by such means include: