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 */ 041package org.jfree.xml.factory.objects; 042 043import java.io.Serializable; 044import 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 */ 054public 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. 067 * <p>Note: throws ClassCastException if the arguments' types prevent them 068 * from being compared by this Comparator. And IllegalArgumentException if 069 * the classes share no relation.</p> 070 * 071 * The implementor must ensure that <tt>sgn(compare(x, y)) == 072 * -sgn(compare(y, x))</tt> for all <tt>x</tt> and <tt>y</tt>. (This 073 * implies that <tt>compare(x, y)</tt> must throw an exception if and only 074 * if <tt>compare(y, x)</tt> throws an exception.)<p> 075 * 076 * The implementor must also ensure that the relation is transitive: 077 * <tt>((compare(x, y)>0) && (compare(y, z)>0))</tt> implies 078 * <tt>compare(x, z)>0</tt>.<p> 079 * 080 * Finally, the implementer must ensure that <tt>compare(x, y)==0</tt> 081 * implies that <tt>sgn(compare(x, z))==sgn(compare(y, z))</tt> for all 082 * <tt>z</tt>.<p> 083 * 084 * It is generally the case, but <i>not</i> strictly required that 085 * <tt>(compare(x, y)==0) == (x.equals(y))</tt>. Generally speaking, 086 * any comparator that violates this condition should clearly indicate 087 * this fact. The recommended language is "Note: this comparator 088 * imposes orderings that are inconsistent with equals." 089 * 090 * @param o1 the first object to be compared. 091 * @param o2 the second object to be compared. 092 * @return a negative integer, zero, or a positive integer as the 093 * first argument is less than, equal to, or greater than the 094 * second. 095 */ 096 public int compare(final Object o1, final Object o2) { 097 final Class c1 = (Class) o1; 098 final Class c2 = (Class) o2; 099 if (c1.equals(o2)) { 100 return 0; 101 } 102 if (c1.isAssignableFrom(c2)) { 103 return -1; 104 } 105 else { 106 if (!c2.isAssignableFrom(c2)) { 107 throw new IllegalArgumentException("The classes share no relation"); 108 } 109 return 1; 110 } 111 } 112 113 /** 114 * Checks, whether the given classes are comparable. This method will 115 * return true, if one of the classes is assignable from the other class. 116 * 117 * @param c1 the first class to compare 118 * @param c2 the second class to compare 119 * @return true, if the classes share a direct relation, false otherwise. 120 */ 121 public boolean isComparable(final Class c1, final Class c2) { 122 return (c1.isAssignableFrom(c2) || c2.isAssignableFrom(c1)); 123 } 124}