1 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 34 class SimpleLookup extends org.openide.util.Lookup { 35 37 private Collection<Item<?>> allItems; 38 39 43 SimpleLookup(Collection<Object > instances) { 44 allItems = new ArrayList<Item<?>>(instances.size()); 45 46 for (Iterator i = instances.iterator(); i.hasNext();) { 47 allItems.add(new InstanceContent.SimpleItem<Object >(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 toString() { 60 return "SimpleLookup" + lookup(new Template<Object >(Object .class)).allInstances(); 61 } 62 63 public <T> Result<T> lookup(Template<T> template) { 64 if (template == null) { 65 throw new NullPointerException (); 66 } 67 68 return new SimpleResult<T>(template); 69 } 70 71 public <T> T lookup(Class <T> clazz) { 72 for (Iterator i = allItems.iterator(); i.hasNext();) { 73 Object o = i.next(); 74 75 if (o instanceof AbstractLookup.Pair) { 76 AbstractLookup.Pair<?> p = (AbstractLookup.Pair<?>)o; 77 if (p.instanceOf(clazz)) { 78 Object ret = p.getInstance(); 79 if (clazz.isInstance(ret)) { 80 return clazz.cast(ret); 81 } 82 } 83 } 84 } 85 return null; 86 } 87 88 92 private static boolean matches(Template<?> t, AbstractLookup.Pair<?> item) { 93 if (!AbstractLookup.matches(t, item, true)) { 94 return false; 95 } 96 97 Class <?> type = t.getType(); 98 99 if ((type != null) && !type.isAssignableFrom(item.getType())) { 100 return false; 101 } 102 103 return true; 104 } 105 106 111 private class SimpleResult<T> extends Lookup.Result<T> { 112 113 private Set<Class <? extends T>> classes; 114 115 116 private Collection<? extends Item<T>> items; 117 118 119 private Template<T> template; 120 121 122 private Collection<T> results; 123 124 125 SimpleResult(Template<T> template) { 126 this.template = template; 127 } 128 129 133 public void addLookupListener(LookupListener l) { 134 } 135 136 140 public void removeLookupListener(LookupListener l) { 141 } 142 143 147 public java.util.Collection <? 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 172 public Set<Class <? extends T>> allClasses() { 173 synchronized (this) { 174 if (classes != null) { 175 return classes; 176 } 177 } 178 179 Set<Class <? extends T>> res = new HashSet<Class <? 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 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 ("unchecked") 224 private Item<T> cast(Item<?> i) { 225 return (Item<T>)i; 226 } 227 } 228 } 229 | Popular Tags |