1 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 ; 25 26 import java.util.*; 27 28 29 45 public final class InstanceContent extends AbstractLookup.Content { 46 49 public InstanceContent() { 50 } 51 52 55 public final void add(Object inst) { 56 addPair(new SimpleItem<Object >(inst)); 57 } 58 59 64 public final <T,R> void add(T inst, Convertor<T,R> conv) { 65 addPair(new ConvertingItem<T,R>(inst, conv)); 66 } 67 68 71 public final void remove(Object inst) { 72 removePair(new SimpleItem<Object >(inst)); 73 } 74 75 80 public final <T,R> void remove(T inst, Convertor<T,R> conv) { 81 removePair(new ConvertingItem<T,R>(inst, conv)); 82 } 83 84 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 109 public static interface Convertor<T,R> { 110 118 public R convert(T obj); 119 120 125 public Class <? extends R> type(T obj); 126 127 131 public String id(T obj); 132 133 137 public String displayName(T obj); 138 } 139 140 142 final static class SimpleItem<T> extends Pair<T> { 143 private T obj; 144 145 148 public SimpleItem(T obj) { 149 if (obj == null) { 150 throw new NullPointerException (); 151 } 152 this.obj = obj; 153 } 154 155 158 public boolean instanceOf(Class <?> c) { 159 return c.isInstance(obj); 160 } 161 162 167 public T getInstance() { 168 return obj; 169 } 170 171 public boolean equals(Object 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 187 public String getId() { 188 return "IL[" + obj.toString(); } 190 191 193 public String getDisplayName() { 194 return obj.toString(); 195 } 196 197 204 protected boolean creatorOf(Object obj) { 205 return obj == this.obj; 206 } 207 208 211 @SuppressWarnings ("unchecked") 212 public Class <? extends T> getType() { 213 return (Class <? extends T>)obj.getClass(); 214 } 215 } 216 218 220 final static class ConvertingItem<T,R> extends Pair<R> { 221 222 private T obj; 223 224 225 private WeakReference <R> ref; 226 227 228 private Convertor<? super T,R> conv; 229 230 234 public ConvertingItem(T obj, Convertor<? super T,R> conv) { 235 this.obj = obj; 236 this.conv = conv; 237 } 238 239 242 public boolean instanceOf(Class <?> c) { 243 return c.isAssignableFrom(getType()); 244 } 245 246 249 private R getConverted() { 250 if (ref == null) { 251 return null; 252 } 253 254 return ref.get(); 255 } 256 257 262 public synchronized R getInstance() { 263 R converted = getConverted(); 264 265 if (converted == null) { 266 converted = conv.convert(obj); 267 ref = new WeakReference <R>(converted); 268 } 269 270 return converted; 271 } 272 273 public boolean equals(Object 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 289 public String getId() { 290 return conv.id(obj); 291 } 292 293 295 public String getDisplayName() { 296 return conv.displayName(obj); 297 } 298 299 306 protected boolean creatorOf(Object obj) { 307 if (conv == null) { 308 return obj == this.obj; 309 } else { 310 return obj == getConverted(); 311 } 312 } 313 314 317 @SuppressWarnings ("unchecked") 318 public Class <? extends R> getType() { 319 R converted = getConverted(); 320 321 if (converted == null) { 322 return conv.type(obj); 323 } 324 325 return (Class <? extends R>)converted.getClass(); 326 } 327 } 328 } 330 | Popular Tags |