1 /* Copyright (c) 2009 Peter Troshin
\r
3 * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0
\r
5 * This library is free software; you can redistribute it and/or modify it under the terms of the
\r
6 * Apache License version 2 as published by the Apache Software Foundation
\r
8 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
\r
9 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache
\r
10 * License for more details.
\r
12 * A copy of the license is in apache_license.txt. It is also available here:
\r
13 * @see: http://www.apache.org/licenses/LICENSE-2.0.txt
\r
15 * Any republication or derived work distributed in source code form
\r
16 * must include this copyright and license notice.
\r
19 package compbio.metadata;
\r
21 import java.util.ArrayList;
\r
22 import java.util.Collections;
\r
23 import java.util.HashSet;
\r
24 import java.util.List;
\r
25 import java.util.Set;
\r
26 import java.util.TreeSet;
\r
28 import javax.xml.bind.ValidationException;
\r
29 import javax.xml.bind.annotation.XmlAccessType;
\r
30 import javax.xml.bind.annotation.XmlAccessorType;
\r
31 import javax.xml.bind.annotation.XmlElement;
\r
33 import compbio.util.SysPrefs;
\r
34 import compbio.util.Util;
\r
37 * A single value containing option supported by the web service e.g.
\r
38 * seqType=protein. Where seqType is a optionName and protein is one of
\r
46 * Date November 2009
\r
48 @XmlAccessorType(XmlAccessType.FIELD)
\r
49 public class Parameter<T> extends Option<T> {
\r
52 Set<String> possibleValues = new HashSet<String>();
\r
54 ValueConstrain validValue;
\r
56 private Parameter() {
\r
57 // JAXB noargs const
\r
60 public Parameter(String name, String description) {
\r
61 super(name, description);
\r
64 public ValueConstrain getValidValue() {
\r
68 public void setValidValue(ValueConstrain validValue) {
\r
69 if (validValue == null) {
\r
70 throw new NullPointerException("ValueConstrain is expected!");
\r
72 this.validValue = validValue;
\r
76 public String toString() {
\r
77 String value = super.toString();
\r
78 if (validValue != null) {
\r
79 value += validValue.toString();
\r
81 if (!this.possibleValues.isEmpty()) {
\r
82 Set<String> sortedPosval = new TreeSet<String>(this.possibleValues);
\r
83 value += "POSSIBLE VALUES:" + SysPrefs.newlinechar;
\r
84 for (String val : sortedPosval) {
\r
85 value += val + SysPrefs.newlinechar;
\r
88 value += SysPrefs.newlinechar;
\r
93 public String toCommand(String nameValueSeparator) {
\r
94 if (nameValueSeparator == null) {
\r
95 throw new NullPointerException("Name value separator is expected!");
\r
97 return getOptionName() + nameValueSeparator + getValue();
\r
101 public boolean equals(Object obj) {
\r
102 if (!super.equals(obj)) {
\r
105 Parameter<?> objp = null;
\r
106 if (obj instanceof Parameter<?>) {
\r
107 objp = (Parameter<?>) obj;
\r
112 if (objp.possibleValues.size() != this.possibleValues.size()) {
\r
115 int matchCount = 0;
\r
116 for (String pv : objp.possibleValues) {
\r
117 if (Util.isEmpty(pv)) {
\r
120 for (String thispv : this.possibleValues) {
\r
121 if (pv.equals(thispv)) {
\r
127 if (matchCount != objp.possibleValues.size()) {
\r
135 * List is more convenient to work with
\r
137 * @return List of String
\r
140 public List<String> getPossibleValues() {
\r
141 return new ArrayList<String>(possibleValues);
\r
144 public void setPossibleValues(Set<String> possibleValues) {
\r
145 this.possibleValues = new HashSet<String>(possibleValues);
\r
148 public Set<String> addPossibleValues(String... value) {
\r
149 for (String v : value) {
\r
150 this.possibleValues.add(v);
\r
152 return this.possibleValues;
\r
156 public int hashCode() {
\r
157 int code = super.hashCode();
\r
158 if (possibleValues != null) {
\r
159 code += possibleValues.hashCode();
\r
165 public void setOptionNames(Set<String> optionName) {
\r
166 if (optionName.size() != 1) {
\r
167 throw new IllegalArgumentException(
\r
168 "Parameter must have a single option name! But given "
\r
169 + optionName.size() + " names: " + optionName);
\r
171 super.setOptionNames(optionName);
\r
175 public Set<String> addOptionNames(String... value) {
\r
176 throw new UnsupportedOperationException(
\r
177 "Parameter must have only one optionName! If you setting the only name that use setOptionName instead");
\r
180 public String getOptionName() {
\r
181 assert optionNames.size() == 1;
\r
182 return optionNames.iterator().next();
\r
185 public void setOptionName(String optionName) {
\r
186 assert !Util.isEmpty(optionName);
\r
187 setOptionNames(Collections.singleton(optionName));
\r
190 String getValue() {
\r
191 if (this.possibleValues.size() == 1) {
\r
192 return this.possibleValues.iterator().next();
\r
194 return getDefaultValue();
\r
198 void validate() throws ValidationException {
\r
200 if (validValue == null) {
\r
201 if (this.possibleValues.isEmpty()) {
\r
202 throw new ValidationException(
\r
203 "No possible values defined for parameter: " + this);
\r
205 if (this.possibleValues.size() > 1
\r
206 && Util.isEmpty(getDefaultValue())) {
\r
207 throw new ValidationException(
\r
208 "Multiple possible values are defined but no default value for parameter: "
\r
212 if (Util.isEmpty(getDefaultValue())) {
\r
213 throw new ValidationException(
\r
214 "Default value is not defined for numeric parameter! "
\r
217 validValue.checkValue(getDefaultValue());
\r
221 boolean isValidValue(String value) {
\r
222 assert !Util.isEmpty(value);
\r
223 return Option.valueExist(value, getPossibleValues());
\r
227 public void setDefaultValue(String defaultVal)
\r
228 throws WrongParameterException {
\r
229 // If valid value constrain is not defined, then possible values must
\r
230 // be, and they must contain the value which is
\r
231 // about to be set!
\r
232 if (validValue == null) {
\r
233 if (getPossibleValues().isEmpty()) {
\r
234 throw new IllegalStateException(
\r
235 "Attempting to set default value for parameter: "
\r
237 + " Without possible values! Please define possible value first!");
\r
239 if (!isValidValue(defaultVal)) {
\r
240 throw new WrongParameterException(
\r
241 "Attempting to set illegal value '" + defaultVal
\r
242 + "' for the parameter: " + this);
\r
246 validValue.checkValue(defaultVal);
\r
247 } catch (IndexOutOfBoundsException e) {
\r
248 throw new WrongParameterException(
\r
249 "Attempting to set default value outside boundaries defined by the constraint: "
\r
250 + validValue + "\n For parameter: " + this);
\r
253 this.defaultValue = defaultVal;
\r