comparison org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/ClassLookupSupport.d @ 78:0a55d2d5a946

Added file for databinding
author Frank Benoit <benoit@tionex.de>
date Tue, 14 Apr 2009 11:35:29 +0200
parents
children 6be48cf9f95c
comparison
equal deleted inserted replaced
76:f05e6e8b2f2d 78:0a55d2d5a946
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 module org.eclipse.core.internal.databinding.ClassLookupSupport;
13
14 import java.lang.all;
15
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.HashMap;
19 import java.util.HashSet;
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.Set;
23
24 /**
25 * @since 1.0
26 *
27 */
28 public class ClassLookupSupport {
29
30 /*
31 * code copied from AdapterManager.java
32 */
33 private static HashMap classSearchOrderLookup;
34
35 /**
36 * For a given class or interface, return an array containing the given type and all its direct and indirect supertypes.
37 * @param type
38 * @return an array containing the given type and all its direct and indirect supertypes
39 */
40 public static ClassInfo[] getTypeHierarchyFlattened(ClassInfo type) {
41 List classes = null;
42 //cache reference to lookup to protect against concurrent flush
43 HashMap lookup = classSearchOrderLookup;
44 if (lookup !is null)
45 classes = cast(List) lookup.get(type);
46 // compute class order only if it hasn't been cached before
47 if (classes is null) {
48 classes = new ArrayList();
49 computeClassOrder(type, classes);
50 if (lookup is null)
51 classSearchOrderLookup = lookup = new HashMap();
52 lookup.put(type, classes);
53 }
54 return cast(ClassInfo[]) classes.toArray(new ClassInfo[classes.size()]);
55 }
56
57 /**
58 * Builds and returns a table of adapters for the given adaptable type.
59 * The table is keyed by adapter class name. The
60 * value is the <b>sole<b> factory that defines that adapter. Note that
61 * if multiple adapters technically define the same property, only the
62 * first found in the search order is considered.
63 *
64 * Note that it is important to maintain a consistent class and interface
65 * lookup order. See the class comment for more details.
66 */
67 private static void computeClassOrder(ClassInfo adaptable, Collection classes) {
68 ClassInfo clazz = adaptable;
69 Set seen = new HashSet(4);
70 while (clazz !is null) {
71 classes.add(clazz);
72 computeInterfaceOrder(clazz.getInterfaces(), classes, seen);
73 clazz = clazz.isInterface() ? Object.classinfo : clazz.getSuperclass();
74 }
75 }
76
77 private static void computeInterfaceOrder(ClassInfo[] interfaces, Collection classes, Set seen) {
78 List newInterfaces = new ArrayList(interfaces.length);
79 for (int i = 0; i < interfaces.length; i++) {
80 ClassInfo interfac = interfaces[i];
81 if (seen.add(interfac)) {
82 //note we cannot recurse here without changing the resulting interface order
83 classes.add(interfac);
84 newInterfaces.add(interfac);
85 }
86 }
87 for (Iterator it = newInterfaces.iterator(); it.hasNext();)
88 computeInterfaceOrder((cast(ClassInfo) it.next()).getInterfaces(), classes, seen);
89 }
90
91
92 }