001    package org.picocontainer.containers;
002    
003    import org.picocontainer.ComponentAdapter;
004    import org.picocontainer.Converters;
005    import org.picocontainer.Converting;
006    import org.picocontainer.NameBinding;
007    import org.picocontainer.PicoContainer;
008    import org.picocontainer.PicoException;
009    import org.picocontainer.PicoVisitor;
010    
011    import java.io.Serializable;
012    import java.lang.annotation.Annotation;
013    import java.lang.reflect.Type;
014    import java.util.Collection;
015    import java.util.List;
016    
017    /**
018     * Abstract base class for <i>immutable<i> delegation to a PicoContainer
019     * 
020     * @author Konstantin Pribluda
021     * 
022     */
023    @SuppressWarnings("serial")
024    public abstract class AbstractDelegatingPicoContainer implements PicoContainer, Converting, Serializable{
025    
026        private PicoContainer delegate;
027    
028        public AbstractDelegatingPicoContainer(PicoContainer delegate) {
029                    if (delegate == null) {
030                            throw new NullPointerException(
031                                            "PicoContainer delegate must not be null");
032                    }
033                    this.delegate = delegate;
034            }
035    
036            public final void accept(PicoVisitor visitor) {
037            visitor.visitContainer(this);
038            delegate.accept(visitor);
039            }
040    
041    
042            @Override
043        public boolean equals(Object obj) {
044                    // required to make it pass on both jdk 1.3 and jdk 1.4. Btw, what about
045                    // overriding hashCode()? (AH)
046                    return delegate.equals(obj) || this == obj;
047            }
048    
049            public <T> T getComponent(Class<T> componentType) {
050                    return componentType.cast(getComponent((Object) 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 Object getComponent(Object componentKeyOrType) {
058                    return delegate.getComponent(componentKeyOrType);
059            }
060    
061        public Object getComponent(Object componentKeyOrType, Type into) {
062            return delegate.getComponent(componentKeyOrType, into);
063        }
064    
065        public <T> ComponentAdapter<T> getComponentAdapter(Class<T> componentType,
066                            NameBinding componentNameBinding) {
067                    return delegate.getComponentAdapter(componentType,
068                                                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 ComponentAdapter<?> getComponentAdapter(Object componentKey) {
076                    return delegate.getComponentAdapter(componentKey);
077            }
078    
079            public Collection<ComponentAdapter<?>> getComponentAdapters() {
080                    return delegate.getComponentAdapters();
081            }
082    
083            public <T> List<ComponentAdapter<T>> getComponentAdapters(
084                            Class<T> componentType) {
085                    return delegate.getComponentAdapters(componentType);
086            }
087    
088        public <T> List<ComponentAdapter<T>> getComponentAdapters(Class<T> componentType, Class<? extends Annotation> binding) {
089            return delegate.getComponentAdapters(componentType, binding);
090        }    
091    
092        public List<Object> getComponents() {
093                    return delegate.getComponents();
094            }
095    
096            public <T> List<T> getComponents(Class<T> type) throws PicoException {
097                    return delegate.getComponents(type);
098            }
099    
100            public PicoContainer getDelegate() {
101                    return delegate;
102            }
103    
104            public PicoContainer getParent() {
105                    return delegate.getParent();
106            }
107        
108        @Override
109        public String toString() {
110            return "D<" + delegate.toString();
111        }
112    
113        public Converters getConverters() {
114            if (delegate instanceof Converting) {
115                return ((Converting) delegate).getConverters();
116            } else {
117                return null;
118            }
119        }
120    }