KickJava   Java API By Example, From Geeks To Geeks.

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


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.AbstractLookup;
22 import org.openide.util.lookup.AbstractLookup.Pair;
23
24 import java.lang.ref.WeakReference JavaDoc;
25
26 import java.util.*;
27
28
29 /** A special content implementation that can be passed to AbstractLookup
30  * and provides methods for registration of instances and lazy instances.
31  * <PRE>
32  * InstanceContent ic = new InstanceContent ();
33  * AbstractLookup al = new AbstractLookup (ic);
34  *
35  * ic.add (new Object ());
36  * ic.add (new Dimension (...));
37  *
38  * Dimension theDim = (Dimension)al.lookup (Dimension.class);
39  * </PRE>
40  *
41  * @author Jaroslav Tulach
42  *
43  * @since 1.25
44  */

45 public final class InstanceContent extends AbstractLookup.Content {
46     /**
47      * Create a new, empty content.
48      */

49     public InstanceContent() {
50     }
51
52     /** The method to add instance to the lookup with.
53      * @param inst instance
54      */

55     public final void add(Object JavaDoc inst) {
56         addPair(new SimpleItem<Object JavaDoc>(inst));
57     }
58
59     /** The method to add instance to the lookup with.
60      * @param inst instance
61      * @param conv convertor which postponing an instantiation,
62      * if <code>conv==null</code> then the instance is registered directly.
63      */

64     public final <T,R> void add(T inst, Convertor<T,R> conv) {
65         addPair(new ConvertingItem<T,R>(inst, conv));
66     }
67
68     /** Remove instance.
69      * @param inst instance
70      */

71     public final void remove(Object JavaDoc inst) {
72         removePair(new SimpleItem<Object JavaDoc>(inst));
73     }
74
75     /** Remove instance added with a convertor.
76      * @param inst instance
77      * @param conv convertor, if <code>conv==null</code> it is same like
78      * remove(Object)
79      */

80     public final <T,R> void remove(T inst, Convertor<T,R> conv) {
81         removePair(new ConvertingItem<T,R>(inst, conv));
82     }
83
84     /** Changes all pairs in the lookup to new values. Converts collection of
85      * instances to collection of pairs.
86      * @param col the collection of (Item) objects
87      * @param conv the convertor to use or null
88      */

89     public final <T,R> void set(Collection<T> col, Convertor<T,R> conv) {
90         ArrayList<Pair<?>> l = new ArrayList<Pair<?>>(col.size());
91         Iterator<T> it = col.iterator();
92
93         if (conv == null) {
94             while (it.hasNext()) {
95                 l.add(new SimpleItem<T>(it.next()));
96             }
97         } else {
98             while (it.hasNext()) {
99                 l.add(new ConvertingItem<T,R>(it.next(), conv));
100             }
101         }
102
103         setPairs(l);
104     }
105
106     /** Convertor postpones an instantiation of an object.
107      * @since 1.25
108      */

109     public static interface Convertor<T,R> {
110         /** Convert obj to other object. There is no need to implement
111          * cache mechanism. It is provided by InstanceLookup.Item.getInstance().
112          * Method should be called more than once because Lookup holds
113          * just weak reference.
114          *
115          * @param obj the registered object
116          * @return the object converted from this object
117          */

118         public R convert(T obj);
119
120         /** Return type of converted object.
121          * @param obj the registered object
122          * @return the class that will be produced from this object (class or
123          * superclass of convert (obj))
124          */

125         public Class JavaDoc<? extends R> type(T obj);
126
127         /** Computes the ID of the resulted object.
128          * @param obj the registered object
129          * @return the ID for the object
130          */

131         public String JavaDoc id(T obj);
132
133         /** The human presentable name for the object.
134          * @param obj the registered object
135          * @return the name representing the object for the user
136          */

137         public String JavaDoc displayName(T obj);
138     }
139
140     /** Instance of one item representing an object.
141      */

142     final static class SimpleItem<T> extends Pair<T> {
143         private T obj;
144
145         /** Create an item.
146          * @obj object to register
147          */

148         public SimpleItem(T obj) {
149             if (obj == null) {
150                 throw new NullPointerException JavaDoc();
151             }
152             this.obj = obj;
153         }
154
155         /** Tests whether this item can produce object
156          * of class c.
157          */

158         public boolean instanceOf(Class JavaDoc<?> c) {
159             return c.isInstance(obj);
160         }
161
162         /** Get instance of registered object. If convertor is specified then
163          * method InstanceLookup.Convertor.convertor is used and weak reference
164          * to converted object is saved.
165          * @return the instance of the object.
166          */

167         public T getInstance() {
168             return obj;
169         }
170
171         public boolean equals(Object JavaDoc o) {
172             if (o instanceof SimpleItem) {
173                 return obj.equals(((SimpleItem) o).obj);
174             } else {
175                 return false;
176             }
177         }
178
179         public int hashCode() {
180             return obj.hashCode();
181         }
182
183         /** An identity of the item.
184          * @return string representing the item, that can be used for
185          * persistance purposes to locate the same item next time
186          */

187         public String JavaDoc getId() {
188             return "IL[" + obj.toString(); // NOI18N
189
}
190
191         /** Getter for display name of the item.
192          */

193         public String JavaDoc getDisplayName() {
194             return obj.toString();
195         }
196
197         /** Method that can test whether an instance of a class has been created
198          * by this item.
199          *
200          * @param obj the instance
201          * @return if the item has already create an instance and it is the same
202          * as obj.
203          */

204         protected boolean creatorOf(Object JavaDoc obj) {
205             return obj == this.obj;
206         }
207
208         /** The class of this item.
209          * @return the correct class
210          */

211         @SuppressWarnings JavaDoc("unchecked")
212         public Class JavaDoc<? extends T> getType() {
213             return (Class JavaDoc<? extends T>)obj.getClass();
214         }
215     }
216      // end of SimpleItem
217

218     /** Instance of one item registered in the map.
219      */

220     final static class ConvertingItem<T,R> extends Pair<R> {
221         /** registered object */
222         private T obj;
223
224         /** Reference to converted object. */
225         private WeakReference JavaDoc<R> ref;
226
227         /** convertor to use */
228         private Convertor<? super T,R> conv;
229
230         /** Create an item.
231          * @obj object to register
232          * @conv a convertor, can be <code>null</code>.
233          */

234         public ConvertingItem(T obj, Convertor<? super T,R> conv) {
235             this.obj = obj;
236             this.conv = conv;
237         }
238
239         /** Tests whether this item can produce object
240          * of class c.
241          */

242         public boolean instanceOf(Class JavaDoc<?> c) {
243             return c.isAssignableFrom(getType());
244         }
245
246         /** Returns converted object or null if obj has not been converted yet
247          * or reference was cleared by garbage collector.
248          */

249         private R getConverted() {
250             if (ref == null) {
251                 return null;
252             }
253
254             return ref.get();
255         }
256
257         /** Get instance of registered object. If convertor is specified then
258          * method InstanceLookup.Convertor.convertor is used and weak reference
259          * to converted object is saved.
260          * @return the instance of the object.
261          */

262         public synchronized R getInstance() {
263             R converted = getConverted();
264
265             if (converted == null) {
266                 converted = conv.convert(obj);
267                 ref = new WeakReference JavaDoc<R>(converted);
268             }
269
270             return converted;
271         }
272
273         public boolean equals(Object JavaDoc o) {
274             if (o instanceof ConvertingItem) {
275                 return obj.equals(((ConvertingItem) o).obj);
276             } else {
277                 return false;
278             }
279         }
280
281         public int hashCode() {
282             return obj.hashCode();
283         }
284
285         /** An identity of the item.
286          * @return string representing the item, that can be used for
287          * persistance purposes to locate the same item next time
288          */

289         public String JavaDoc getId() {
290             return conv.id(obj);
291         }
292
293         /** Getter for display name of the item.
294          */

295         public String JavaDoc getDisplayName() {
296             return conv.displayName(obj);
297         }
298
299         /** Method that can test whether an instance of a class has been created
300          * by this item.
301          *
302          * @param obj the instance
303          * @return if the item has already create an instance and it is the same
304          * as obj.
305          */

306         protected boolean creatorOf(Object JavaDoc obj) {
307             if (conv == null) {
308                 return obj == this.obj;
309             } else {
310                 return obj == getConverted();
311             }
312         }
313
314         /** The class of this item.
315          * @return the correct class
316          */

317         @SuppressWarnings JavaDoc("unchecked")
318         public Class JavaDoc<? extends R> getType() {
319             R converted = getConverted();
320
321             if (converted == null) {
322                 return conv.type(obj);
323             }
324
325             return (Class JavaDoc<? extends R>)converted.getClass();
326         }
327     }
328      // end of ConvertingItem
329
}
330
Popular Tags