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-2005, 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.3 2005/10/18 13:24:19 mungady Exp $
036 *
037 * Changes
038 * -------
039 * 02-May-2003 : Initial version
040 * 
041 */
042
043package org.jfree.util;
044
045import java.io.Serializable;
046import java.util.Comparator;
047
048/**
049 * The class comparator can be used to compare and sort classes and their
050 * superclasses. The comparator is not able to compare classes which have
051 * no relation...
052 *
053 * @author Thomas Morgner
054 */
055public class ClassComparator implements Comparator, Serializable {
056
057    /** For serialization. */
058    private static final long serialVersionUID = -5225335361837391120L;
059    
060    /**
061     * Defaultconstructor.
062     */
063    public ClassComparator() {
064        super();
065    }
066
067    /**
068     * Compares its two arguments for order.  Returns a negative integer,
069     * zero, or a positive integer as the first argument is less than, equal
070     * to, or greater than the second.<p>
071     * <P>
072     * Note: throws ClassCastException if the arguments' types prevent them from
073     * being compared by this Comparator.
074     * And IllegalArgumentException if the classes share no relation.
075     *
076     * The implementor must ensure that <tt>sgn(compare(x, y)) ==
077     * -sgn(compare(y, x))</tt> for all <tt>x</tt> and <tt>y</tt>.  (This
078     * implies that <tt>compare(x, y)</tt> must throw an exception if and only
079     * if <tt>compare(y, x)</tt> throws an exception.)<p>
080     *
081     * The implementor must also ensure that the relation is transitive:
082     * <tt>((compare(x, y)&gt;0) &amp;&amp; (compare(y, z)&gt;0))</tt> implies
083     * <tt>compare(x, z)&gt;0</tt>.<p>
084     *
085     * Finally, the implementer must ensure that <tt>compare(x, y)==0</tt>
086     * implies that <tt>sgn(compare(x, z))==sgn(compare(y, z))</tt> for all
087     * <tt>z</tt>.<p>
088     *
089     * It is generally the case, but <i>not</i> strictly required that
090     * <tt>(compare(x, y)==0) == (x.equals(y))</tt>.  Generally speaking,
091     * any comparator that violates this condition should clearly indicate
092     * this fact.  The recommended language is "Note: this comparator
093     * imposes orderings that are inconsistent with equals."
094     *
095     * @param o1 the first object to be compared.
096     * @param o2 the second object to be compared.
097     * @return a negative integer, zero, or a positive integer as the
098     *         first argument is less than, equal to, or greater than the
099     *         second.
100     */
101    public int compare(final Object o1, final Object o2) {
102        final Class c1 = (Class) o1;
103        final Class c2 = (Class) o2;
104        if (c1.equals(o2)) {
105            return 0;
106        }
107        if (c1.isAssignableFrom(c2)) {
108            return -1;
109        }
110        else {
111            if (!c2.isAssignableFrom(c2)) {
112                throw new IllegalArgumentException(
113                    "The classes share no relation"
114                );
115            }
116            return 1;
117        }
118    }
119
120    /**
121     * Checks, whether the given classes are comparable. This method will
122     * return true, if one of the classes is assignable from the other class.
123     *
124     * @param c1 the first class to compare
125     * @param c2 the second class to compare
126     * @return true, if the classes share a direct relation, false otherwise.
127     */
128    public boolean isComparable(final Class c1, final Class c2) {
129        return (c1.isAssignableFrom(c2) || c2.isAssignableFrom(c1));
130    }
131}