001    /*****************************************************************************
002     * Copyright (C) PicoContainer Organization. All rights reserved.            *
003     * ------------------------------------------------------------------------- *
004     * The software in this package is published under the terms of the BSD      *
005     * style license a copy of which has been included with this distribution in *
006     * the LICENSE.txt file.                                                     *
007     *                                                                           *
008     * Original code by                                                          *
009     *****************************************************************************/
010    package org.picocontainer.containers;
011    
012    import org.picocontainer.PicoContainer;
013    import org.picocontainer.ComponentAdapter;
014    import org.picocontainer.PicoVisitor;
015    import org.picocontainer.NameBinding;
016    
017    import java.util.List;
018    import java.util.Collection;
019    import java.io.Serializable;
020    import java.lang.annotation.Annotation;
021    import java.lang.reflect.Type;
022    
023    /**
024    * wrap pico container to achieve immutability
025     * Typically its used to mock a parent container.
026     *
027     * @author Konstantin Pribluda
028     */
029    @SuppressWarnings("serial")
030    public final class ImmutablePicoContainer implements PicoContainer, Serializable {
031    
032        private final PicoContainer delegate;
033    
034        public ImmutablePicoContainer(PicoContainer delegate) {
035            if (delegate == null) {
036                throw new NullPointerException();
037            }
038            this.delegate = delegate;
039        }
040    
041        public Object getComponent(Object componentKeyOrType) {
042            return delegate.getComponent(componentKeyOrType);
043        }
044    
045        public Object getComponent(Object componentKeyOrType, Type into) {
046            return delegate.getComponent(componentKeyOrType, into);
047        }
048    
049        public <T> T getComponent(Class<T> componentType) {
050            return delegate.getComponent(componentType);
051        }
052    
053        public <T> T getComponent(Class<T> componentType, Class<? extends Annotation> binding) {
054            return delegate.getComponent(componentType, binding);
055        }
056    
057        public List getComponents() {
058            return delegate.getComponents();
059        }
060    
061        public PicoContainer getParent() {
062            return delegate.getParent();
063        }
064    
065        public ComponentAdapter<?> getComponentAdapter(Object componentKey) {
066            return delegate.getComponentAdapter(componentKey);
067        }
068    
069        public <T> ComponentAdapter<T> getComponentAdapter(Class<T> componentType, NameBinding componentNameBinding) {
070            return delegate.getComponentAdapter(componentType, componentNameBinding);
071        }
072    
073        public <T> ComponentAdapter<T> getComponentAdapter(Class<T> componentType, Class<? extends Annotation> binding) {
074            return delegate.getComponentAdapter(componentType, binding);
075        }
076    
077        public Collection<ComponentAdapter<?>> getComponentAdapters() {
078            return delegate.getComponentAdapters();
079        }
080    
081        public <T> List<ComponentAdapter<T>> getComponentAdapters(Class<T> componentType) {
082            return delegate.getComponentAdapters(componentType);
083        }
084    
085        public <T> List<ComponentAdapter<T>> getComponentAdapters(Class<T> componentType, Class<? extends Annotation> binding) {
086            return delegate.getComponentAdapters(componentType, binding);
087        }
088    
089        public <T> List<T> getComponents(Class<T> componentType) {
090            return delegate.getComponents(componentType);
091        }
092    
093        public final void accept(PicoVisitor visitor) {
094            // don't visit "this" its pointless.
095            delegate.accept(visitor);
096        }
097    
098        public boolean equals(Object obj) {
099            return obj == this
100                   || (obj != null && obj == delegate)
101                   || (obj instanceof ImmutablePicoContainer && ((ImmutablePicoContainer) obj).delegate == delegate)
102                ;
103        }
104    
105        public int hashCode() {
106            return delegate.hashCode();
107        }
108    
109        public String toString() {
110            return "I<" + delegate.toString();
111        }
112    }