1.1 --- a/lookup/src/main/java/org/openide/util/lookup/SimpleLookup.java Wed Jan 27 17:46:23 2010 -0500
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,250 +0,0 @@
1.4 -/*
1.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1.6 - *
1.7 - * Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved.
1.8 - *
1.9 - * The contents of this file are subject to the terms of either the GNU
1.10 - * General Public License Version 2 only ("GPL") or the Common
1.11 - * Development and Distribution License("CDDL") (collectively, the
1.12 - * "License"). You may not use this file except in compliance with the
1.13 - * License. You can obtain a copy of the License at
1.14 - * http://www.netbeans.org/cddl-gplv2.html
1.15 - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
1.16 - * specific language governing permissions and limitations under the
1.17 - * License. When distributing the software, include this License Header
1.18 - * Notice in each file and include the License file at
1.19 - * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
1.20 - * particular file as subject to the "Classpath" exception as provided
1.21 - * by Sun in the GPL Version 2 section of the License file that
1.22 - * accompanied this code. If applicable, add the following below the
1.23 - * License Header, with the fields enclosed by brackets [] replaced by
1.24 - * your own identifying information:
1.25 - * "Portions Copyrighted [year] [name of copyright owner]"
1.26 - *
1.27 - * Contributor(s):
1.28 - *
1.29 - * The Original Software is NetBeans. The Initial Developer of the Original
1.30 - * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
1.31 - * Microsystems, Inc. All Rights Reserved.
1.32 - *
1.33 - * If you wish your version of this file to be governed by only the CDDL
1.34 - * or only the GPL Version 2, indicate your decision by adding
1.35 - * "[Contributor] elects to include this software in this distribution
1.36 - * under the [CDDL or GPL Version 2] license." If you do not indicate a
1.37 - * single choice of license, a recipient has the option to distribute
1.38 - * your version of this file under either the CDDL, the GPL Version 2 or
1.39 - * to extend the choice of license to its licensees as provided above.
1.40 - * However, if you add GPL Version 2 code and therefore, elected the GPL
1.41 - * Version 2 license, then the option applies only if the new code is
1.42 - * made subject to such option by the copyright holder.
1.43 - */
1.44 -package org.openide.util.lookup;
1.45 -
1.46 -import org.openide.util.Lookup;
1.47 -import org.openide.util.LookupListener;
1.48 -
1.49 -import java.util.*;
1.50 -
1.51 -
1.52 -/**
1.53 - * Simple lookup implementation. It can be used to create temporary lookups
1.54 - * that do not change over time. The result stores references to all objects
1.55 - * passed in the constructor. Those objecst are the only ones returned as
1.56 - * result.
1.57 - * @author David Strupl
1.58 - */
1.59 -class SimpleLookup extends org.openide.util.Lookup {
1.60 - /** This variable is initialized in constructor and thus null
1.61 - * value is not allowed as its value. */
1.62 - private Collection<Item<?>> allItems;
1.63 -
1.64 - /**
1.65 - * Creates new Result object with supplied instances parameter.
1.66 - * @param instances to be used to return from the lookup
1.67 - */
1.68 - SimpleLookup(Collection<Object> instances) {
1.69 - allItems = new ArrayList<Item<?>>(instances.size());
1.70 -
1.71 - for (Iterator i = instances.iterator(); i.hasNext();) {
1.72 - allItems.add(new InstanceContent.SimpleItem<Object>(i.next()));
1.73 - }
1.74 - }
1.75 -
1.76 - <T,R> SimpleLookup(Collection<T> keys, InstanceContent.Convertor<? super T,R> conv) {
1.77 - allItems = new ArrayList<Item<?>>(keys.size());
1.78 -
1.79 - for (T item : keys) {
1.80 - allItems.add(new InstanceContent.ConvertingItem<T,R>(item, conv));
1.81 - }
1.82 - }
1.83 -
1.84 - public String toString() {
1.85 - return "SimpleLookup" + lookup(new Template<Object>(Object.class)).allInstances();
1.86 - }
1.87 -
1.88 - public <T> Result<T> lookup(Template<T> template) {
1.89 - if (template == null) {
1.90 - throw new NullPointerException();
1.91 - }
1.92 -
1.93 - return new SimpleResult<T>(template);
1.94 - }
1.95 -
1.96 - public <T> T lookup(Class<T> clazz) {
1.97 - for (Iterator i = allItems.iterator(); i.hasNext();) {
1.98 - Object o = i.next();
1.99 -
1.100 - if (o instanceof AbstractLookup.Pair) {
1.101 - AbstractLookup.Pair<?> p = (AbstractLookup.Pair<?>)o;
1.102 - if (p.instanceOf(clazz)) {
1.103 - Object ret = p.getInstance();
1.104 - if (clazz.isInstance(ret)) {
1.105 - return clazz.cast(ret);
1.106 - }
1.107 - }
1.108 - }
1.109 - }
1.110 - return null;
1.111 - }
1.112 -
1.113 - /** A method that defines matching between Item and Template.
1.114 - * @param item the item to match
1.115 - * @return true if item matches the template requirements, false if not
1.116 - */
1.117 - private static boolean matches(Template<?> t, AbstractLookup.Pair<?> item) {
1.118 - if (!AbstractLookup.matches(t, item, true)) {
1.119 - return false;
1.120 - }
1.121 -
1.122 - Class<?> type = t.getType();
1.123 -
1.124 - if ((type != null) && !type.isAssignableFrom(item.getType())) {
1.125 - return false;
1.126 - }
1.127 -
1.128 - return true;
1.129 - }
1.130 -
1.131 - /**
1.132 - * Result used in SimpleLookup. It holds a reference to the collection
1.133 - * passed in constructor. As the contents of this lookup result never
1.134 - * changes the addLookupListener and removeLookupListener are empty.
1.135 - */
1.136 - private class SimpleResult<T> extends Lookup.Result<T> {
1.137 - /** can be null and is initialized lazily */
1.138 - private Set<Class<? extends T>> classes;
1.139 -
1.140 - /** can be null and is initialized lazily */
1.141 - private Collection<? extends Item<T>> items;
1.142 -
1.143 - /** Template used for this result. It is never null.*/
1.144 - private Template<T> template;
1.145 -
1.146 - /** can be null and is initialized lazily */
1.147 - private Collection<T> results;
1.148 -
1.149 - /** Just remembers the supplied argument in variable template.*/
1.150 - SimpleResult(Template<T> template) {
1.151 - this.template = template;
1.152 - }
1.153 -
1.154 - /**
1.155 - * Intentionally does nothing because the lookup does not change
1.156 - * and no notification is needed.
1.157 - */
1.158 - public void addLookupListener(LookupListener l) {
1.159 - }
1.160 -
1.161 - /**
1.162 - * Intentionally does nothing because the lookup does not change
1.163 - * and no notification is needed.
1.164 - */
1.165 - public void removeLookupListener(LookupListener l) {
1.166 - }
1.167 -
1.168 - /**
1.169 - * Lazy initializes the results collection. Uses a call to allItems
1.170 - * to obtain the instances.
1.171 - */
1.172 - public java.util.Collection<? extends T> allInstances() {
1.173 - synchronized (this) {
1.174 - if (results != null) {
1.175 - return results;
1.176 - }
1.177 - }
1.178 -
1.179 -
1.180 - Collection<T> res = new ArrayList<T>(allItems.size());
1.181 -
1.182 - for (Item<T> item : allItems()) {
1.183 - res.add(item.getInstance());
1.184 - }
1.185 -
1.186 - synchronized (this) {
1.187 - results = Collections.unmodifiableCollection(res);
1.188 - }
1.189 -
1.190 - return results;
1.191 - }
1.192 -
1.193 - /**
1.194 - * Lazy initializes variable classes. Uses a call to allItems to
1.195 - * compute the result.
1.196 - */
1.197 - public Set<Class<? extends T>> allClasses() {
1.198 - synchronized (this) {
1.199 - if (classes != null) {
1.200 - return classes;
1.201 - }
1.202 - }
1.203 -
1.204 - Set<Class<? extends T>> res = new HashSet<Class<? extends T>>();
1.205 -
1.206 - for (Item<T> item : allItems()) {
1.207 - res.add(item.getType());
1.208 - }
1.209 -
1.210 - synchronized (this) {
1.211 - classes = Collections.unmodifiableSet(res);
1.212 - }
1.213 -
1.214 - return classes;
1.215 - }
1.216 -
1.217 - /**
1.218 - * Lazy initializes variable items. Creates an item for each
1.219 - * element in the instances collection. It puts either SimpleItem
1.220 - * or ConvertingItem to the collection.
1.221 - */
1.222 - public Collection<? extends Item<T>> allItems() {
1.223 - synchronized (this) {
1.224 - if (items != null) {
1.225 - return items;
1.226 - }
1.227 - }
1.228 -
1.229 - Collection<Item<T>> res = new ArrayList<Item<T>>(allItems.size());
1.230 -
1.231 - for (Iterator<Item<?>> i = allItems.iterator(); i.hasNext();) {
1.232 - Item<?> o = i.next();
1.233 -
1.234 - if (o instanceof AbstractLookup.Pair) {
1.235 - if (matches(template, (AbstractLookup.Pair) o)) {
1.236 - res.add(cast(o));
1.237 - }
1.238 - }
1.239 - }
1.240 -
1.241 - synchronized (this) {
1.242 - items = Collections.unmodifiableCollection(res);
1.243 - }
1.244 -
1.245 - return items;
1.246 - }
1.247 -
1.248 - @SuppressWarnings("unchecked")
1.249 - private Item<T> cast(Item<?> i) {
1.250 - return (Item<T>)i;
1.251 - }
1.252 - }
1.253 -}