1 19 20 package org.openide.util.lookup; 21 22 import java.util.Arrays ; 23 import java.util.Collections ; 24 import org.openide.util.Lookup; 25 26 32 public class Lookups { 33 34 35 private Lookups() {} 36 37 47 public static Lookup singleton(Object objectToLookup) { 48 if (objectToLookup == null) { 49 throw new NullPointerException (); 50 } 51 52 return new SimpleLookup(Collections.singleton(objectToLookup)); 56 } 57 58 71 public static Lookup fixed(Object ... objectsToLookup) { 72 if (objectsToLookup == null) { 73 throw new NullPointerException (); 74 } 75 76 return new SimpleLookup(Arrays.asList(objectsToLookup)); 77 } 78 79 93 public static <T,R> Lookup fixed(T[] keys, InstanceContent.Convertor<? super T,R> convertor) { 94 if (keys == null) { 95 throw new NullPointerException (); 96 } 97 98 if (convertor == null) { 99 throw new NullPointerException (); 100 } 101 102 return new SimpleLookup(Arrays.asList(keys), convertor); 103 } 104 105 123 public static Lookup proxy(Lookup.Provider provider) { 124 return new SimpleProxyLookup(provider); 125 } 126 127 136 public static Lookup metaInfServices(ClassLoader classLoader) { 137 return new MetaInfServicesLookup(classLoader); 138 } 139 140 178 public static Lookup exclude(Lookup lookup, Class ... classes) { 179 return new ExcludingLookup(lookup, classes); 180 } 181 182 190 public static <T> Lookup.Item<T> lookupItem(T instance, String id) { 191 return new LookupItem<T>(instance, id); 192 } 193 194 private static class LookupItem<T> extends Lookup.Item<T> { 195 private String id; 196 private T instance; 197 198 public LookupItem(T instance) { 199 this(instance, null); 200 } 201 202 public LookupItem(T instance, String id) { 203 this.id = id; 204 this.instance = instance; 205 } 206 207 public String getDisplayName() { 208 return getId(); 209 } 210 211 public String getId() { 212 return (id == null) ? instance.toString() : id; 213 } 214 215 public T getInstance() { 216 return instance; 217 } 218 219 @SuppressWarnings ("unchecked") 220 public Class <? extends T> getType() { 221 return (Class <? extends T>)instance.getClass(); 222 } 223 224 public boolean equals(Object object) { 225 if (object instanceof LookupItem) { 226 return instance == ((LookupItem) object).getInstance(); 227 } 228 229 return false; 230 } 231 232 public int hashCode() { 233 return instance.hashCode(); 234 } 235 } 236 } 238 | Popular Tags |