001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package EDU.purdue.cs.bloat.editor;
022:
023: import java.util.*;
024:
025: import EDU.purdue.cs.bloat.util.*;
026:
027: // // For testing only
028: // import EDU.purdue.cs.bloat.file.*;
029: // import EDU.purdue.cs.bloat.context.*;
030:
031: /**
032: * A <tt>TypeComparator</tt> orders <tt>Type</tt>s such that a subclass
033: * preceededs its superclass. Note that this doesn't really work with
034: * interfaces.
035: */
036: public final class TypeComparator implements Comparator {
037:
038: public static boolean DEBUG = false;
039:
040: private EditorContext context;
041:
042: private static void db(final String s) {
043: if (TypeComparator.DEBUG) {
044: System.out.println(s);
045: }
046: }
047:
048: /**
049: * Constructor.
050: */
051: public TypeComparator(final EditorContext context) {
052: this .context = context;
053: }
054:
055: /**
056: * Returns a negative value if o1 < o2 (t1 is a subclass of t2). Otherwise,
057: * it returns a positive value.
058: */
059: public int compare(final Object o1, final Object o2) {
060: Assert.isTrue(o1 instanceof Type, o1 + " is not a Type");
061: Assert.isTrue(o2 instanceof Type, o2 + " is not a Type");
062:
063: final Type t1 = (Type) o1;
064: final Type t2 = (Type) o2;
065:
066: TypeComparator.db("Comparing " + t1 + " to " + t2);
067:
068: final ClassHierarchy hier = context.getHierarchy();
069:
070: if (hier.subclassOf(t1, t2)) {
071: TypeComparator.db(" " + t1 + " is a subclass of " + t2);
072: return (-1);
073:
074: } else if (hier.subclassOf(t2, t1)) {
075: TypeComparator.db(" " + t2 + " is a subclass of " + t1);
076: return (1);
077:
078: } else {
079: TypeComparator.db(" " + t1 + " and " + t2
080: + " are unrelated");
081:
082: // Don't return 0. If you do, the type will not get included in
083: // the sorted set. Weird.
084: return (1);
085: }
086: }
087:
088: /**
089: * Indicates whether some other object is "equal to" this Comparator.
090: */
091: public boolean equals(final Object other) {
092: return (other instanceof TypeComparator);
093: }
094:
095: // /**
096: // * Test program. Reads class names from the command line.
097: // */
098: // public static void main(String[] args) {
099: // // Make a list of class names
100: // List names = new ArrayList();
101: // for(int i = 0; i < args.length; i++) {
102: // names.add(args[i]);
103: // }
104:
105: // // Do some BLOAT magic
106: // EditorContext context =
107: // new CachingBloatContext(new ClassFileLoader(), names, false);
108: // Collection classes = context.getHierarchy().classes();
109:
110: // TypeComparator.DEBUG = true;
111: // SortedSet sorted = new TreeSet(new TypeComparator(context));
112: // sorted.addAll(classes);
113:
114: // System.out.println(classes.size() + " classes");
115: // System.out.println(sorted.size() + " sorted types:");
116: // Object[] array = sorted.toArray();
117: // for(int i = 0; i < array.length; i++) {
118: // System.out.println(" " + array[i]);
119: // }
120: // }
121: }
|