lookup/src/main/java/org/openide/util/lookup/DelegatingStorage.java
changeset 972 a2947558c966
parent 971 b3ae88304dd0
child 973 5653a70ebb56
     1.1 --- a/lookup/src/main/java/org/openide/util/lookup/DelegatingStorage.java	Wed Jan 27 17:46:23 2010 -0500
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,180 +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 -
    1.48 -import java.io.*;
    1.49 -
    1.50 -import java.lang.ref.WeakReference;
    1.51 -
    1.52 -import java.util.*;
    1.53 -import org.openide.util.lookup.AbstractLookup.Pair;
    1.54 -
    1.55 -
    1.56 -/** Storages that can switch between another storages.
    1.57 - * @author  Jaroslav Tulach
    1.58 - */
    1.59 -final class DelegatingStorage<Transaction> extends Object
    1.60 -implements Serializable, AbstractLookup.Storage<Transaction> {
    1.61 -    /** object to delegate to */
    1.62 -    private AbstractLookup.Storage<Transaction> delegate;
    1.63 -
    1.64 -    /** thread just accessing the storage */
    1.65 -    private Thread owner;
    1.66 -
    1.67 -    public DelegatingStorage(AbstractLookup.Storage<Transaction> d) {
    1.68 -        this.delegate = d;
    1.69 -        this.owner = Thread.currentThread();
    1.70 -    }
    1.71 -
    1.72 -    /** Never serialize yourself, always put there the delegate */
    1.73 -    public Object writeReplace() {
    1.74 -        return this.delegate;
    1.75 -    }
    1.76 -
    1.77 -    /** Method to check whether there is not multiple access from the same thread.
    1.78 -     */
    1.79 -    public void checkForTreeModification() {
    1.80 -        if (Thread.currentThread() == owner) {
    1.81 -            throw new AbstractLookup.ISE("You are trying to modify lookup from lookup query!"); // NOI18N
    1.82 -        }
    1.83 -    }
    1.84 -
    1.85 -    /** Checks whether we have simple behaviour or complex.
    1.86 -     */
    1.87 -    public static boolean isSimple(AbstractLookup.Storage s) {
    1.88 -        if (s instanceof DelegatingStorage) {
    1.89 -            return ((DelegatingStorage) s).delegate instanceof ArrayStorage;
    1.90 -        } else {
    1.91 -            return s instanceof ArrayStorage;
    1.92 -        }
    1.93 -    }
    1.94 -
    1.95 -    /** Exits from the owners ship of the storage.
    1.96 -     */
    1.97 -    public AbstractLookup.Storage<Transaction> exitDelegate() {
    1.98 -        if (Thread.currentThread() != owner) {
    1.99 -            throw new IllegalStateException("Onwer: " + owner + " caller: " + Thread.currentThread()); // NOI18N
   1.100 -        }
   1.101 -
   1.102 -        AbstractLookup.Storage<Transaction> d = delegate;
   1.103 -        delegate = null;
   1.104 -
   1.105 -        return d;
   1.106 -    }
   1.107 -
   1.108 -    public boolean add(AbstractLookup.Pair<?> item, Transaction transaction) {
   1.109 -        return delegate.add(item, transaction);
   1.110 -    }
   1.111 -
   1.112 -    public void remove(org.openide.util.lookup.AbstractLookup.Pair item, Transaction transaction) {
   1.113 -        delegate.remove(item, transaction);
   1.114 -    }
   1.115 -
   1.116 -    public void retainAll(Map retain, Transaction transaction) {
   1.117 -        delegate.retainAll(retain, transaction);
   1.118 -    }
   1.119 -
   1.120 -    /** A special method to change the backing storage.
   1.121 -     * In fact it is not much typesafe as it changes the
   1.122 -     * type of Transaction but we know that nobody is currently
   1.123 -     * holding a transaction object, so there cannot be inconsitencies.
   1.124 -     */
   1.125 -    @SuppressWarnings("unchecked")
   1.126 -    private void changeDelegate(InheritanceTree st) {
   1.127 -        delegate = (AbstractLookup.Storage<Transaction>)st;
   1.128 -    }
   1.129 -
   1.130 -    public Transaction beginTransaction(int ensure) {
   1.131 -        try {
   1.132 -            return delegate.beginTransaction(ensure);
   1.133 -        } catch (UnsupportedOperationException ex) {
   1.134 -            // let's convert to InheritanceTree
   1.135 -            ArrayStorage arr = (ArrayStorage) delegate;
   1.136 -            InheritanceTree inh = new InheritanceTree();
   1.137 -            changeDelegate(inh);
   1.138 -
   1.139 -            //
   1.140 -            // Copy content
   1.141 -            //
   1.142 -            Enumeration<Pair<Object>> en = arr.lookup(Object.class);
   1.143 -
   1.144 -            while (en.hasMoreElements()) {
   1.145 -                if (!inh.add(en.nextElement(), new ArrayList<Class>())) {
   1.146 -                    throw new IllegalStateException("All objects have to be accepted"); // NOI18N
   1.147 -                }
   1.148 -            }
   1.149 -
   1.150 -            //
   1.151 -            // Copy listeners
   1.152 -            //
   1.153 -            AbstractLookup.ReferenceToResult<?> ref = arr.cleanUpResult(null);
   1.154 -
   1.155 -            if (ref != null) {
   1.156 -                ref.cloneList(inh);
   1.157 -            }
   1.158 -
   1.159 -            // we have added the current content and now we can start transaction
   1.160 -            return delegate.beginTransaction(ensure);
   1.161 -        }
   1.162 -    }
   1.163 -
   1.164 -    public org.openide.util.lookup.AbstractLookup.ReferenceToResult cleanUpResult(
   1.165 -        org.openide.util.Lookup.Template templ
   1.166 -    ) {
   1.167 -        return delegate.cleanUpResult(templ);
   1.168 -    }
   1.169 -
   1.170 -    public void endTransaction(Transaction transaction, Set<AbstractLookup.R> modified) {
   1.171 -        delegate.endTransaction(transaction, modified);
   1.172 -    }
   1.173 -
   1.174 -    public <T> Enumeration<Pair<T>> lookup(Class<T> clazz) {
   1.175 -        return delegate.lookup(clazz);
   1.176 -    }
   1.177 -
   1.178 -    public org.openide.util.lookup.AbstractLookup.ReferenceToResult registerReferenceToResult(
   1.179 -        org.openide.util.lookup.AbstractLookup.ReferenceToResult newRef
   1.180 -    ) {
   1.181 -        return delegate.registerReferenceToResult(newRef);
   1.182 -    }
   1.183 -}