import compbio.util.Util;\r
\r
/**\r
- * A single value containing option supported by the web service e.g.\r
+ * A single value containing an option supported by the web service e.g.\r
* seqType=protein. Where seqType is a optionName and protein is one of\r
* possibleValues\r
* \r
* \r
* @author pvtroshin\r
* \r
- * Date November 2009\r
+ * @version 1.0 November 2009\r
*/\r
@XmlAccessorType(XmlAccessType.FIELD)\r
public class Parameter<T> extends Option<T> {\r
\r
- @XmlElement\r
- Set<String> possibleValues = new HashSet<String>();\r
-\r
- ValueConstrain validValue;\r
-\r
- private Parameter() {\r
- // JAXB noargs const\r
- }\r
-\r
- public Parameter(String name, String description) {\r
- super(name, description);\r
- }\r
-\r
- public ValueConstrain getValidValue() {\r
- return validValue;\r
- }\r
-\r
- public void setValidValue(ValueConstrain validValue) {\r
- if (validValue == null) {\r
- throw new NullPointerException("ValueConstrain is expected!");\r
- }\r
- this.validValue = validValue;\r
- }\r
-\r
- @Override\r
- public String toString() {\r
- String value = super.toString();\r
- if (validValue != null) {\r
- value += validValue.toString();\r
- }\r
- if (!this.possibleValues.isEmpty()) {\r
- Set<String> sortedPosval = new TreeSet<String>(this.possibleValues);\r
- value += "POSSIBLE VALUES:" + SysPrefs.newlinechar;\r
- for (String val : sortedPosval) {\r
- value += val + SysPrefs.newlinechar;\r
- }\r
- }\r
- value += SysPrefs.newlinechar;\r
- return value;\r
- }\r
-\r
- @Override\r
- public String toCommand(String nameValueSeparator) {\r
- if (nameValueSeparator == null) {\r
- throw new NullPointerException("Name value separator is expected!");\r
- }\r
- return getOptionName() + nameValueSeparator + getValue();\r
- }\r
-\r
- @Override\r
- public boolean equals(Object obj) {\r
- if (!super.equals(obj)) {\r
- return false;\r
- }\r
- Parameter<?> objp = null;\r
- if (obj instanceof Parameter<?>) {\r
- objp = (Parameter<?>) obj;\r
- } else {\r
- return false;\r
- }\r
-\r
- if (objp.possibleValues.size() != this.possibleValues.size()) {\r
- return false;\r
- }\r
- int matchCount = 0;\r
- for (String pv : objp.possibleValues) {\r
- if (Util.isEmpty(pv)) {\r
- continue;\r
- }\r
- for (String thispv : this.possibleValues) {\r
- if (pv.equals(thispv)) {\r
- matchCount++;\r
- break;\r
+ @XmlElement\r
+ Set<String> possibleValues = new HashSet<String>();\r
+\r
+ ValueConstrain validValue;\r
+\r
+ private Parameter() {\r
+ // JAXB noargs const\r
+ }\r
+\r
+ public Parameter(String name, String description) {\r
+ super(name, description);\r
+ }\r
+\r
+ public ValueConstrain getValidValue() {\r
+ return validValue;\r
+ }\r
+\r
+ public void setValidValue(ValueConstrain validValue) {\r
+ if (validValue == null) {\r
+ throw new NullPointerException("ValueConstrain is expected!");\r
+ }\r
+ this.validValue = validValue;\r
+ }\r
+\r
+ @Override\r
+ public String toString() {\r
+ String value = super.toString();\r
+ if (validValue != null) {\r
+ value += validValue.toString();\r
+ }\r
+ if (!this.possibleValues.isEmpty()) {\r
+ Set<String> sortedPosval = new TreeSet<String>(this.possibleValues);\r
+ value += "POSSIBLE VALUES:" + SysPrefs.newlinechar;\r
+ for (String val : sortedPosval) {\r
+ value += val + SysPrefs.newlinechar;\r
+ }\r
+ }\r
+ value += SysPrefs.newlinechar;\r
+ return value;\r
+ }\r
+\r
+ @Override\r
+ public String toCommand(String nameValueSeparator) {\r
+ if (nameValueSeparator == null) {\r
+ throw new NullPointerException("Name value separator is expected!");\r
+ }\r
+ return getOptionName() + nameValueSeparator + getValue();\r
+ }\r
+\r
+ @Override\r
+ public boolean equals(Object obj) {\r
+ if (!super.equals(obj)) {\r
+ return false;\r
+ }\r
+ Parameter<?> objp = null;\r
+ if (obj instanceof Parameter<?>) {\r
+ objp = (Parameter<?>) obj;\r
+ } else {\r
+ return false;\r
+ }\r
+\r
+ if (objp.possibleValues.size() != this.possibleValues.size()) {\r
+ return false;\r
}\r
- }\r
- }\r
- if (matchCount != objp.possibleValues.size()) {\r
- return false;\r
- }\r
-\r
- return true;\r
- }\r
-\r
- /**\r
- * List is more convenient to work with\r
- * \r
- * @return List of String\r
- */\r
- @Override\r
- public List<String> getPossibleValues() {\r
- return new ArrayList<String>(possibleValues);\r
- }\r
-\r
- public void setPossibleValues(Set<String> possibleValues) {\r
- this.possibleValues = new HashSet<String>(possibleValues);\r
- }\r
-\r
- public Set<String> addPossibleValues(String... value) {\r
- for (String v : value) {\r
- this.possibleValues.add(v);\r
- }\r
- return this.possibleValues;\r
- }\r
-\r
- @Override\r
- public int hashCode() {\r
- int code = super.hashCode();\r
- if (possibleValues != null) {\r
- code += possibleValues.hashCode();\r
- }\r
- return code;\r
- }\r
-\r
- @Override\r
- public void setOptionNames(Set<String> optionName) {\r
- if (optionName.size() != 1) {\r
- throw new IllegalArgumentException(\r
- "Parameter must have a single option name! But given "\r
- + optionName.size() + " names: " + optionName);\r
- }\r
- super.setOptionNames(optionName);\r
- }\r
-\r
- @Override\r
- public Set<String> addOptionNames(String... value) {\r
- throw new UnsupportedOperationException(\r
- "Parameter must have only one optionName! If you setting the only name that use setOptionName instead");\r
- }\r
-\r
- public String getOptionName() {\r
- assert optionNames.size() == 1;\r
- return optionNames.iterator().next();\r
- }\r
-\r
- public void setOptionName(String optionName) {\r
- assert !Util.isEmpty(optionName);\r
- setOptionNames(Collections.singleton(optionName));\r
- }\r
-\r
- String getValue() {\r
- if (this.possibleValues.size() == 1) {\r
- return this.possibleValues.iterator().next();\r
- }\r
- return getDefaultValue();\r
- }\r
-\r
- @Override\r
- void validate() throws ValidationException {\r
- super.validate();\r
- if (validValue == null) {\r
- if (this.possibleValues.isEmpty()) {\r
- throw new ValidationException(\r
- "No possible values defined for parameter: " + this);\r
- }\r
- if (this.possibleValues.size() > 1\r
- && Util.isEmpty(getDefaultValue())) {\r
- throw new ValidationException(\r
- "Multiple possible values are defined but no default value for parameter: "\r
- + this);\r
- }\r
- } else {\r
- if (Util.isEmpty(getDefaultValue())) {\r
- throw new ValidationException(\r
- "Default value is not defined for numeric parameter! "\r
- + this);\r
- }\r
- validValue.checkValue(getDefaultValue());\r
- }\r
- }\r
-\r
- boolean isValidValue(String value) {\r
- assert !Util.isEmpty(value);\r
- return Option.valueExist(value, getPossibleValues());\r
- }\r
-\r
- @Override\r
- public void setDefaultValue(String defaultVal)\r
- throws WrongParameterException {\r
- // If valid value constrain is not defined, then possible values must\r
- // be, and they must contain the value which is\r
- // about to be set!\r
- if (validValue == null) {\r
- if (getPossibleValues().isEmpty()) {\r
- throw new IllegalStateException(\r
- "Attempting to set default value for parameter: "\r
- + this\r
- + " Without possible values! Please define possible value first!");\r
- }\r
- if (!isValidValue(defaultVal)) {\r
- throw new WrongParameterException(\r
- "Attempting to set illegal value '" + defaultVal\r
- + "' for the parameter: " + this);\r
- }\r
- } else {\r
- try {\r
- validValue.checkValue(defaultVal);\r
- } catch (IndexOutOfBoundsException e) {\r
- throw new WrongParameterException(\r
- "Attempting to set default value outside boundaries defined by the constraint: "\r
- + validValue + "\n For parameter: " + this);\r
- }\r
- }\r
- this.defaultValue = defaultVal;\r
- }\r
+ int matchCount = 0;\r
+ for (String pv : objp.possibleValues) {\r
+ if (Util.isEmpty(pv)) {\r
+ continue;\r
+ }\r
+ for (String thispv : this.possibleValues) {\r
+ if (pv.equals(thispv)) {\r
+ matchCount++;\r
+ break;\r
+ }\r
+ }\r
+ }\r
+ if (matchCount != objp.possibleValues.size()) {\r
+ return false;\r
+ }\r
+\r
+ return true;\r
+ }\r
+\r
+ /**\r
+ * List is more convenient to work with\r
+ * \r
+ * @return List of String\r
+ */\r
+ @Override\r
+ public List<String> getPossibleValues() {\r
+ return new ArrayList<String>(possibleValues);\r
+ }\r
+\r
+ public void setPossibleValues(Set<String> possibleValues) {\r
+ this.possibleValues = new HashSet<String>(possibleValues);\r
+ }\r
+\r
+ public Set<String> addPossibleValues(String... value) {\r
+ for (String v : value) {\r
+ this.possibleValues.add(v);\r
+ }\r
+ return this.possibleValues;\r
+ }\r
+\r
+ @Override\r
+ public int hashCode() {\r
+ int code = super.hashCode();\r
+ if (possibleValues != null) {\r
+ code += possibleValues.hashCode();\r
+ }\r
+ return code;\r
+ }\r
+\r
+ @Override\r
+ public void setOptionNames(Set<String> optionName) {\r
+ if (optionName.size() != 1) {\r
+ throw new IllegalArgumentException(\r
+ "Parameter must have a single option name! But given "\r
+ + optionName.size() + " names: " + optionName);\r
+ }\r
+ super.setOptionNames(optionName);\r
+ }\r
+\r
+ @Override\r
+ public Set<String> addOptionNames(String... value) {\r
+ throw new UnsupportedOperationException(\r
+ "Parameter must have only one optionName! If you setting the only name that use setOptionName instead");\r
+ }\r
+\r
+ public String getOptionName() {\r
+ assert optionNames.size() == 1;\r
+ return optionNames.iterator().next();\r
+ }\r
+\r
+ public void setOptionName(String optionName) {\r
+ assert !Util.isEmpty(optionName);\r
+ setOptionNames(Collections.singleton(optionName));\r
+ }\r
+\r
+ String getValue() {\r
+ if (this.possibleValues.size() == 1) {\r
+ return this.possibleValues.iterator().next();\r
+ }\r
+ return getDefaultValue();\r
+ }\r
+\r
+ @Override\r
+ void validate() throws ValidationException {\r
+ super.validate();\r
+ if (validValue == null) {\r
+ if (this.possibleValues.isEmpty()) {\r
+ throw new ValidationException(\r
+ "No possible values defined for parameter: " + this);\r
+ }\r
+ if (this.possibleValues.size() > 1\r
+ && Util.isEmpty(getDefaultValue())) {\r
+ throw new ValidationException(\r
+ "Multiple possible values are defined but no default value for parameter: "\r
+ + this);\r
+ }\r
+ } else {\r
+ if (Util.isEmpty(getDefaultValue())) {\r
+ throw new ValidationException(\r
+ "Default value is not defined for numeric parameter! "\r
+ + this);\r
+ }\r
+ validValue.checkValue(getDefaultValue());\r
+ }\r
+ }\r
+\r
+ boolean isValidValue(String value) {\r
+ assert !Util.isEmpty(value);\r
+ return Option.valueExist(value, getPossibleValues());\r
+ }\r
+\r
+ @Override\r
+ public void setDefaultValue(String defaultVal)\r
+ throws WrongParameterException {\r
+ // If valid value constrain is not defined, then possible values must\r
+ // be, and they must contain the value which is\r
+ // about to be set!\r
+ if (validValue == null) {\r
+ if (getPossibleValues().isEmpty()) {\r
+ throw new IllegalStateException(\r
+ "Attempting to set default value for parameter: "\r
+ + this\r
+ + " Without possible values! Please define possible value first!");\r
+ }\r
+ if (!isValidValue(defaultVal)) {\r
+ throw new WrongParameterException(\r
+ "Attempting to set illegal value '" + defaultVal\r
+ + "' for the parameter: " + this);\r
+ }\r
+ } else {\r
+ try {\r
+ validValue.checkValue(defaultVal);\r
+ } catch (IndexOutOfBoundsException e) {\r
+ throw new WrongParameterException(\r
+ "Attempting to set default value outside boundaries defined by the constraint: "\r
+ + validValue + "\n For parameter: " + this);\r
+ }\r
+ }\r
+ this.defaultValue = defaultVal;\r
+ }\r
}\r