2009-05-04 08:11:42 +08:00
|
|
|
package org.broadinstitute.sting.utils.cmdLine;
|
|
|
|
|
|
|
|
|
|
import java.lang.reflect.Field;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Created by IntelliJ IDEA.
|
|
|
|
|
* User: mhanna
|
|
|
|
|
* Date: May 3, 2009
|
|
|
|
|
* Time: 6:02:04 PM
|
|
|
|
|
* <p/>
|
|
|
|
|
* The Broad Institute
|
|
|
|
|
* SOFTWARE COPYRIGHT NOTICE AGREEMENT
|
|
|
|
|
* This software and its documentation are copyright 2009 by the
|
|
|
|
|
* Broad Institute/Massachusetts Institute of Technology. All rights are reserved.
|
|
|
|
|
* <p/>
|
|
|
|
|
* This software is supplied without any warranty or guaranteed support whatsoever. Neither
|
|
|
|
|
* the Broad Institute nor MIT can be responsible for its use, misuse, or functionality.
|
|
|
|
|
*/
|
|
|
|
|
/**
|
|
|
|
|
* A collection of argument definitions.
|
|
|
|
|
*/
|
|
|
|
|
class ArgumentDefinitions {
|
|
|
|
|
/**
|
2009-05-05 06:41:23 +08:00
|
|
|
* Backing data set of argument stored by short name and long name.
|
2009-05-04 08:11:42 +08:00
|
|
|
*/
|
|
|
|
|
private Map<String,ArgumentDefinition> argumentsByShortName = new HashMap<String,ArgumentDefinition>();
|
2009-05-05 06:41:23 +08:00
|
|
|
private Map<String,ArgumentDefinition> argumentsByLongName = new HashMap<String,ArgumentDefinition>();
|
2009-05-04 08:11:42 +08:00
|
|
|
|
|
|
|
|
/**
|
2009-05-05 06:41:23 +08:00
|
|
|
* Returns the argument with the given short name.
|
|
|
|
|
* @param shortName Argument short name.
|
|
|
|
|
* @return The argument definition, or null if nothing matches.
|
2009-05-04 08:11:42 +08:00
|
|
|
*/
|
2009-05-05 06:41:23 +08:00
|
|
|
public ArgumentDefinition getArgumentWithShortName( String shortName ) {
|
|
|
|
|
return argumentsByShortName.get( shortName );
|
2009-05-04 08:11:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the argument with the given short name.
|
2009-05-05 06:41:23 +08:00
|
|
|
* @param longName Argument long name.
|
2009-05-04 08:11:42 +08:00
|
|
|
* @return The argument definition, or null if nothing matches.
|
|
|
|
|
*/
|
2009-05-05 06:41:23 +08:00
|
|
|
public ArgumentDefinition getArgumentWithLongName( String longName ) {
|
|
|
|
|
return argumentsByLongName.get( longName );
|
2009-05-04 08:11:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Adds an argument to the this argument definition list.
|
|
|
|
|
* @param argument The argument to add.
|
|
|
|
|
* @param sourceClass Class where the argument was defined.
|
|
|
|
|
* @param sourceField Field in which the argument was defined.
|
|
|
|
|
*/
|
|
|
|
|
public void add( Argument argument, Class sourceClass, Field sourceField ) {
|
2009-05-05 06:41:23 +08:00
|
|
|
ArgumentDefinition definition = new ArgumentDefinition( argument, sourceClass, sourceField );
|
|
|
|
|
String fullName = argument.fullName().trim();
|
|
|
|
|
String shortName = argument.shortName().trim();
|
|
|
|
|
|
|
|
|
|
if( fullName.length() == 0 )
|
|
|
|
|
throw new IllegalArgumentException( "Argument cannot have 0-length fullname." );
|
|
|
|
|
|
|
|
|
|
argumentsByLongName.put( fullName, definition );
|
|
|
|
|
if( shortName.length() != 0 )
|
|
|
|
|
argumentsByShortName.put( shortName, definition );
|
2009-05-04 08:11:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A specific argument definition. Maps one-to-one with a field in some class.
|
|
|
|
|
*/
|
|
|
|
|
class ArgumentDefinition {
|
|
|
|
|
public final Argument argument;
|
|
|
|
|
public final Class sourceClass;
|
|
|
|
|
public final Field sourceField;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a new argument definition.
|
|
|
|
|
* @param argument Attributes of the argument, read from the source field.
|
|
|
|
|
* @param sourceClass Source class for the argument, provided to the ParsingEngine.
|
|
|
|
|
* @param sourceField Source field for the argument, extracted from the sourceClass.
|
|
|
|
|
*/
|
|
|
|
|
public ArgumentDefinition( Argument argument, Class sourceClass, Field sourceField ) {
|
|
|
|
|
this.argument = argument;
|
|
|
|
|
this.sourceClass = sourceClass;
|
|
|
|
|
this.sourceField = sourceField;
|
|
|
|
|
}
|
2009-05-05 06:41:23 +08:00
|
|
|
}
|
2009-05-04 08:11:42 +08:00
|
|
|
|
2009-05-05 06:41:23 +08:00
|
|
|
/**
|
|
|
|
|
* A general purpose accessor interface for ArgumentDefinitions.
|
|
|
|
|
*/
|
|
|
|
|
interface DefinitionMatcher {
|
|
|
|
|
ArgumentDefinition get( ArgumentDefinitions argumentDefinitions, String key );
|
2009-05-04 08:11:42 +08:00
|
|
|
}
|
2009-05-05 06:41:23 +08:00
|
|
|
|
|
|
|
|
class FullNameDefinitionMatcher implements DefinitionMatcher {
|
|
|
|
|
public ArgumentDefinition get( ArgumentDefinitions argumentDefinitions, String key ) {
|
|
|
|
|
return argumentDefinitions.getArgumentWithLongName( key );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class ShortNameDefinitionMatcher implements DefinitionMatcher {
|
|
|
|
|
public ArgumentDefinition get( ArgumentDefinitions argumentDefinitions, String key ) {
|
|
|
|
|
return argumentDefinitions.getArgumentWithShortName( key );
|
|
|
|
|
}
|
|
|
|
|
}
|