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