Skip to content

Commit

Permalink
task: redefine Parameter with generics
Browse files Browse the repository at this point in the history
The Parameter class is now, ahem, parameterised with a type <T> giving
the class of the values it obtains.  This is generally sensible,
and allows various automations based on the parameter type which were
not possible before.  Runtime pluggability can be much improved:
string values can now as a matter of course (but optionally)
be classnames of classes with the right superclass and no-arg
constructors.  It also allows an Environment to accept
typed values as an alternative to string values.

At the same time, rework the way the Parameter class was written.
It was pretty nasty, making subclassing error-prone and unpleasant.
Now you usually just have to implement an abstract method
stringToObject.

Parameter is now an abstract superclass, with StringParameter replacing
it if the value you want is a String.

Since the TASK API has changed, code in TTOOLS and (the moribund)
NDTOOLS packages also needs to be changed.  NDTOOLS builds and
should be OK, but I haven't tested it.  There ought not to be
user-visible changes to TTOOLS, and the unit tests are fairly
extensive, but quite a bit of code has changed so nasty surprises
are possible.  In particular null parameter values are handled
slightly differently, so watch out for new NullPointerExceptions.
  • Loading branch information
mbtaylor committed Aug 21, 2014
1 parent a99a465 commit d5103f1
Show file tree
Hide file tree
Showing 83 changed files with 1,468 additions and 1,472 deletions.
20 changes: 10 additions & 10 deletions ndtools/src/main/uk/ac/starlink/ndtools/ExistingNdxParameter.java
Expand Up @@ -11,14 +11,13 @@
/**
* Parameter representing an Ndx object which already exists.
*/
class ExistingNdxParameter extends Parameter {
class ExistingNdxParameter extends Parameter<Ndx> {

private NdxIO ndxio = new NdxIO();
private Ndx ndxvalue;
private AccessMode accessMode = AccessMode.READ;

public ExistingNdxParameter( String name ) {
super( name );
super( name, Ndx.class, true );
}

/**
Expand All @@ -40,25 +39,26 @@ public void setAccessMode( AccessMode mode ) {
* Get the value of the parameter as an Ndx object.
*/
public Ndx ndxValue( Environment env ) throws TaskException {
checkGotValue( env );
return ndxvalue;
return objectValue( env );
}

public void setValueFromString( Environment env, String stringval )
public Ndx stringToObject( Environment env, String stringval )
throws TaskException {
String loc = stringval;
Ndx ndx;
try {
ndxvalue = ndxio.makeNdx( loc, accessMode );
ndx = ndxio.makeNdx( loc, accessMode );
}
catch ( Exception e ) {
throw new ParameterValueException(
this, "Failed to read NDX from " + loc, e );
}
if ( ndxvalue == null ) {
if ( ndx == null ) {
throw new ParameterValueException(
this, "Unknown NDX type " + loc );
}
super.setValueFromString( env, stringval );
else {
return ndx;
}
}

}
6 changes: 2 additions & 4 deletions ndtools/src/main/uk/ac/starlink/ndtools/NewNdxParameter.java
Expand Up @@ -5,14 +5,14 @@
import uk.ac.starlink.ndx.Ndx;
import uk.ac.starlink.ndx.NdxIO;
import uk.ac.starlink.task.Environment;
import uk.ac.starlink.task.Parameter;
import uk.ac.starlink.task.StringParameter;
import uk.ac.starlink.task.ParameterValueException;
import uk.ac.starlink.task.TaskException;

/**
* Parameter representing a new Ndx object ready for writing.
*/
class NewNdxParameter extends Parameter {
class NewNdxParameter extends StringParameter {

private NdxIO ndxio = new NdxIO();

Expand All @@ -31,7 +31,6 @@ public NewNdxParameter( String name ) {
*/
public Ndx getOutputNdx( Environment env, Ndx template )
throws TaskException {
checkGotValue( env );
String loc = stringValue( env );
try {
if ( ndxio.makeBlankNdx( loc, template ) ) {
Expand All @@ -58,7 +57,6 @@ public Ndx getOutputNdx( Environment env, Ndx template )
*/
public NdxConsumer ndxConsumerValue( final Environment env )
throws TaskException {
checkGotValue( env );
final String loc = stringValue( env );
return new NdxConsumer() {
public void consume( Ndx ndx ) throws IOException {
Expand Down
20 changes: 5 additions & 15 deletions ndtools/src/main/uk/ac/starlink/ndtools/ShapeParameter.java
Expand Up @@ -9,31 +9,21 @@
/**
* Parameter representing an N-dimensional shape.
*/
class ShapeParameter extends Parameter {

private NDShape shape;
class ShapeParameter extends Parameter<NDShape> {

public ShapeParameter( String name ) {
super( name );
super( name, NDShape.class, false );
}

public void setValueFromString( Environment env, String stringval )
throws TaskException {
try {
shape = NDShape.fromString( stringval );
}
catch ( IllegalArgumentException e ) {
throw new ParameterValueException( this, e.getMessage() );
}
super.setValueFromString( env, stringval );
public NDShape stringToObject( Environment env, String stringval ) {
return NDShape.fromString( stringval );
}


/**
* Gets the value of this parameter as an NDShape object.
*/
public NDShape shapeValue( Environment env ) throws TaskException {
checkGotValue( env );
return shape;
return objectValue( env );
}
}
32 changes: 5 additions & 27 deletions ndtools/src/main/uk/ac/starlink/ndtools/TypeParameter.java
Expand Up @@ -3,8 +3,7 @@
import java.util.Iterator;
import uk.ac.starlink.array.Type;
import uk.ac.starlink.task.Environment;
import uk.ac.starlink.task.Parameter;
import uk.ac.starlink.task.ParameterValueException;
import uk.ac.starlink.task.ChoiceParameter;
import uk.ac.starlink.task.TaskException;

/**
Expand All @@ -13,38 +12,17 @@
*
* @see uk.ac.starlink.array.Type
*/
class TypeParameter extends Parameter {

private Type typeval;
class TypeParameter extends ChoiceParameter<Type> {

public TypeParameter( String name ) {
super( name );
super( name, Type.class,
(Type[]) Type.allTypes().toArray( new Type[ 0 ] ) );
}

/**
* Returns the numeric primitive Type represented by this parameter.
*/
public Type typeValue( Environment env ) throws TaskException {
checkGotValue( env );
return typeval;
}

public void setValueFromString( Environment env, String stringval )
throws TaskException {
String typename = stringval;
for ( Iterator it = Type.allTypes().iterator(); it.hasNext(); ) {
Type type = (Type) it.next();
if ( typename.equalsIgnoreCase( type.toString() ) ) {
typeval = type;
super.setValueFromString( env, stringval );
}
}

/* Didn't find one. */
StringBuffer sb = new StringBuffer( "Known types are:" );
for ( Iterator it = Type.allTypes().iterator(); it.hasNext(); ) {
sb.append( " " ).append( it.next().toString() );
}
throw new ParameterValueException( this, sb.toString() );
return objectValue( env );
}
}
21 changes: 10 additions & 11 deletions task/src/main/uk/ac/starlink/task/BooleanParameter.java
Expand Up @@ -7,29 +7,29 @@
* @author Mark Taylor
* @since 9 Aug 2005
*/
public class BooleanParameter extends Parameter {

private boolean booleanVal_;
public class BooleanParameter extends Parameter<Boolean> {

/**
* Constructs a new boolean parameter.
*
* @param name parameter name
*/
public BooleanParameter( String name ) {
super( name );
super( name, Boolean.class, false );
setUsage( "true|false" );
setNullPermitted( false );
}

/**
* Returns the value of this parameter as a boolean.
*
* @param env execution environment
* @return boolean value
* @throws NullPointerException if the value is null, only possible
* if isNullPermitted is true (not by default)
*/
public boolean booleanValue( Environment env ) throws TaskException {
checkGotValue( env );
return booleanVal_;
return objectValue( env ).booleanValue();
}

/**
Expand All @@ -41,20 +41,19 @@ public void setDefault( boolean dflt ) {
setDefault( dflt ? "true" : "false" );
}

public void setValueFromString( Environment env, String stringval )
throws TaskException {
public Boolean stringToObject( Environment env, String stringval )
throws ParameterValueException {
if ( "TRUE".equalsIgnoreCase( stringval ) ||
"YES".equalsIgnoreCase( stringval ) ) {
booleanVal_ = true;
return Boolean.TRUE;
}
else if ( "FALSE".equalsIgnoreCase( stringval ) ||
"NO".equalsIgnoreCase( stringval ) ) {
booleanVal_ = false;
return Boolean.FALSE;
}
else {
throw new ParameterValueException( this, stringval +
" is not true/false/yes/no" );
}
super.setValueFromString( env, stringval );
}
}

0 comments on commit d5103f1

Please sign in to comment.