KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > databinding > ClassLookupSupport


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  ******************************************************************************/

11
12 package org.eclipse.core.internal.databinding;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Collection JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.HashSet JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Set JavaDoc;
21
22 /**
23  * @since 1.0
24  *
25  */

26 public class ClassLookupSupport {
27     
28     /*
29      * code copied from AdapterManager.java
30      */

31     private static HashMap JavaDoc classSearchOrderLookup;
32
33     /**
34      * For a given class or interface, return an array containing the given type and all its direct and indirect supertypes.
35      * @param type
36      * @return an array containing the given type and all its direct and indirect supertypes
37      */

38     public static Class JavaDoc[] getTypeHierarchyFlattened(Class JavaDoc type) {
39         List JavaDoc classes = null;
40         //cache reference to lookup to protect against concurrent flush
41
HashMap JavaDoc lookup = classSearchOrderLookup;
42         if (lookup != null)
43             classes = (List JavaDoc) lookup.get(type);
44         // compute class order only if it hasn't been cached before
45
if (classes == null) {
46             classes = new ArrayList JavaDoc();
47             computeClassOrder(type, classes);
48             if (lookup == null)
49                 classSearchOrderLookup = lookup = new HashMap JavaDoc();
50             lookup.put(type, classes);
51         }
52         return (Class JavaDoc[]) classes.toArray(new Class JavaDoc[classes.size()]);
53     }
54
55     /**
56      * Builds and returns a table of adapters for the given adaptable type.
57      * The table is keyed by adapter class name. The
58      * value is the <b>sole<b> factory that defines that adapter. Note that
59      * if multiple adapters technically define the same property, only the
60      * first found in the search order is considered.
61      *
62      * Note that it is important to maintain a consistent class and interface
63      * lookup order. See the class comment for more details.
64      */

65     private static void computeClassOrder(Class JavaDoc adaptable, Collection JavaDoc classes) {
66         Class JavaDoc clazz = adaptable;
67         Set JavaDoc seen = new HashSet JavaDoc(4);
68         while (clazz != null) {
69             classes.add(clazz);
70             computeInterfaceOrder(clazz.getInterfaces(), classes, seen);
71             clazz = clazz.isInterface() ? Object JavaDoc.class : clazz.getSuperclass();
72         }
73     }
74
75     private static void computeInterfaceOrder(Class JavaDoc[] interfaces, Collection JavaDoc classes, Set JavaDoc seen) {
76         List JavaDoc newInterfaces = new ArrayList JavaDoc(interfaces.length);
77         for (int i = 0; i < interfaces.length; i++) {
78             Class JavaDoc interfac = interfaces[i];
79             if (seen.add(interfac)) {
80                 //note we cannot recurse here without changing the resulting interface order
81
classes.add(interfac);
82                 newInterfaces.add(interfac);
83             }
84         }
85         for (Iterator JavaDoc it = newInterfaces.iterator(); it.hasNext();)
86             computeInterfaceOrder(((Class JavaDoc) it.next()).getInterfaces(), classes, seen);
87     }
88
89
90 }
91
Popular Tags