lookup/src/main/java/org/openide/util/lookup/SingletonLookup.java
changeset 972 a2947558c966
parent 971 b3ae88304dd0
child 973 5653a70ebb56
     1.1 --- a/lookup/src/main/java/org/openide/util/lookup/SingletonLookup.java	Wed Jan 27 17:46:23 2010 -0500
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,173 +0,0 @@
     1.4 -/*
     1.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6 - * 
     1.7 - * Copyright 2008 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 - * If you wish your version of this file to be governed by only the CDDL
    1.28 - * or only the GPL Version 2, indicate your decision by adding
    1.29 - * "[Contributor] elects to include this software in this distribution
    1.30 - * under the [CDDL or GPL Version 2] license." If you do not indicate a
    1.31 - * single choice of license, a recipient has the option to distribute
    1.32 - * your version of this file under either the CDDL, the GPL Version 2 or
    1.33 - * to extend the choice of license to its licensees as provided above.
    1.34 - * However, if you add GPL Version 2 code and therefore, elected the GPL
    1.35 - * Version 2 license, then the option applies only if the new code is
    1.36 - * made subject to such option by the copyright holder.
    1.37 - * 
    1.38 - * Contributor(s):
    1.39 - * 
    1.40 - * Portions Copyrighted 2008 Sun Microsystems, Inc.
    1.41 - */
    1.42 -
    1.43 -package org.openide.util.lookup;
    1.44 -
    1.45 -import java.util.Collection;
    1.46 -import java.util.Collections;
    1.47 -import java.util.Set;
    1.48 -import org.openide.util.Lookup;
    1.49 -import org.openide.util.LookupListener;
    1.50 -
    1.51 -/**
    1.52 - * Unmodifiable lookup that contains just one fixed object.
    1.53 - *
    1.54 - * @author Marian Petras
    1.55 - */
    1.56 -class SingletonLookup extends Lookup {
    1.57 -
    1.58 -    private final Object objectToLookup;
    1.59 -    private final String id;
    1.60 -
    1.61 -    SingletonLookup(Object objectToLookup) {
    1.62 -        this(objectToLookup, null);
    1.63 -    }
    1.64 -
    1.65 -    SingletonLookup(Object objectToLookup, String id) {
    1.66 -        if (objectToLookup == null) {
    1.67 -            throw new IllegalArgumentException("null");                 //NOI18N
    1.68 -        }
    1.69 -
    1.70 -        this.objectToLookup = objectToLookup;
    1.71 -        this.id = id;
    1.72 -    }
    1.73 -
    1.74 -    @Override
    1.75 -    public <T> T lookup(Class<T> clazz) {
    1.76 -        if (clazz == null) {
    1.77 -            throw new IllegalArgumentException("null");                 //NOI18N
    1.78 -        }
    1.79 -
    1.80 -        return (clazz.isInstance(objectToLookup))
    1.81 -               ? clazz.cast(objectToLookup)
    1.82 -               : null;
    1.83 -    }
    1.84 -
    1.85 -    @Override
    1.86 -    public <T> Result<T> lookup(Template<T> template) {
    1.87 -        if (template == null) {
    1.88 -            throw new IllegalArgumentException("null");                 //NOI18N
    1.89 -        }
    1.90 -
    1.91 -        Lookup.Item<T> item = lookupItem(template);
    1.92 -        if (item != null) {
    1.93 -            return new SingletonResult<T>(item);
    1.94 -        } else {
    1.95 -            return Lookup.EMPTY.lookup(template);
    1.96 -        }
    1.97 -    }
    1.98 -
    1.99 -    @Override
   1.100 -    public <T> Collection<? extends T> lookupAll(Class<T> clazz) {
   1.101 -        if (clazz == null) {
   1.102 -            throw new IllegalArgumentException("null");                 //NOI18N
   1.103 -        }
   1.104 -
   1.105 -        return (clazz.isInstance(objectToLookup))
   1.106 -               ? Collections.singletonList(clazz.cast(objectToLookup))
   1.107 -               : Collections.<T>emptyList();
   1.108 -    }
   1.109 -
   1.110 -    @Override
   1.111 -    @SuppressWarnings("unchecked")
   1.112 -    public <T> Item<T> lookupItem(Template<T> template) {
   1.113 -        if (template == null) {
   1.114 -            throw new IllegalArgumentException("null");                 //NOI18N
   1.115 -        }
   1.116 -
   1.117 -        String templateId = template.getId();
   1.118 -        if ((templateId != null) && !templateId.equals(id)) {
   1.119 -            return null;
   1.120 -        }
   1.121 -
   1.122 -        Object templateInst = template.getInstance();
   1.123 -        if ((templateInst != null) && (objectToLookup != templateInst)) {
   1.124 -            return null;
   1.125 -        }
   1.126 -
   1.127 -        Class<T> clazz = template.getType();
   1.128 -        if ((clazz != null) && !clazz.isInstance(objectToLookup)) {
   1.129 -            return null;
   1.130 -        }
   1.131 -
   1.132 -        Lookup.Item<T> item;
   1.133 -        if (clazz != null) {
   1.134 -            item = Lookups.lookupItem(clazz.cast(objectToLookup), id);
   1.135 -        } else {
   1.136 -            item = Lookups.lookupItem((T) objectToLookup, id);
   1.137 -        }
   1.138 -        return item;
   1.139 -    }
   1.140 -
   1.141 -    static class SingletonResult<T> extends Lookup.Result<T> {
   1.142 -
   1.143 -        private final Lookup.Item<T> item;
   1.144 -
   1.145 -        SingletonResult(Lookup.Item<T> item) {
   1.146 -            this.item = item;
   1.147 -        }
   1.148 -
   1.149 -        @Override
   1.150 -        public void addLookupListener(LookupListener l) {
   1.151 -            // this result never changes - no need to register a listener
   1.152 -        }
   1.153 -
   1.154 -        @Override
   1.155 -        public void removeLookupListener(LookupListener l) {
   1.156 -            // this result never changes - no need to register a listener
   1.157 -        }
   1.158 -
   1.159 -        @Override
   1.160 -        public Set<Class<? extends T>> allClasses() {
   1.161 -            return Collections.<Class<? extends T>>singleton(item.getType());
   1.162 -        }
   1.163 -
   1.164 -        @Override
   1.165 -        public Collection<? extends Item<T>> allItems() {
   1.166 -            return Collections.singletonList(item);
   1.167 -        }
   1.168 -
   1.169 -        @Override
   1.170 -        public Collection<? extends T> allInstances() {
   1.171 -            return Collections.singletonList(item.getInstance());
   1.172 -        }
   1.173 -
   1.174 -    }
   1.175 -
   1.176 -}