KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > util > lookup > SimpleLookup


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.openide.util.lookup;
20
21 import org.openide.util.Lookup;
22 import org.openide.util.LookupListener;
23
24 import java.util.*;
25
26
27 /**
28  * Simple lookup implementation. It can be used to create temporary lookups
29  * that do not change over time. The result stores references to all objects
30  * passed in the constructor. Those objecst are the only ones returned as
31  * result.
32  * @author David Strupl
33  */

34 class SimpleLookup extends org.openide.util.Lookup {
35     /** This variable is initialized in constructor and thus null
36      * value is not allowed as its value. */

37     private Collection<Item<?>> allItems;
38
39     /**
40      * Creates new Result object with supplied instances parameter.
41      * @param instances to be used to return from the lookup
42      */

43     SimpleLookup(Collection<Object JavaDoc> instances) {
44         allItems = new ArrayList<Item<?>>(instances.size());
45
46         for (Iterator i = instances.iterator(); i.hasNext();) {
47             allItems.add(new InstanceContent.SimpleItem<Object JavaDoc>(i.next()));
48         }
49     }
50
51     <T,R> SimpleLookup(Collection<T> keys, InstanceContent.Convertor<? super T,R> conv) {
52         allItems = new ArrayList<Item<?>>(keys.size());
53
54         for (T item : keys) {
55             allItems.add(new InstanceContent.ConvertingItem<T,R>(item, conv));
56         }
57     }
58
59     public String JavaDoc toString() {
60         return "SimpleLookup" + lookup(new Template<Object JavaDoc>(Object JavaDoc.class)).allInstances();
61     }
62
63     public <T> Result<T> lookup(Template<T> template) {
64         if (template == null) {
65             throw new NullPointerException JavaDoc();
66         }
67
68         return new SimpleResult<T>(template);
69     }
70
71     public <T> T lookup(Class JavaDoc<T> clazz) {
72         for (Iterator i = allItems.iterator(); i.hasNext();) {
73             Object JavaDoc o = i.next();
74
75             if (o instanceof AbstractLookup.Pair) {
76                 AbstractLookup.Pair<?> p = (AbstractLookup.Pair<?>)o;
77                 if (p.instanceOf(clazz)) {
78                     Object JavaDoc ret = p.getInstance();
79                     if (clazz.isInstance(ret)) {
80                         return clazz.cast(ret);
81                     }
82                 }
83             }
84         }
85         return null;
86     }
87
88     /** A method that defines matching between Item and Template.
89      * @param item the item to match
90      * @return true if item matches the template requirements, false if not
91      */

92     private static boolean matches(Template<?> t, AbstractLookup.Pair<?> item) {
93         if (!AbstractLookup.matches(t, item, true)) {
94             return false;
95         }
96
97         Class JavaDoc<?> type = t.getType();
98
99         if ((type != null) && !type.isAssignableFrom(item.getType())) {
100             return false;
101         }
102
103         return true;
104     }
105
106     /**
107      * Result used in SimpleLookup. It holds a reference to the collection
108      * passed in constructor. As the contents of this lookup result never
109      * changes the addLookupListener and removeLookupListener are empty.
110      */

111     private class SimpleResult<T> extends Lookup.Result<T> {
112         /** can be null and is initialized lazily */
113         private Set<Class JavaDoc<? extends T>> classes;
114
115         /** can be null and is initialized lazily */
116         private Collection<? extends Item<T>> items;
117
118         /** Template used for this result. It is never null.*/
119         private Template<T> template;
120
121         /** can be null and is initialized lazily */
122         private Collection<T> results;
123
124         /** Just remembers the supplied argument in variable template.*/
125         SimpleResult(Template<T> template) {
126             this.template = template;
127         }
128
129         /**
130          * Intentionally does nothing because the lookup does not change
131          * and no notification is needed.
132          */

133         public void addLookupListener(LookupListener l) {
134         }
135
136         /**
137          * Intentionally does nothing because the lookup does not change
138          * and no notification is needed.
139          */

140         public void removeLookupListener(LookupListener l) {
141         }
142
143         /**
144          * Lazy initializes the results collection. Uses a call to allItems
145          * to obtain the instances.
146          */

147         public java.util.Collection JavaDoc<? extends T> allInstances() {
148             synchronized (this) {
149                 if (results != null) {
150                     return results;
151                 }
152             }
153
154
155             Collection<T> res = new ArrayList<T>(allItems.size());
156
157             for (Item<T> item : allItems()) {
158                 res.add(item.getInstance());
159             }
160
161             synchronized (this) {
162                 results = Collections.unmodifiableCollection(res);
163             }
164
165             return results;
166         }
167
168         /**
169          * Lazy initializes variable classes. Uses a call to allItems to
170          * compute the result.
171          */

172         public Set<Class JavaDoc<? extends T>> allClasses() {
173             synchronized (this) {
174                 if (classes != null) {
175                     return classes;
176                 }
177             }
178
179             Set<Class JavaDoc<? extends T>> res = new HashSet<Class JavaDoc<? extends T>>();
180
181             for (Item<T> item : allItems()) {
182                 res.add(item.getType());
183             }
184
185             synchronized (this) {
186                 classes = Collections.unmodifiableSet(res);
187             }
188
189             return classes;
190         }
191
192         /**
193          * Lazy initializes variable items. Creates an item for each
194          * element in the instances collection. It puts either SimpleItem
195          * or ConvertingItem to the collection.
196          */

197         public Collection<? extends Item<T>> allItems() {
198             synchronized (this) {
199                 if (items != null) {
200                     return items;
201                 }
202             }
203
204             Collection<Item<T>> res = new ArrayList<Item<T>>(allItems.size());
205
206             for (Iterator<Item<?>> i = allItems.iterator(); i.hasNext();) {
207                 Item<?> o = i.next();
208
209                 if (o instanceof AbstractLookup.Pair) {
210                     if (matches(template, (AbstractLookup.Pair) o)) {
211                         res.add(cast(o));
212                     }
213                 }
214             }
215
216             synchronized (this) {
217                 items = Collections.unmodifiableCollection(res);
218             }
219
220             return items;
221         }
222
223         @SuppressWarnings JavaDoc("unchecked")
224         private Item<T> cast(Item<?> i) {
225             return (Item<T>)i;
226         }
227     }
228 }
229
Popular Tags