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.List;
\r
24 import javax.xml.bind.annotation.XmlAccessType;
\r
25 import javax.xml.bind.annotation.XmlAccessorType;
\r
26 import javax.xml.bind.annotation.XmlElement;
\r
27 import javax.xml.bind.annotation.XmlElementWrapper;
\r
28 import javax.xml.bind.annotation.XmlTransient;
\r
30 import compbio.util.SysPrefs;
\r
33 * Collection of Options and Parameters with their values
\r
40 * @version 1.0 December 2009
\r
44 @XmlAccessorType(XmlAccessType.FIELD)
\r
45 public class Preset<T> {
\r
48 private static final String SPACE = " ";
\r
50 @XmlElement(required = true, nillable = false)
\r
51 // @XmlID - ? require no spaces (!)
\r
56 @XmlElement(required = true, nillable = false)
\r
57 @XmlElementWrapper(name = "optlist")
\r
58 List<String> option;
\r
60 public void setOptions(List<String> option) {
\r
61 this.option = option;
\r
64 public void setName(String name) {
\r
68 public void setDescription(String description) {
\r
69 this.description = description;
\r
73 * @return a List of Options as a String
\r
75 public List<String> getOptions() {
\r
76 return new ArrayList<String>(option);
\r
81 * @return - name of the Preset
\r
83 public String getName() {
\r
89 * @return - a long description of the Preset
\r
91 public String getDescription() {
\r
96 * Converts list of options as String to type Option
\r
99 * @return List of Options
\r
100 * @throws WrongParameterException
\r
101 * if the value of the parameter is invalid @see
\r
102 * {@link Parameter}
\r
104 public List<Option<T>> getArguments(RunnerConfig<T> rconfig)
\r
105 throws WrongParameterException {
\r
106 List<Option<T>> options = new ArrayList<Option<T>>();
\r
107 for (String optionName : option) {
\r
108 optionName = optionName.trim();
\r
109 String oname = getName(optionName);
\r
110 Option<T> option = rconfig.getArgumentByOptionName(oname);
\r
111 if (option != null) {
\r
112 // Set default value to the preset value
\r
113 if (containValue(optionName)) {
\r
114 // extract and set value to the parameter
\r
115 option.setDefaultValue(getValue(optionName));
\r
117 // set value to the option as default, as this could be a
\r
118 // multi-option value
\r
119 option.setDefaultValue(oname);
\r
121 options.add(option);
\r
127 boolean containValue(String option) {
\r
128 if (option.trim().contains(SPACE)) {
\r
134 String getName(String option) {
\r
135 option = option.trim();
\r
136 if (containValue(option)) {
\r
137 return option.substring(0, option.indexOf(SPACE)).trim();
\r
142 String getValue(String option) {
\r
143 assert containValue(option);
\r
144 option = option.trim();
\r
145 return option.substring(option.indexOf(SPACE) + 1).trim();
\r
149 public String toString() {
\r
150 String value = "Preset name: '" + name + "'" + SysPrefs.newlinechar;
\r
151 value += "Description: " + description + SysPrefs.newlinechar;
\r
152 value += "Options: " + SysPrefs.newlinechar;
\r
153 for (String oname : this.option) {
\r
154 value += oname + SysPrefs.newlinechar;
\r
156 value += SysPrefs.newlinechar;
\r
161 public int hashCode() {
\r
162 final int prime = 31;
\r
164 result = prime * result
\r
165 + ((description == null) ? 0 : description.hashCode());
\r
166 result = prime * result + ((name == null) ? 0 : name.hashCode());
\r
167 result = prime * result + ((option == null) ? 0 : option.hashCode());
\r
172 public boolean equals(Object obj) {
\r
177 if (getClass() != obj.getClass())
\r
179 Preset other = (Preset) obj;
\r
180 if (description == null) {
\r
181 if (other.description != null)
\r
183 } else if (!description.equals(other.description))
\r
185 if (name == null) {
\r
186 if (other.name != null)
\r
188 } else if (!name.equals(other.name))
\r
190 if (option == null) {
\r
191 if (other.option != null)
\r
193 } else if (!option.equals(other.option))
\r