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.*;
013    import org.picocontainer.converters.ConvertsNothing;
014    
015    import java.util.List;
016    import java.util.Collection;
017    import java.io.Serializable;
018    import java.lang.annotation.Annotation;
019    import java.lang.reflect.Type;
020    
021    /**
022    * wrap pico container to achieve immutability
023     * Typically its used to mock a parent container.
024     *
025     * @author Konstantin Pribluda
026     */
027    @SuppressWarnings("serial")
028    public final class ImmutablePicoContainer implements PicoContainer, Converting, Serializable {
029    
030        private final PicoContainer delegate;
031    
032        public ImmutablePicoContainer(PicoContainer delegate) {
033            if (delegate == null) {
034                throw new NullPointerException();
035            }
036            this.delegate = delegate;
037        }
038    
039        public Object getComponent(Object componentKeyOrType) {
040            return delegate.getComponent(componentKeyOrType);
041        }
042    
043        public Object getComponent(Object componentKeyOrType, Type into) {
044            return delegate.getComponent(componentKeyOrType, into);
045        }
046    
047        public <T> T getComponent(Class<T> componentType) {
048            return delegate.getComponent(componentType);
049        }
050    
051        public <T> T getComponent(Class<T> componentType, Class<? extends Annotation> binding) {
052            return delegate.getComponent(componentType, binding);
053        }
054    
055        public List getComponents() {
056            return delegate.getComponents();
057        }
058    
059        public PicoContainer getParent() {
060            return delegate.getParent();
061        }
062    
063        public ComponentAdapter<?> getComponentAdapter(Object componentKey) {
064            return delegate.getComponentAdapter(componentKey);
065        }
066    
067        public <T> ComponentAdapter<T> getComponentAdapter(Class<T> componentType, NameBinding componentNameBinding) {
068            return delegate.getComponentAdapter(componentType, componentNameBinding);
069        }
070    
071        public <T> ComponentAdapter<T> getComponentAdapter(Class<T> componentType, Class<? extends Annotation> binding) {
072            return delegate.getComponentAdapter(componentType, binding);
073        }
074    
075        public Collection<ComponentAdapter<?>> getComponentAdapters() {
076            return delegate.getComponentAdapters();
077        }
078    
079        public <T> List<ComponentAdapter<T>> getComponentAdapters(Class<T> componentType) {
080            return delegate.getComponentAdapters(componentType);
081        }
082    
083        public <T> List<ComponentAdapter<T>> getComponentAdapters(Class<T> componentType, Class<? extends Annotation> binding) {
084            return delegate.getComponentAdapters(componentType, binding);
085        }
086    
087        public <T> List<T> getComponents(Class<T> componentType) {
088            return delegate.getComponents(componentType);
089        }
090    
091        public final void accept(PicoVisitor visitor) {
092            // don't visit "this" its pointless.
093            delegate.accept(visitor);
094        }
095    
096        public boolean equals(Object obj) {
097            return obj == this
098                   || (obj != null && obj == delegate)
099                   || (obj instanceof ImmutablePicoContainer && ((ImmutablePicoContainer) obj).delegate == delegate)
100                ;
101        }
102    
103        public int hashCode() {
104            return delegate.hashCode();
105        }
106    
107        public String toString() {
108            return "I<" + delegate.toString();
109        }
110    
111        public Converters getConverters() {
112            if (delegate instanceof Converting) {
113                return ((Converting) delegate).getConverters();
114            }
115            return new ConvertsNothing();
116        }
117    }