All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eclipse.sisu.wire.BeanProviders Maven / Gradle / Ivy

There is a newer version: 3.0.0-alpha-3
Show newest version
/*******************************************************************************
 * Copyright (c) 2010-present Sonatype, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Stuart McCulloch (Sonatype, Inc.) - initial API and implementation
 *******************************************************************************/
package org.eclipse.sisu.wire;

import java.lang.annotation.Annotation;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.eclipse.sisu.BeanEntry;
import org.eclipse.sisu.inject.BeanLocator;
import org.eclipse.sisu.inject.TypeArguments;

import com.google.inject.Binder;
import com.google.inject.Key;
import com.google.inject.Provider;
import com.google.inject.TypeLiteral;
import com.google.inject.name.Named;

/**
 * Supplies various bean {@link Provider}s backed by dynamic bean lookups.
 */
final class BeanProviders
{
    // ----------------------------------------------------------------------
    // Implementation fields
    // ----------------------------------------------------------------------

    final Provider locator;

    // ----------------------------------------------------------------------
    // Constructors
    // ----------------------------------------------------------------------

    BeanProviders( final Binder binder )
    {
        locator = binder.getProvider( BeanLocator.class );
    }

    // ----------------------------------------------------------------------
    // Public methods
    // ----------------------------------------------------------------------

    /**
     * Provides {@link Iterable} sequences of raw {@link BeanEntry}s.
     */
    public  Provider>> beanEntriesOf( final Key key )
    {
        return new Provider>>()
        {
            public Iterable> get()
            {
                return locator.get().locate( key );
            }
        };
    }

    /**
     * Provides {@link Iterable} sequences of bean/provider mappings
     */
    @SuppressWarnings( { "rawtypes", "unchecked" } )
    public  Provider>> entriesOf( final Key key )
    {
        final TypeLiteral type = key.getTypeLiteral();
        final Class clazz = type.getRawType();
        if ( javax.inject.Provider.class != clazz && com.google.inject.Provider.class != clazz )
        {
            return beanEntriesOf( key );
        }
        final Provider> beanEntries = beanEntriesOf( key.ofType( TypeArguments.get( type, 0 ) ) );
        return new Provider>>()
        {
            public Iterable> get()
            {
                return new ProviderIterableAdapter( beanEntries.get() );
            }
        };
    }

    /**
     * Provides {@link List}s of qualified beans/providers.
     */
    public  Provider> listOf( final Key key )
    {
        final Provider>> entries = entriesOf( key );
        return new Provider>()
        {
            public List get()
            {
                return new EntryListAdapter( entries.get() );
            }
        };
    }

    /**
     * Provides {@link Set}s of qualified beans/providers.
     */
    public  Provider> setOf( final Key key )
    {
        final Provider>> entries = entriesOf( key );
        return new Provider>()
        {
            public Set get()
            {
                return new EntrySetAdapter( entries.get() );
            }
        };
    }

    /**
     * Provides {@link Map}s of qualified beans/providers.
     */
    public  Provider> mapOf( final Key key )
    {
        final Provider>> entries = entriesOf( key );
        return new Provider>()
        {
            public Map get()
            {
                return new EntryMapAdapter( entries.get() );
            }
        };
    }

    /**
     * Provides string {@link Map}s of named beans/providers.
     */
    public  Provider> stringMapOf( final TypeLiteral type )
    {
        final Provider>> entries = entriesOf( Key.get( type, Named.class ) );
        return new Provider>()
        {
            public Map get()
            {
                return new EntryMapAdapter( new NamedIterableAdapter( entries.get() ) );
            }
        };
    }

    /**
     * Provides single qualified beans/providers.
     */
     Provider firstOf( final Key key )
    {
        final Provider>> beanEntries = beanEntriesOf( key );
        return new Provider()
        {
            private volatile Iterable> cachedLookup; // NOSONAR

            public V get()
            {
                if ( null == cachedLookup )
                {
                    cachedLookup = beanEntries.get();
                }
                final Iterator> itr = cachedLookup.iterator();
                return itr.hasNext() ? itr.next().getProvider().get() : null;
            }
        };
    }

    /**
     * Provides placeholder beans/providers.
     */
    public  Provider placeholderOf( final Key key )
    {
        return new PlaceholderBeanProvider( this, key );
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy