Components registry - Component / component state query methods

This commit is contained in:
Emmanuel BENOîT 2015-09-15 11:33:30 +02:00
parent 59f28483dd
commit d554f02ede

View file

@ -3,7 +3,9 @@ package info.ebenoit.ebul.cmp;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@ -48,6 +50,98 @@ public class ComponentRegistry
}
public ComponentState getState( String name )
{
return this.byName.get( name );
}
public ComponentState getState( Class< ? > type )
throws AmbiguousComponentException
{
ArrayList< ComponentState > l = this.byType.get( type );
if ( l == null ) {
return null;
}
if ( l.size( ) == 1 ) {
return l.get( 0 );
}
throw new AmbiguousComponentException( type , l.size( ) );
}
public List< ComponentState > getStates( Class< ? > type )
throws AmbiguousComponentException
{
ArrayList< ComponentState > l = this.byType.get( type );
if ( l == null ) {
return Collections.emptyList( );
}
return Collections.unmodifiableList( l );
}
public void getStates( Class< ? > type , Collection< ComponentState > states )
{
ArrayList< ComponentState > l = this.byType.get( type );
if ( l != null ) {
int nFound = l.size( );
for ( int i = 0 ; i < nFound ; i++ ) {
states.add( l.get( i ) );
}
}
}
public Object get( String name )
{
ComponentState cs = this.byName.get( name );
return cs == null ? null : cs.getComponent( );
}
public < T > T get( Class< T > type )
throws AmbiguousComponentException
{
ArrayList< ComponentState > l = this.byType.get( type );
if ( l == null ) {
return null;
}
if ( l.size( ) == 1 ) {
return type.cast( l.get( 0 ).getComponent( ) );
}
throw new AmbiguousComponentException( type , l.size( ) );
}
public < T > List< T > getAll( Class< T > type )
{
ArrayList< ComponentState > l = this.byType.get( type );
if ( l == null ) {
return Collections.emptyList( );
}
int nFound = l.size( );
ArrayList< T > result = new ArrayList< >( nFound );
for ( int i = 0 ; i < nFound ; i++ ) {
result.add( type.cast( l.get( i ).getComponent( ) ) );
}
return result;
}
public < T > void getAll( Class< T > type , Collection< T > result )
{
ArrayList< ComponentState > l = this.byType.get( type );
if ( l != null ) {
int nFound = l.size( );
for ( int i = 0 ; i < nFound ; i++ ) {
result.add( type.cast( l.get( i ).getComponent( ) ) );
}
}
}
public ComponentRegistry register( Collection< NewComponentInfo< ? > > components )
throws ComponentCreationException , DuplicateComponentException , RecursiveDependenciesException ,
DependencyInjectionException , AmbiguousComponentException