package info.ebenoit.ebul.cmp; /** * This class carries information about a dependency's target. Dependencies may be specified by name or by type. * * @author E. BenoƮt */ public class DependencyInfo { /** * The name of the component being depended upon, or null if the dependency information is based on * type. */ private final String name; /** * The type of the component being depended upon, or null if the dependency information is based on the * name. */ private final Class< ? > klass; /** * Creates a name-based dependency information record * * @param name * the name of the component being depended upon */ public DependencyInfo( final String name ) { this.name = name; this.klass = null; } /** * Creates a type-based dependency information record * * @param klass * the type of the component being depended upon * @throws ComponentDefinitionException * if the specified type is not supported (e.g. primitives or arrays) */ public DependencyInfo( final Class< ? > klass ) throws ComponentDefinitionException { this.name = null; this.klass = klass; if ( klass.isPrimitive( ) ) { throw new ComponentDefinitionException( "specified dependency is a primitive type" ); } if ( klass.isArray( ) ) { throw new ComponentDefinitionException( "specified dependency is an array type" ); } } @Override public int hashCode( ) { final int prime = 31; int result = 1; result = prime * result + ( this.klass == null ? 0 : this.klass.hashCode( ) ); result = prime * result + ( this.name == null ? 0 : this.name.hashCode( ) ); return result; } @Override public boolean equals( final Object obj ) { if ( this == obj ) { return true; } if ( obj == null ) { return false; } if ( this.getClass( ) != obj.getClass( ) ) { return false; } final DependencyInfo other = (DependencyInfo) obj; if ( this.klass == null ) { if ( other.klass != null ) { return false; } return this.name.equals( other.name ); } else if ( this.klass != other.klass ) { return false; } return true; } /** * @return the name of the component being depended upon, or null if the dependency information is * based on type. */ public String getTargetName( ) { return this.name; } /** * @return the type of the component being depended upon, or null if the dependency information is * based on the name. */ public Class< ? > getTargetClass( ) { return this.klass; } }