001: /* ========================================================================
002: * JCommon : a free general purpose class library for the Java(tm) platform
003: * ========================================================================
004: *
005: * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jcommon/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * --------------------
028: * ClassComparator.java
029: * --------------------
030: * (C)opyright 2003, 2004, by Thomas Morgner and Contributors.
031: *
032: * Original Author: Thomas Morgner (taquera@sherito.org);
033: * Contributor(s): David Gilbert (for Object Refinery Limited);
034: *
035: * $Id: ClassComparator.java,v 1.5 2005/11/08 14:22:04 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 02-May-2003 : Initial version
040: */
041: package org.jfree.xml.factory.objects;
042:
043: import java.io.Serializable;
044: import java.util.Comparator;
045:
046: /**
047: * The class comparator can be used to compare and sort classes and their
048: * superclasses. The comparator is not able to compare classes which have
049: * no relation...
050: *
051: * @author Thomas Morgner
052: * @deprecated Moved to org.jfree.util
053: */
054: public class ClassComparator implements Comparator, Serializable {
055:
056: /**
057: * Defaultconstructor.
058: */
059: public ClassComparator() {
060: super ();
061: }
062:
063: /**
064: * Compares its two arguments for order. Returns a negative integer,
065: * zero, or a positive integer as the first argument is less than, equal
066: * to, or greater than the second.<p>
067: * <P>
068: * Note: throws ClassCastException if the arguments' types prevent them from
069: * being compared by this Comparator.
070: * And IllegalArgumentException if the classes share no relation.
071: *
072: * The implementor must ensure that <tt>sgn(compare(x, y)) ==
073: * -sgn(compare(y, x))</tt> for all <tt>x</tt> and <tt>y</tt>. (This
074: * implies that <tt>compare(x, y)</tt> must throw an exception if and only
075: * if <tt>compare(y, x)</tt> throws an exception.)<p>
076: *
077: * The implementor must also ensure that the relation is transitive:
078: * <tt>((compare(x, y)>0) && (compare(y, z)>0))</tt> implies
079: * <tt>compare(x, z)>0</tt>.<p>
080: *
081: * Finally, the implementer must ensure that <tt>compare(x, y)==0</tt>
082: * implies that <tt>sgn(compare(x, z))==sgn(compare(y, z))</tt> for all
083: * <tt>z</tt>.<p>
084: *
085: * It is generally the case, but <i>not</i> strictly required that
086: * <tt>(compare(x, y)==0) == (x.equals(y))</tt>. Generally speaking,
087: * any comparator that violates this condition should clearly indicate
088: * this fact. The recommended language is "Note: this comparator
089: * imposes orderings that are inconsistent with equals."
090: *
091: * @param o1 the first object to be compared.
092: * @param o2 the second object to be compared.
093: * @return a negative integer, zero, or a positive integer as the
094: * first argument is less than, equal to, or greater than the
095: * second.
096: */
097: public int compare(final Object o1, final Object o2) {
098: final Class c1 = (Class) o1;
099: final Class c2 = (Class) o2;
100: if (c1.equals(o2)) {
101: return 0;
102: }
103: if (c1.isAssignableFrom(c2)) {
104: return -1;
105: } else {
106: if (!c2.isAssignableFrom(c2)) {
107: throw new IllegalArgumentException(
108: "The classes share no relation");
109: }
110: return 1;
111: }
112: }
113:
114: /**
115: * Checks, whether the given classes are comparable. This method will
116: * return true, if one of the classes is assignable from the other class.
117: *
118: * @param c1 the first class to compare
119: * @param c2 the second class to compare
120: * @return true, if the classes share a direct relation, false otherwise.
121: */
122: public boolean isComparable(final Class c1, final Class c2) {
123: return (c1.isAssignableFrom(c2) || c2.isAssignableFrom(c1));
124: }
125: }
|