* \r
* @author pvtroshin\r
* \r
- * Date October 2009\r
+ * @version 1.0 October 2009\r
* @param <T>\r
* type of executable\r
*/\r
@XmlAccessorType(XmlAccessType.FIELD)\r
public class Option<T> implements Argument<T> {\r
\r
- @XmlElement(required = true)\r
- protected String description;\r
-\r
- @XmlElement(required = true)\r
- Set<String> optionNames = new HashSet<String>();\r
-\r
- @XmlElement(required = true)\r
- protected String name;\r
-\r
- @XmlAttribute\r
- protected boolean isRequired;\r
- @XmlElement\r
- protected URL furtherDetails;\r
- @XmlElement\r
- protected String defaultValue;\r
-\r
- Option() {\r
- // Has to have no arg constructor for JAXB\r
- }\r
-\r
- public Option(String name, String description) {\r
- this.name = name;\r
- this.description = description;\r
- }\r
-\r
- /**\r
- * Human readable name of the option\r
- */\r
- public String getName() {\r
- return name;\r
- }\r
-\r
- public void setName(String name) {\r
- this.name = name;\r
- }\r
-\r
- /**\r
- * A long description of the Option\r
- */\r
- public String getDescription() {\r
- return description;\r
- }\r
-\r
- public void setDescription(String description) {\r
- this.description = description;\r
- }\r
-\r
- /**\r
- * The URL where further details about the option can be found\r
- */\r
- public URL getFurtherDetails() {\r
- return furtherDetails;\r
- }\r
-\r
- public void setFurtherDetails(URL furtherDetails) {\r
- this.furtherDetails = furtherDetails;\r
- }\r
-\r
- /**\r
- * A default value of the option. Defaults to command line argument name\r
- * e.g. -auto\r
- */\r
- public String getDefaultValue() {\r
- return defaultValue;\r
- }\r
-\r
- /**\r
- * Sets one of the values defined in optionList as default. Attempting set\r
- * the value not listed there will result in WrongParameter exception\r
- * \r
- * @param defaultVal\r
- * @throws WrongParameterException\r
- * is thrown if the defaultValue is not found in optionList\r
- */\r
- public void setDefaultValue(String defaultVal)\r
- throws WrongParameterException {\r
- if (optionNames.isEmpty()) {\r
- throw new IllegalStateException("Please define optionNames first!");\r
+ @XmlElement(required = true)\r
+ protected String description;\r
+\r
+ @XmlElement(required = true)\r
+ Set<String> optionNames = new HashSet<String>();\r
+\r
+ @XmlElement(required = true)\r
+ protected String name;\r
+\r
+ @XmlAttribute\r
+ protected boolean isRequired;\r
+ @XmlElement\r
+ protected URL furtherDetails;\r
+ @XmlElement\r
+ protected String defaultValue;\r
+\r
+ Option() {\r
+ // Has to have no arg constructor for JAXB\r
}\r
- if (!valueExist(defaultVal, getOptionNames())) {\r
- throw new WrongParameterException(\r
- "Attempting to set illegal defaultValue '" + defaultVal\r
- + "' which is not defined optionNames for option: "\r
- + this);\r
+\r
+ public Option(String name, String description) {\r
+ this.name = name;\r
+ this.description = description;\r
}\r
- this.defaultValue = defaultVal;\r
- }\r
\r
- static boolean valueExist(String testValue, List<String> values) {\r
- assert !Util.isEmpty(testValue);\r
- for (String val : values) {\r
- if (testValue.equalsIgnoreCase(val)) {\r
- return true;\r
- }\r
+ /**\r
+ * Human readable name of the option\r
+ */\r
+ public String getName() {\r
+ return name;\r
}\r
- return false;\r
- }\r
-\r
- /**\r
- * Flag that indicated that this option must be specified in the command\r
- * line for an executable to run\r
- * \r
- * @return true is the option is required, false otherwise\r
- */\r
- public boolean isRequired() {\r
- return isRequired;\r
- }\r
-\r
- public void setRequired(boolean isRequired) {\r
- this.isRequired = isRequired;\r
- }\r
-\r
- /**\r
- * \r
- * @return List of option names\r
- */\r
- public List<String> getOptionNames() {\r
- return new ArrayList<String>(optionNames);\r
- }\r
-\r
- public void setOptionNames(Set<String> optionNames) {\r
- this.optionNames = new HashSet<String>(optionNames);\r
- }\r
-\r
- /**\r
- * Adds an option to the optionName list\r
- * \r
- * @param value\r
- * @return modified optionName list\r
- */\r
- public Set<String> addOptionNames(String... value) {\r
- for (String v : value) {\r
- boolean added = this.optionNames.add(v);\r
- assert added : "Duplicated optionName is detected!";\r
+\r
+ public void setName(String name) {\r
+ this.name = name;\r
}\r
- return this.optionNames;\r
- }\r
-\r
- @Override\r
- public String toString() {\r
- String value = "Option name: " + this.name + SysPrefs.newlinechar;\r
- value += "Description: " + this.description + SysPrefs.newlinechar;\r
- if (!Util.isEmpty(defaultValue)) {\r
- value += "Default value: " + this.defaultValue\r
- + SysPrefs.newlinechar;\r
+\r
+ /**\r
+ * A long description of the Option\r
+ */\r
+ public String getDescription() {\r
+ return description;\r
}\r
- value += "URL: " + this.furtherDetails + SysPrefs.newlinechar;\r
- value += "Is required: " + this.isRequired + SysPrefs.newlinechar;\r
- if (!this.optionNames.isEmpty()) {\r
- Set<String> sortedPosval = new TreeSet<String>(this.optionNames);\r
- value += "Option Names: " + SysPrefs.newlinechar;\r
- for (String val : sortedPosval) {\r
- value += val + SysPrefs.newlinechar;\r
- }\r
+\r
+ public void setDescription(String description) {\r
+ this.description = description;\r
}\r
- return value;\r
- }\r
-\r
- /**\r
- * Convert the option to the command string.\r
- * \r
- * @return If only one optionName is defined, than it is returned, if many\r
- * option names are defined, then the defaultValue is returned.\r
- * Option must have a default value if there are many optionNames to\r
- * be valid.\r
- */\r
- public String toCommand(String nameValueSeparator) {\r
- if (optionNames.size() == 1) {\r
- return optionNames.iterator().next();\r
+\r
+ /**\r
+ * The URL where further details about the option can be found\r
+ */\r
+ public URL getFurtherDetails() {\r
+ return furtherDetails;\r
}\r
- return getDefaultValue();\r
- }\r
\r
- @Override\r
- public boolean equals(Object obj) {\r
- if (obj == null) {\r
- return false;\r
+ public void setFurtherDetails(URL furtherDetails) {\r
+ this.furtherDetails = furtherDetails;\r
}\r
- Option<?> objArg = null;\r
- if (obj instanceof Option<?>) {\r
- objArg = (Option<?>) obj;\r
- } else {\r
- return false;\r
+\r
+ /**\r
+ * A default value of the option. Defaults to command line argument name\r
+ * e.g. -auto\r
+ */\r
+ public String getDefaultValue() {\r
+ return defaultValue;\r
}\r
- if (!Util.isEmpty(objArg.name) && !Util.isEmpty(name)) {\r
- if (!objArg.name.equals(this.name)) {\r
- return false;\r
- }\r
+\r
+ /**\r
+ * Sets one of the values defined in optionList as default. Attempting set\r
+ * the value not listed there will result in WrongParameter exception\r
+ * \r
+ * @param defaultVal\r
+ * @throws WrongParameterException\r
+ * is thrown if the defaultValue is not found in optionList\r
+ */\r
+ public void setDefaultValue(String defaultVal)\r
+ throws WrongParameterException {\r
+ if (optionNames.isEmpty()) {\r
+ throw new IllegalStateException("Please define optionNames first!");\r
+ }\r
+ if (!valueExist(defaultVal, getOptionNames())) {\r
+ throw new WrongParameterException(\r
+ "Attempting to set illegal defaultValue '" + defaultVal\r
+ + "' which is not defined optionNames for option: "\r
+ + this);\r
+ }\r
+ this.defaultValue = defaultVal;\r
}\r
- if (!Util.isEmpty(objArg.description) && !Util.isEmpty(description)) {\r
- if (!objArg.description.equals(this.description)) {\r
+\r
+ static boolean valueExist(String testValue, List<String> values) {\r
+ assert !Util.isEmpty(testValue);\r
+ for (String val : values) {\r
+ if (testValue.equalsIgnoreCase(val)) {\r
+ return true;\r
+ }\r
+ }\r
return false;\r
- }\r
}\r
- if (objArg.isRequired != this.isRequired) {\r
- return false;\r
+\r
+ /**\r
+ * Flag that indicated that this option must be specified in the command\r
+ * line for an executable to run\r
+ * \r
+ * @return true is the option is required, false otherwise\r
+ */\r
+ public boolean isRequired() {\r
+ return isRequired;\r
}\r
- if (!Util.isEmpty(objArg.defaultValue) && !Util.isEmpty(defaultValue)) {\r
- if (!objArg.defaultValue.equals(this.defaultValue)) {\r
- return false;\r
- }\r
+\r
+ public void setRequired(boolean isRequired) {\r
+ this.isRequired = isRequired;\r
+ }\r
+\r
+ /**\r
+ * \r
+ * @return List of option names\r
+ */\r
+ public List<String> getOptionNames() {\r
+ return new ArrayList<String>(optionNames);\r
}\r
- if (objArg.optionNames.size() != this.optionNames.size()) {\r
- return false;\r
+\r
+ public void setOptionNames(Set<String> optionNames) {\r
+ this.optionNames = new HashSet<String>(optionNames);\r
+ }\r
+\r
+ /**\r
+ * Adds an option to the optionName list\r
+ * \r
+ * @param value\r
+ * @return modified optionName list\r
+ */\r
+ public Set<String> addOptionNames(String... value) {\r
+ for (String v : value) {\r
+ boolean added = this.optionNames.add(v);\r
+ assert added : "Duplicated optionName is detected!";\r
+ }\r
+ return this.optionNames;\r
}\r
- int matchCount = 0;\r
- for (String oname : objArg.optionNames) {\r
- if (Util.isEmpty(oname)) {\r
- continue;\r
- }\r
- for (String thisoname : this.optionNames) {\r
- if (oname.equals(thisoname)) {\r
- matchCount++;\r
- break;\r
+\r
+ @Override\r
+ public String toString() {\r
+ String value = "Option name: " + this.name + SysPrefs.newlinechar;\r
+ value += "Description: " + this.description + SysPrefs.newlinechar;\r
+ if (!Util.isEmpty(defaultValue)) {\r
+ value += "Default value: " + this.defaultValue\r
+ + SysPrefs.newlinechar;\r
}\r
- }\r
+ value += "URL: " + this.furtherDetails + SysPrefs.newlinechar;\r
+ value += "Is required: " + this.isRequired + SysPrefs.newlinechar;\r
+ if (!this.optionNames.isEmpty()) {\r
+ Set<String> sortedPosval = new TreeSet<String>(this.optionNames);\r
+ value += "Option Names: " + SysPrefs.newlinechar;\r
+ for (String val : sortedPosval) {\r
+ value += val + SysPrefs.newlinechar;\r
+ }\r
+ }\r
+ return value;\r
}\r
- if (matchCount != objArg.optionNames.size()) {\r
- return false;\r
+\r
+ /**\r
+ * Convert the option to the command string.\r
+ * \r
+ * @return If only one optionName is defined, than it is returned, if many\r
+ * option names are defined, then the defaultValue is returned.\r
+ * Option must have a default value if there are many optionNames to\r
+ * be valid.\r
+ */\r
+ public String toCommand(String nameValueSeparator) {\r
+ if (optionNames.size() == 1) {\r
+ return optionNames.iterator().next();\r
+ }\r
+ return getDefaultValue();\r
}\r
- return true;\r
- }\r
-\r
- @Override\r
- public int hashCode() {\r
- int code = this.name.hashCode() * this.description.hashCode();\r
- if (this.isRequired) {\r
- code += this.furtherDetails.hashCode() * 3;\r
- } else {\r
- if (defaultValue != null) {\r
- code += this.defaultValue.hashCode() * 2;\r
- }\r
+\r
+ @Override\r
+ public boolean equals(Object obj) {\r
+ if (obj == null) {\r
+ return false;\r
+ }\r
+ Option<?> objArg = null;\r
+ if (obj instanceof Option<?>) {\r
+ objArg = (Option<?>) obj;\r
+ } else {\r
+ return false;\r
+ }\r
+ if (!Util.isEmpty(objArg.name) && !Util.isEmpty(name)) {\r
+ if (!objArg.name.equals(this.name)) {\r
+ return false;\r
+ }\r
+ }\r
+ if (!Util.isEmpty(objArg.description) && !Util.isEmpty(description)) {\r
+ if (!objArg.description.equals(this.description)) {\r
+ return false;\r
+ }\r
+ }\r
+ if (objArg.isRequired != this.isRequired) {\r
+ return false;\r
+ }\r
+ if (!Util.isEmpty(objArg.defaultValue) && !Util.isEmpty(defaultValue)) {\r
+ if (!objArg.defaultValue.equals(this.defaultValue)) {\r
+ return false;\r
+ }\r
+ }\r
+ if (objArg.optionNames.size() != this.optionNames.size()) {\r
+ return false;\r
+ }\r
+ int matchCount = 0;\r
+ for (String oname : objArg.optionNames) {\r
+ if (Util.isEmpty(oname)) {\r
+ continue;\r
+ }\r
+ for (String thisoname : this.optionNames) {\r
+ if (oname.equals(thisoname)) {\r
+ matchCount++;\r
+ break;\r
+ }\r
+ }\r
+ }\r
+ if (matchCount != objArg.optionNames.size()) {\r
+ return false;\r
+ }\r
+ return true;\r
}\r
- if (this.description != null) {\r
- code += this.description.hashCode() * 4;\r
+\r
+ @Override\r
+ public int hashCode() {\r
+ int code = this.name.hashCode() * this.description.hashCode();\r
+ if (this.isRequired) {\r
+ code += this.furtherDetails.hashCode() * 3;\r
+ } else {\r
+ if (defaultValue != null) {\r
+ code += this.defaultValue.hashCode() * 2;\r
+ }\r
+ }\r
+ if (this.description != null) {\r
+ code += this.description.hashCode() * 4;\r
+ }\r
+\r
+ return code;\r
}\r
\r
- return code;\r
- }\r
-\r
- /**\r
- * List of possible optionNames\r
- */\r
- @Override\r
- public List<String> getPossibleValues() {\r
- return new ArrayList<String>(optionNames);\r
- }\r
-\r
- @Override\r
- public void setValue(String dValue) throws WrongParameterException {\r
- this.setDefaultValue(dValue);\r
- }\r
-\r
- /**\r
- * Validate the option\r
- * \r
- * @throws ValidationException\r
- * is the option is invalid. This happens if option does not\r
- * have a default value but have multiple option names, or no\r
- * option names is defined\r
- */\r
- void validate() throws ValidationException {\r
- if (optionNames == null) {\r
- throw new ValidationException(\r
- "Option names are not defined for option: " + this);\r
+ /**\r
+ * List of possible optionNames\r
+ */\r
+ @Override\r
+ public List<String> getPossibleValues() {\r
+ return new ArrayList<String>(optionNames);\r
}\r
- if (optionNames.size() > 1 && Util.isEmpty(getDefaultValue())) {\r
- throw new ValidationException(\r
- "Default value is required as multiple optionNames are defined for option: "\r
- + this);\r
+\r
+ @Override\r
+ public void setValue(String dValue) throws WrongParameterException {\r
+ this.setDefaultValue(dValue);\r
}\r
- if (Util.isEmpty(name)) {\r
- throw new ValidationException("No name is defined for option: "\r
- + this);\r
+\r
+ /**\r
+ * Validate the option\r
+ * \r
+ * @throws ValidationException\r
+ * is the option is invalid. This happens if option does not\r
+ * have a default value but have multiple option names, or no\r
+ * option names is defined\r
+ */\r
+ void validate() throws ValidationException {\r
+ if (optionNames == null) {\r
+ throw new ValidationException(\r
+ "Option names are not defined for option: " + this);\r
+ }\r
+ if (optionNames.size() > 1 && Util.isEmpty(getDefaultValue())) {\r
+ throw new ValidationException(\r
+ "Default value is required as multiple optionNames are defined for option: "\r
+ + this);\r
+ }\r
+ if (Util.isEmpty(name)) {\r
+ throw new ValidationException("No name is defined for option: "\r
+ + this);\r
+ }\r
}\r
- }\r
}
\ No newline at end of file