org.schmant.arg
Class AbstractArgumentInterpretationStrategy<T>

java.lang.Object
  extended by org.schmant.arg.AbstractArgumentInterpretationStrategy<T>
All Implemented Interfaces:
ArgumentInterpretationStrategy<T>
Direct Known Subclasses:
AbstractOneToOneArgumentInterpretationStrategy

public abstract class AbstractArgumentInterpretationStrategy<T>
extends Object
implements ArgumentInterpretationStrategy<T>

Since:
1.0
Author:
Karl Gustafsson

Field Summary
 
Fields inherited from interface org.schmant.arg.ArgumentInterpretationStrategy
ALLOW_NOT_INTERPRETED, ALLOW_ONE_AND_ONLY_ONE_RESULT_OBJECT
 
Constructor Summary
protected AbstractArgumentInterpretationStrategy(int[] configuration)
          Create a new argument interpreted.
 
Method Summary
protected static void checkConfiguration(int[] configuration, int[] allowedFlags)
          This method may be used by subclass constructors to validate that the configuration passed to it are valid.
protected  String convertBackslashesToSlashes(String s)
          Convert all backslashes in the string to forward slashes.
protected  File getExistingFileFromCharSequence(CharSequence cs, List<String> t)
          Try to interpret the value of the CharSequence as an existing File.
protected  File getParentFile(File f)
          Get the file's parent directory.
protected abstract  Class<T> getTargetType()
          Subclasses implement this to return the type that they interpret objects as.
protected  void trace(List<String> trace, String msg)
          Log an interpretation decision to the trace (FINEST) level and add it to the interpretation trace list.
 void validateResult(ArgumentInterpretationResult<T> result)
          This method checks that the result is valid in respect to the values of the ArgumentInterpretationStrategy.ALLOW_NOT_INTERPRETED and ArgumentInterpretationStrategy.ALLOW_ONE_AND_ONLY_ONE_RESULT_OBJECT flags used when creating this object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.schmant.arg.ArgumentInterpretationStrategy
interpretList
 

Constructor Detail

AbstractArgumentInterpretationStrategy

protected AbstractArgumentInterpretationStrategy(int[] configuration)
Create a new argument interpreted.

Parameters:
configuration - Configuration flags. This object understands the flags ArgumentInterpretationStrategy.ALLOW_NOT_INTERPRETED and ArgumentInterpretationStrategy.ALLOW_ONE_AND_ONLY_ONE_RESULT_OBJECT.
Method Detail

checkConfiguration

protected static void checkConfiguration(int[] configuration,
                                         int[] allowedFlags)
                                  throws IllegalArgumentException
This method may be used by subclass constructors to validate that the configuration passed to it are valid.

Parameters:
configuration - The configuration flags that it got.
allowedFlags - The configuration flags that it understands.
Throws:
IllegalArgumentException - If it got a configuration flag that it did not understand.

getTargetType

protected abstract Class<T> getTargetType()
Subclasses implement this to return the type that they interpret objects as.

Returns:
The target type of the interpretation.

validateResult

public void validateResult(ArgumentInterpretationResult<T> result)
                    throws ArgumentInterpretationException
This method checks that the result is valid in respect to the values of the ArgumentInterpretationStrategy.ALLOW_NOT_INTERPRETED and ArgumentInterpretationStrategy.ALLOW_ONE_AND_ONLY_ONE_RESULT_OBJECT flags used when creating this object.

Specified by:
validateResult in interface ArgumentInterpretationStrategy<T>
Parameters:
result - The result object.
Throws:
ArgumentInterpretationException - If the result is invalid.

trace

protected void trace(List<String> trace,
                     String msg)
Log an interpretation decision to the trace (FINEST) level and add it to the interpretation trace list. Subclasses are encouraged to call this method when they do interpretation decisions.

Parameters:
trace - A list with trace messages that is built as they are logged. (The logged message is appended to the list by this method.) The messages are saved so that they can be included in the ArgumentInterpretationException that may be thrown if the interpretation fails. This argument may be null.
msg - The message to log and perhaps include in the interpretation trace list.

getParentFile

protected File getParentFile(File f)
Get the file's parent directory.

Parameters:
f - The file.
Returns:
The file's parent directory.

convertBackslashesToSlashes

protected String convertBackslashesToSlashes(String s)
Convert all backslashes in the string to forward slashes.

Parameters:
s - The string to convert.
Returns:
The converted string.

getExistingFileFromCharSequence

protected File getExistingFileFromCharSequence(CharSequence cs,
                                               List<String> t)
Try to interpret the value of the CharSequence as an existing File. If the text is the path to a file, that file is returned. If it is an URI string, the file referenced by that URI is returned.

Parameters:
cs - The text to interpret
t - The interpretation trace list.
Returns:
A file or null if the text could not be interpreted as an existing file.