KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > xml > factory > objects > ClassComparator


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, 2004, 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.5 2005/11/08 14:22:04 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 02-May-2003 : Initial version
40  */

41 package org.jfree.xml.factory.objects;
42
43 import java.io.Serializable JavaDoc;
44 import java.util.Comparator JavaDoc;
45
46 /**
47  * The class comparator can be used to compare and sort classes and their
48  * superclasses. The comparator is not able to compare classes which have
49  * no relation...
50  *
51  * @author Thomas Morgner
52  * @deprecated Moved to org.jfree.util
53  */

54 public class ClassComparator implements Comparator JavaDoc, Serializable JavaDoc {
55
56     /**
57      * Defaultconstructor.
58      */

59     public ClassComparator() {
60         super();
61     }
62
63     /**
64      * Compares its two arguments for order. Returns a negative integer,
65      * zero, or a positive integer as the first argument is less than, equal
66      * to, or greater than the second.<p>
67      * <P>
68      * Note: throws ClassCastException if the arguments' types prevent them from
69      * being compared by this Comparator.
70      * And IllegalArgumentException if the classes share no relation.
71      *
72      * The implementor must ensure that <tt>sgn(compare(x, y)) ==
73      * -sgn(compare(y, x))</tt> for all <tt>x</tt> and <tt>y</tt>. (This
74      * implies that <tt>compare(x, y)</tt> must throw an exception if and only
75      * if <tt>compare(y, x)</tt> throws an exception.)<p>
76      *
77      * The implementor must also ensure that the relation is transitive:
78      * <tt>((compare(x, y)&gt;0) &amp;&amp; (compare(y, z)&gt;0))</tt> implies
79      * <tt>compare(x, z)&gt;0</tt>.<p>
80      *
81      * Finally, the implementer must ensure that <tt>compare(x, y)==0</tt>
82      * implies that <tt>sgn(compare(x, z))==sgn(compare(y, z))</tt> for all
83      * <tt>z</tt>.<p>
84      *
85      * It is generally the case, but <i>not</i> strictly required that
86      * <tt>(compare(x, y)==0) == (x.equals(y))</tt>. Generally speaking,
87      * any comparator that violates this condition should clearly indicate
88      * this fact. The recommended language is "Note: this comparator
89      * imposes orderings that are inconsistent with equals."
90      *
91      * @param o1 the first object to be compared.
92      * @param o2 the second object to be compared.
93      * @return a negative integer, zero, or a positive integer as the
94      * first argument is less than, equal to, or greater than the
95      * second.
96      */

97     public int compare(final Object JavaDoc o1, final Object JavaDoc o2) {
98         final Class JavaDoc c1 = (Class JavaDoc) o1;
99         final Class JavaDoc c2 = (Class JavaDoc) o2;
100         if (c1.equals(o2)) {
101             return 0;
102         }
103         if (c1.isAssignableFrom(c2)) {
104             return -1;
105         }
106         else {
107             if (!c2.isAssignableFrom(c2)) {
108                 throw new IllegalArgumentException JavaDoc("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 JavaDoc c1, final Class JavaDoc c2) {
123         return (c1.isAssignableFrom(c2) || c2.isAssignableFrom(c1));
124     }
125 }
126
Popular Tags