org.eclipse.sisu.wire.BeanProviders Maven / Gradle / Ivy
/*******************************************************************************
* 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 extends BeanEntry> 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