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 Leo Simmons & Jörg Schaible                         *
009     *****************************************************************************/
010    package org.picocontainer.gems.adapters;
011    
012    import org.picocontainer.PicoContainer;
013    import org.picocontainer.PicoCompositionException;
014    import org.picocontainer.PicoVerificationException;
015    import org.picocontainer.adapters.AbstractAdapter;
016    
017    import java.lang.reflect.Type;
018    
019    
020    /**
021     * Component adapter that wrapps a static factory with the help of {@link StaticFactory}.
022     *
023     * @author Jörg Schaible
024     * @author Leo Simmons
025     */
026    @SuppressWarnings("serial")
027    public final class StaticFactoryAdapter<T> extends AbstractAdapter<T> {
028        
029            
030            private final StaticFactory<T> staticFactory;
031    
032        /**
033         * Construct a ComponentAdapter accessing a static factory creating the component.
034         *
035         * @param type The type of the created component.
036         * @param staticFactory Wrapper instance for the static factory.
037         */
038        public StaticFactoryAdapter(final Class<T> type, final StaticFactory<T> staticFactory) {
039    
040            this(type, type, staticFactory);
041        }
042    
043        /**
044         * Construct a ComponentAdapter accessing a static factory creating the component using a special key for addComponent
045         * registration.
046         *
047         * @param componentKey The key of the created component.
048         * @param type The type of the created component.
049         * @param staticFactory Wrapper instance for the static factory.
050         */
051        public StaticFactoryAdapter(final Object componentKey, final Class<T> type, final StaticFactory<T> staticFactory) {
052            super(componentKey, type);
053            this.staticFactory = staticFactory;
054        }
055    
056        /**
057         * @return Returns the component created by the static factory.
058         * @see org.picocontainer.ComponentAdapter#getComponentInstance(org.picocontainer.PicoContainer, java.lang.Class into)
059         */
060        public T getComponentInstance(final PicoContainer container, final Type into) throws PicoCompositionException {
061            return staticFactory.get();
062        }
063    
064        /**
065         * {@inheritDoc}
066         *
067         * @see org.picocontainer.ComponentAdapter#verify(org.picocontainer.PicoContainer)
068         */
069        public void verify(final PicoContainer container) throws PicoVerificationException {
070        }
071    
072        public String getDescriptor() {
073            return "StaticFactory";
074        }
075    }