Automated merge release65_m1_base
authorffjre@netbeans.org
Mon, 23 Jun 2008 22:18:31 +0200
changeset 4148de20b457675
parent 412 e739c58d2571
parent 413 7de4d02b44b6
child 415 6f575d394b5f
child 416 f4ad1a5385d0
child 417 7d2b0bcd263c
Automated merge
     1.1 --- a/openide.util/src/org/openide/util/lookup/Lookups.java	Mon Jun 23 12:20:58 2008 -0400
     1.2 +++ b/openide.util/src/org/openide/util/lookup/Lookups.java	Mon Jun 23 22:18:31 2008 +0200
     1.3 @@ -1,7 +1,7 @@
     1.4  /*
     1.5   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6   *
     1.7 - * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
     1.8 + * Copyright 1997-2008 Sun Microsystems, Inc. All rights reserved.
     1.9   *
    1.10   * The contents of this file are subject to the terms of either the GNU
    1.11   * General Public License Version 2 only ("GPL") or the Common
    1.12 @@ -24,7 +24,7 @@
    1.13   * Contributor(s):
    1.14   *
    1.15   * The Original Software is NetBeans. The Initial Developer of the Original
    1.16 - * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
    1.17 + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2008 Sun
    1.18   * Microsystems, Inc. All Rights Reserved.
    1.19   *
    1.20   * If you wish your version of this file to be governed by only the CDDL
    1.21 @@ -42,7 +42,6 @@
    1.22  package org.openide.util.lookup;
    1.23  
    1.24  import java.util.Arrays;
    1.25 -import java.util.Collections;
    1.26  import org.netbeans.modules.openide.util.NamedServicesProvider;
    1.27  import org.openide.util.Lookup;
    1.28  
    1.29 @@ -72,10 +71,7 @@
    1.30              throw new NullPointerException();
    1.31          }
    1.32  
    1.33 -        // performance of the resulting lookup might be further
    1.34 -        // improved by providing specialized singleton result (and lookup)
    1.35 -        // instead of using SimpleResult
    1.36 -        return new SimpleLookup(Collections.singleton(objectToLookup));
    1.37 +        return new SingletonLookup(objectToLookup);
    1.38      }
    1.39  
    1.40      /**
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/openide.util/src/org/openide/util/lookup/SingletonLookup.java	Mon Jun 23 22:18:31 2008 +0200
     2.3 @@ -0,0 +1,172 @@
     2.4 +/*
     2.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     2.6 + * 
     2.7 + * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
     2.8 + * 
     2.9 + * The contents of this file are subject to the terms of either the GNU
    2.10 + * General Public License Version 2 only ("GPL") or the Common
    2.11 + * Development and Distribution License("CDDL") (collectively, the
    2.12 + * "License"). You may not use this file except in compliance with the
    2.13 + * License. You can obtain a copy of the License at
    2.14 + * http://www.netbeans.org/cddl-gplv2.html
    2.15 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    2.16 + * specific language governing permissions and limitations under the
    2.17 + * License.  When distributing the software, include this License Header
    2.18 + * Notice in each file and include the License file at
    2.19 + * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    2.20 + * particular file as subject to the "Classpath" exception as provided
    2.21 + * by Sun in the GPL Version 2 section of the License file that
    2.22 + * accompanied this code. If applicable, add the following below the
    2.23 + * License Header, with the fields enclosed by brackets [] replaced by
    2.24 + * your own identifying information:
    2.25 + * "Portions Copyrighted [year] [name of copyright owner]"
    2.26 + * 
    2.27 + * If you wish your version of this file to be governed by only the CDDL
    2.28 + * or only the GPL Version 2, indicate your decision by adding
    2.29 + * "[Contributor] elects to include this software in this distribution
    2.30 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
    2.31 + * single choice of license, a recipient has the option to distribute
    2.32 + * your version of this file under either the CDDL, the GPL Version 2 or
    2.33 + * to extend the choice of license to its licensees as provided above.
    2.34 + * However, if you add GPL Version 2 code and therefore, elected the GPL
    2.35 + * Version 2 license, then the option applies only if the new code is
    2.36 + * made subject to such option by the copyright holder.
    2.37 + * 
    2.38 + * Contributor(s):
    2.39 + * 
    2.40 + * Portions Copyrighted 2008 Sun Microsystems, Inc.
    2.41 + */
    2.42 +
    2.43 +package org.openide.util.lookup;
    2.44 +
    2.45 +import java.util.Collection;
    2.46 +import java.util.Collections;
    2.47 +import java.util.Set;
    2.48 +import org.openide.util.Lookup;
    2.49 +import org.openide.util.LookupListener;
    2.50 +
    2.51 +/**
    2.52 + * Unmodifiable lookup that contains just one fixed object.
    2.53 + *
    2.54 + * @author Marian Petras
    2.55 + */
    2.56 +class SingletonLookup extends Lookup {
    2.57 +
    2.58 +    private final Object objectToLookup;
    2.59 +    private final String id;
    2.60 +
    2.61 +    SingletonLookup(Object objectToLookup) {
    2.62 +        this(objectToLookup, null);
    2.63 +    }
    2.64 +
    2.65 +    SingletonLookup(Object objectToLookup, String id) {
    2.66 +        if (objectToLookup == null) {
    2.67 +            throw new IllegalArgumentException("null");                 //NOI18N
    2.68 +        }
    2.69 +
    2.70 +        this.objectToLookup = objectToLookup;
    2.71 +        this.id = id;
    2.72 +    }
    2.73 +
    2.74 +    @Override
    2.75 +    public <T> T lookup(Class<T> clazz) {
    2.76 +        if (clazz == null) {
    2.77 +            throw new IllegalArgumentException("null");                 //NOI18N
    2.78 +        }
    2.79 +
    2.80 +        return (clazz.isInstance(objectToLookup))
    2.81 +               ? clazz.cast(objectToLookup)
    2.82 +               : null;
    2.83 +    }
    2.84 +
    2.85 +    @Override
    2.86 +    public <T> Result<T> lookup(Template<T> template) {
    2.87 +        if (template == null) {
    2.88 +            throw new IllegalArgumentException("null");                 //NOI18N
    2.89 +        }
    2.90 +
    2.91 +        Lookup.Item<T> item = lookupItem(template);
    2.92 +        if (item != null) {
    2.93 +            return new SingletonResult<T>(item);
    2.94 +        } else {
    2.95 +            return Lookup.EMPTY.lookup(template);
    2.96 +        }
    2.97 +    }
    2.98 +
    2.99 +    @Override
   2.100 +    public <T> Collection<? extends T> lookupAll(Class<T> clazz) {
   2.101 +        if (clazz == null) {
   2.102 +            throw new IllegalArgumentException("null");                 //NOI18N
   2.103 +        }
   2.104 +
   2.105 +        return (clazz.isInstance(objectToLookup))
   2.106 +               ? Collections.singletonList(clazz.cast(objectToLookup))
   2.107 +               : Collections.<T>emptyList();
   2.108 +    }
   2.109 +
   2.110 +    @Override
   2.111 +    public <T> Item<T> lookupItem(Template<T> template) {
   2.112 +        if (template == null) {
   2.113 +            throw new IllegalArgumentException("null");                 //NOI18N
   2.114 +        }
   2.115 +
   2.116 +        String templateId = template.getId();
   2.117 +        if ((templateId != null) && !templateId.equals(id)) {
   2.118 +            return null;
   2.119 +        }
   2.120 +
   2.121 +        Object templateInst = template.getInstance();
   2.122 +        if ((templateInst != null) && (objectToLookup != templateInst)) {
   2.123 +            return null;
   2.124 +        }
   2.125 +
   2.126 +        Class<T> clazz = template.getType();
   2.127 +        if ((clazz != null) && !clazz.isInstance(objectToLookup)) {
   2.128 +            return null;
   2.129 +        }
   2.130 +
   2.131 +        Lookup.Item<T> item;
   2.132 +        if (clazz != null) {
   2.133 +            item = Lookups.lookupItem(clazz.cast(objectToLookup), id);
   2.134 +        } else {
   2.135 +            item = Lookups.lookupItem((T) objectToLookup, id);
   2.136 +        }
   2.137 +        return item;
   2.138 +    }
   2.139 +
   2.140 +    static class SingletonResult<T> extends Lookup.Result<T> {
   2.141 +
   2.142 +        private final Lookup.Item<T> item;
   2.143 +
   2.144 +        SingletonResult(Lookup.Item<T> item) {
   2.145 +            this.item = item;
   2.146 +        }
   2.147 +
   2.148 +        @Override
   2.149 +        public void addLookupListener(LookupListener l) {
   2.150 +            // this result never changes - no need to register a listener
   2.151 +        }
   2.152 +
   2.153 +        @Override
   2.154 +        public void removeLookupListener(LookupListener l) {
   2.155 +            // this result never changes - no need to register a listener
   2.156 +        }
   2.157 +
   2.158 +        @Override
   2.159 +        public Set<Class<? extends T>> allClasses() {
   2.160 +            return Collections.<Class<? extends T>>singleton(item.getType());
   2.161 +        }
   2.162 +
   2.163 +        @Override
   2.164 +        public Collection<? extends Item<T>> allItems() {
   2.165 +            return Collections.singletonList(item);
   2.166 +        }
   2.167 +
   2.168 +        @Override
   2.169 +        public Collection<? extends T> allInstances() {
   2.170 +            return Collections.singletonList(item.getInstance());
   2.171 +        }
   2.172 +
   2.173 +    }
   2.174 +
   2.175 +}
   2.176 \ No newline at end of file
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/openide.util/test/unit/src/org/openide/util/lookup/SingletonLookupTest.java	Mon Jun 23 22:18:31 2008 +0200
     3.3 @@ -0,0 +1,113 @@
     3.4 +/*
     3.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3.6 + * 
     3.7 + * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
     3.8 + * 
     3.9 + * The contents of this file are subject to the terms of either the GNU
    3.10 + * General Public License Version 2 only ("GPL") or the Common
    3.11 + * Development and Distribution License("CDDL") (collectively, the
    3.12 + * "License"). You may not use this file except in compliance with the
    3.13 + * License. You can obtain a copy of the License at
    3.14 + * http://www.netbeans.org/cddl-gplv2.html
    3.15 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    3.16 + * specific language governing permissions and limitations under the
    3.17 + * License.  When distributing the software, include this License Header
    3.18 + * Notice in each file and include the License file at
    3.19 + * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    3.20 + * particular file as subject to the "Classpath" exception as provided
    3.21 + * by Sun in the GPL Version 2 section of the License file that
    3.22 + * accompanied this code. If applicable, add the following below the
    3.23 + * License Header, with the fields enclosed by brackets [] replaced by
    3.24 + * your own identifying information:
    3.25 + * "Portions Copyrighted [year] [name of copyright owner]"
    3.26 + * 
    3.27 + * If you wish your version of this file to be governed by only the CDDL
    3.28 + * or only the GPL Version 2, indicate your decision by adding
    3.29 + * "[Contributor] elects to include this software in this distribution
    3.30 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
    3.31 + * single choice of license, a recipient has the option to distribute
    3.32 + * your version of this file under either the CDDL, the GPL Version 2 or
    3.33 + * to extend the choice of license to its licensees as provided above.
    3.34 + * However, if you add GPL Version 2 code and therefore, elected the GPL
    3.35 + * Version 2 license, then the option applies only if the new code is
    3.36 + * made subject to such option by the copyright holder.
    3.37 + * 
    3.38 + * Contributor(s):
    3.39 + * 
    3.40 + * Portions Copyrighted 2008 Sun Microsystems, Inc.
    3.41 + */
    3.42 +
    3.43 +package org.openide.util.lookup;
    3.44 +
    3.45 +import java.util.Collection;
    3.46 +import org.netbeans.junit.NbTestCase;
    3.47 +import org.openide.util.Lookup;
    3.48 +
    3.49 +/**
    3.50 + * Contains tests of class {@code SingletonLookup}.
    3.51 + *
    3.52 + * @author  Marian Petras
    3.53 + */
    3.54 +public class SingletonLookupTest extends NbTestCase {
    3.55 +    
    3.56 +    public SingletonLookupTest(String testName) {
    3.57 +        super(testName);
    3.58 +    }
    3.59 +
    3.60 +    public void testBasics() {
    3.61 +        Object orig = new Object();
    3.62 +        Lookup p1 = new SingletonLookup(orig);
    3.63 +        Object obj = p1.lookup(Object.class);
    3.64 +        assertTrue(obj == orig);
    3.65 +        assertNull(p1.lookup(String.class)); 
    3.66 +        assertTrue(orig == p1.lookup(Object.class)); // 2nd time, still the same?
    3.67 +        //
    3.68 +        Lookup p2 = new SingletonLookup("test");
    3.69 +        assertNotNull(p2.lookup(Object.class));
    3.70 +        assertNotNull(p2.lookup(String.class));
    3.71 +        assertNotNull(p2.lookup(java.io.Serializable.class));
    3.72 +    }
    3.73 +
    3.74 +    public void testId() {
    3.75 +        Object orig = new Object();
    3.76 +        Collection allInstances;
    3.77 +
    3.78 +        Lookup l = new SingletonLookup(orig, "id");
    3.79 +
    3.80 +        allInstances = l.lookup(new Lookup.Template<Object>(Object.class, null, null)).allInstances();
    3.81 +        assertNotNull(allInstances);
    3.82 +        assertFalse(allInstances.isEmpty());
    3.83 +        assertEquals(1, allInstances.size());
    3.84 +        assertTrue(allInstances.iterator().next() == orig);
    3.85 +
    3.86 +        allInstances = l.lookup(new Lookup.Template<Object>(Object.class, "id", null)).allInstances();
    3.87 +        assertNotNull(allInstances);
    3.88 +        assertFalse(allInstances.isEmpty());
    3.89 +        assertEquals(1, allInstances.size());
    3.90 +        assertTrue(allInstances.iterator().next() == orig);
    3.91 +
    3.92 +        allInstances = l.lookup(new Lookup.Template<Object>(Object.class, "not", null)).allInstances();
    3.93 +        assertNotNull(allInstances);
    3.94 +        assertTrue(allInstances.isEmpty());
    3.95 +
    3.96 +        allInstances = l.lookup(new Lookup.Template<String>(String.class, null, null)).allInstances();
    3.97 +        assertNotNull(allInstances);
    3.98 +        assertTrue(allInstances.isEmpty());
    3.99 +
   3.100 +        allInstances = l.lookup(new Lookup.Template<String>(String.class, "id", null)).allInstances();
   3.101 +        assertNotNull(allInstances);
   3.102 +        assertTrue(allInstances.isEmpty());
   3.103 +
   3.104 +        allInstances = l.lookup(new Lookup.Template<String>(String.class, "not", null)).allInstances();
   3.105 +        assertNotNull(allInstances);
   3.106 +        assertTrue(allInstances.isEmpty());
   3.107 +    }
   3.108 +
   3.109 +    public void testSize() {
   3.110 +        final Object obj = new Object();
   3.111 +        assertSize("The singleton lookup instance should be small",
   3.112 +                   24,
   3.113 +                   new SingletonLookup(obj));
   3.114 +    }
   3.115 +
   3.116 +}