Frames | No Frames |
1: /* ======================================================================== 2: * JCommon : a free general purpose class library for the Java(tm) platform 3: * ======================================================================== 4: * 5: * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors. 6: * 7: * Project Info: http://www.jfree.org/jcommon/index.html 8: * 9: * This library is free software; you can redistribute it and/or modify it 10: * under the terms of the GNU Lesser General Public License as published by 11: * the Free Software Foundation; either version 2.1 of the License, or 12: * (at your option) any later version. 13: * 14: * This library is distributed in the hope that it will be useful, but 15: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 16: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 17: * License for more details. 18: * 19: * You should have received a copy of the GNU Lesser General Public 20: * License along with this library; if not, write to the Free Software 21: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 22: * USA. 23: * 24: * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 25: * in the United States and other countries.] 26: * 27: * -------------------- 28: * ClassComparator.java 29: * -------------------- 30: * (C)opyright 2003-2005, by Thomas Morgner and Contributors. 31: * 32: * Original Author: Thomas Morgner (taquera@sherito.org); 33: * Contributor(s): David Gilbert (for Object Refinery Limited); 34: * 35: * $Id: ClassComparator.java,v 1.3 2005/10/18 13:24:19 mungady Exp $ 36: * 37: * Changes 38: * ------- 39: * 02-May-2003 : Initial version 40: * 41: */ 42: 43: package org.jfree.util; 44: 45: import java.io.Serializable; 46: import java.util.Comparator; 47: 48: /** 49: * The class comparator can be used to compare and sort classes and their 50: * superclasses. The comparator is not able to compare classes which have 51: * no relation... 52: * 53: * @author Thomas Morgner 54: */ 55: public class ClassComparator implements Comparator, Serializable { 56: 57: /** For serialization. */ 58: private static final long serialVersionUID = -5225335361837391120L; 59: 60: /** 61: * Defaultconstructor. 62: */ 63: public ClassComparator() { 64: super(); 65: } 66: 67: /** 68: * Compares its two arguments for order. Returns a negative integer, 69: * zero, or a positive integer as the first argument is less than, equal 70: * to, or greater than the second.<p> 71: * <P> 72: * Note: throws ClassCastException if the arguments' types prevent them from 73: * being compared by this Comparator. 74: * And IllegalArgumentException if the classes share no relation. 75: * 76: * The implementor must ensure that <tt>sgn(compare(x, y)) == 77: * -sgn(compare(y, x))</tt> for all <tt>x</tt> and <tt>y</tt>. (This 78: * implies that <tt>compare(x, y)</tt> must throw an exception if and only 79: * if <tt>compare(y, x)</tt> throws an exception.)<p> 80: * 81: * The implementor must also ensure that the relation is transitive: 82: * <tt>((compare(x, y)>0) && (compare(y, z)>0))</tt> implies 83: * <tt>compare(x, z)>0</tt>.<p> 84: * 85: * Finally, the implementer must ensure that <tt>compare(x, y)==0</tt> 86: * implies that <tt>sgn(compare(x, z))==sgn(compare(y, z))</tt> for all 87: * <tt>z</tt>.<p> 88: * 89: * It is generally the case, but <i>not</i> strictly required that 90: * <tt>(compare(x, y)==0) == (x.equals(y))</tt>. Generally speaking, 91: * any comparator that violates this condition should clearly indicate 92: * this fact. The recommended language is "Note: this comparator 93: * imposes orderings that are inconsistent with equals." 94: * 95: * @param o1 the first object to be compared. 96: * @param o2 the second object to be compared. 97: * @return a negative integer, zero, or a positive integer as the 98: * first argument is less than, equal to, or greater than the 99: * 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: }