/* * ObjectCollator.java * * The contents of this file are subject to the Mozilla Public * License Version 1.1 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. * * The Original Code is Knife. * * The Initial Developer of the Original Code is dog. * Portions created by dog are * Copyright (C) 1998 dog . All * Rights Reserved. * * Contributor(s): n/a. * * You may retrieve the latest version of this package at the knife home page, * located at http://www.dog.net.uk/knife/ */ 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 0.3 */ public class ObjectCollator extends Collator { protected Collator collator; protected boolean descending = false; /** * Constructs a ObjectCollator for the default locale. */ public ObjectCollator() { collator = Collator.getInstance(); } /** * Constructs a ObjectCollator for the specified locale. */ public ObjectCollator(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: