PageRenderTime 80ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/java/org/opensha/commons/param/StringParameter.java

https://github.com/joshuamckenty/OpenSHA
Java | 369 lines | 140 code | 37 blank | 192 comment | 26 complexity | c40b898d9d1512f852e1cdada7a5357f MD5 | raw file
  1. /*******************************************************************************
  2. * Copyright 2009 OpenSHA.org in partnership with the Southern California
  3. * Earthquake Center (SCEC, http://www.scec.org) at the University of Southern
  4. * California and the UnitedStates Geological Survey (USGS; http://www.usgs.gov)
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  7. * use this file except in compliance with the License. You may obtain a copy of
  8. * the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  14. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  15. * License for the specific language governing permissions and limitations under
  16. * the License.
  17. ******************************************************************************/
  18. package org.opensha.commons.param;
  19. import java.util.ArrayList;
  20. import org.dom4j.Element;
  21. import org.opensha.commons.exceptions.ConstraintException;
  22. import org.opensha.commons.exceptions.EditableException;
  23. import org.opensha.commons.exceptions.ParameterException;
  24. import org.opensha.commons.param.editor.ConstrainedStringParameterEditor;
  25. import org.opensha.commons.param.editor.ParameterEditor;
  26. import org.opensha.commons.param.editor.StringParameterEditor;
  27. /**
  28. * <b>Title:</b> StringParameter
  29. * <p>
  30. *
  31. * <b>Description:</b> String Parameter that accepts strings as it's values. If
  32. * constraints are present, setting the value must pass the constraint check.
  33. * Since the Parameter class in an ancestor, all Parameter's fields are
  34. * inherited.
  35. * <p>
  36. *
  37. * The constraints are StringConstraint which implies a StringParameter value
  38. * can only be choosen from a list of strings.
  39. * <p>
  40. *
  41. * Note: SWR: The constraint object could be "supercharged" by using Regular
  42. * Expressions introduced in java 1.4. Then we wouldn't need a list of allowed
  43. * values but rather a matcher pattern. For example "[A-Za-z]*" would allow all
  44. * values that start with a lowercase or upper case alphabet letter.
  45. * <p>
  46. *
  47. * @author Sid Hellman, Steven W. Rock
  48. * @created February 21, 2002
  49. * @version 1.0
  50. */
  51. public class StringParameter extends DependentParameter<String> implements
  52. DependentParameterAPI<String>, ParameterAPI<String> {
  53. /** Class name for debugging. */
  54. protected final static String C = "StringParameter";
  55. /** If true print out debug statements. */
  56. protected final static boolean D = false;
  57. private transient ParameterEditor paramEdit = null;
  58. /**
  59. * Constructor doesn't specify a constraint, all values allowed. This
  60. * constructor sets the name of this parameter.
  61. */
  62. public StringParameter(String name) {
  63. this.name = name;
  64. }
  65. /**
  66. * Input vector is turned into StringConstraints object. If vector contains
  67. * no elements an exception is thrown. This constructor also sets the name
  68. * of this parameter.
  69. *
  70. * @param name
  71. * Name of the parametet
  72. * @param strings
  73. * Converted to the Constraint object
  74. * @exception ConstraintException
  75. * Thrown if vector of allowed values is empty
  76. * @throws ConstraintException
  77. * Thrown if vector of allowed values is empty
  78. */
  79. public StringParameter(String name, ArrayList strings)
  80. throws ConstraintException {
  81. this(name, new StringConstraint(strings), null, null);
  82. }
  83. /**
  84. * Constructor that sets the name and Constraint during initialization.
  85. *
  86. * @param name
  87. * Name of the parametet
  88. * @param constraint
  89. * Constraint object
  90. * @exception ConstraintException
  91. * Description of the Exception
  92. * @throws ConstraintException
  93. * Is thrown if the value is not allowed
  94. */
  95. public StringParameter(String name, StringConstraint constraint)
  96. throws ConstraintException {
  97. this(name, constraint, null, null);
  98. }
  99. /**
  100. * No constraints specified, all values allowed. This constructor set's the
  101. * name of this parameter as well as the value.
  102. *
  103. * @param name
  104. * Name of the parametet
  105. * @param value
  106. * value of this parameter
  107. */
  108. public StringParameter(String name, String value) {
  109. this.name = name;
  110. this.value = value;
  111. }
  112. /**
  113. * No constraints specified, all values allowed. Sets the name, units and
  114. * value.
  115. *
  116. * @param name
  117. * Name of the parameter
  118. * @param units
  119. * Units of the parameter
  120. * @param value
  121. * value of this parameter
  122. * @exception ConstraintException
  123. * Description of the Exception
  124. */
  125. public StringParameter(String name, String units, String value)
  126. throws ConstraintException {
  127. this(name, null, units, value);
  128. }
  129. /**
  130. * Sets the name, vector of string converted to a constraint, amd value.
  131. *
  132. * @param name
  133. * Name of the parametet
  134. * @param strings
  135. * vector of allowed values converted to a constraint
  136. * @param value
  137. * value of this parameter
  138. * @exception ConstraintException
  139. * Is thrown if the value is not allowed
  140. * @throws ConstraintException
  141. * Is thrown if the value is not allowed
  142. */
  143. public StringParameter(String name, ArrayList strings, String value)
  144. throws ConstraintException {
  145. this(name, new StringConstraint(strings), null, value);
  146. }
  147. /**
  148. * Sets the name, constraint, and value.
  149. *
  150. * @param name
  151. * Name of the parametet
  152. * @param constraint
  153. * List of allowed values
  154. * @param value
  155. * value of this parameter
  156. * @exception ConstraintException
  157. * Is thrown if the value is not allowed
  158. * @throws ConstraintException
  159. * Is thrown if the value is not allowed
  160. */
  161. public StringParameter(String name, StringConstraint constraint,
  162. String value) throws ConstraintException {
  163. this(name, constraint, null, value);
  164. }
  165. /**
  166. * This is the main constructor. All other constructors call this one.
  167. * Constraints must be set first, because the value may not be an allowed
  168. * one. Null values are always allowed in the constructor. All values are
  169. * set in this constructor; name, value, units, and constructor
  170. *
  171. * @param name
  172. * Name of the parametet
  173. * @param constraint
  174. * Lsit of allowed values
  175. * @param value
  176. * value object of this parameter
  177. * @param units
  178. * Units of this parameter
  179. * @exception ConstraintException
  180. * Is thrown if the value is not allowed
  181. * @throws ConstraintException
  182. * Is thrown if the value is not allowed
  183. */
  184. public StringParameter(String name, StringConstraint constraint,
  185. String units, String value) throws ConstraintException {
  186. super(name, constraint, units, value);
  187. }
  188. /**
  189. * Sets the constraint reference if it is a StringConstraint and the
  190. * parameter is currently editable, else throws an exception.
  191. */
  192. public void setConstraint(ParameterConstraintAPI constraint)
  193. throws ParameterException, EditableException {
  194. String S = C + ": setConstraint(): ";
  195. checkEditable(S);
  196. if (!(constraint instanceof StringConstraint)) {
  197. throw new ParameterException(
  198. S
  199. + "This parameter only accepts StringConstraints, unable to set the constraint.");
  200. } else
  201. super.setConstraint(constraint);
  202. }
  203. /**
  204. * Gets the type attribute of the StringParameter object. Returns the class
  205. * name if unconstrained, else "Constrained" + classname. This is used to
  206. * determine which type of GUI editor applies to this parameter.
  207. *
  208. * @return The GUI editor type
  209. */
  210. public String getType() {
  211. String type = C;
  212. // Modify if constrained
  213. ParameterConstraintAPI constraint = this.constraint;
  214. if (constraint != null)
  215. type = "Constrained" + type;
  216. return type;
  217. }
  218. /**
  219. * Returns a clone of the allowed strings of the constraint. Useful for
  220. * presenting in a picklist
  221. *
  222. * @return The allowedStrings vector
  223. */
  224. public ArrayList<String> getAllowedStrings() {
  225. return ((StringConstraint) this.constraint).getAllowedStrings();
  226. }
  227. /**
  228. * Compares the values to if this is less than, equal to, or greater than
  229. * the comparing objects. Implementation of comparable interface. Helps with
  230. * sorting a list of parameters.
  231. *
  232. * @param obj
  233. * The object to compare this to
  234. * @return -1 if this value < obj value, 0 if equal, +1 if this value > obj
  235. * value
  236. * @exception ClassCastException
  237. * Is thrown if the comparing object is not a StringParameter
  238. * *
  239. * @see Comparable
  240. */
  241. public int compareTo(Object obj) throws ClassCastException {
  242. String S = C + ":compareTo(): ";
  243. if (!(obj instanceof StringParameter)) {
  244. throw new ClassCastException(S
  245. + "Object not a StringParameter, unable to compare");
  246. }
  247. StringParameter param = (StringParameter) obj;
  248. if ((this.value == null) && (param.value == null))
  249. return 0;
  250. int result = 0;
  251. String n1 = (String) this.getValue();
  252. String n2 = (String) param.getValue();
  253. return n1.compareTo(n2);
  254. }
  255. /**
  256. * Compares the passed in String parameter to see if it has the same name
  257. * and value. If the object is not a String parameter an exception is
  258. * thrown. If the values and names are equal true is returned, otherwise
  259. * false is returned.
  260. *
  261. * @param obj
  262. * The object to compare this to
  263. * @return True if the values are identical
  264. * @exception ClassCastException
  265. * Is thrown if the comparing object is not a StringParameter
  266. */
  267. public boolean equals(Object obj) throws ClassCastException {
  268. String S = C + ":equals(): ";
  269. if (!(obj instanceof StringParameter)) {
  270. throw new ClassCastException(S
  271. + "Object not a StringParameter, unable to compare");
  272. }
  273. String otherName = ((StringParameter) obj).getName();
  274. if ((compareTo(obj) == 0) && getName().equals(otherName)) {
  275. return true;
  276. } else {
  277. return false;
  278. }
  279. }
  280. /**
  281. * Returns a copy so you can't edit or damage the origial. Clones this
  282. * object's value and all fields. The constraints are also cloned.
  283. *
  284. * @return Description of the Return Value
  285. */
  286. public Object clone() {
  287. StringConstraint c1 = null;
  288. if (constraint != null)
  289. c1 = (StringConstraint) constraint.clone();
  290. StringParameter param = null;
  291. if (value == null) {
  292. param = new StringParameter(name, c1);
  293. param.setUnits(units);
  294. } else
  295. param = new StringParameter(name, c1, units, this.value.toString());
  296. if (param == null)
  297. return null;
  298. param.editable = true;
  299. param.info = info;
  300. return param;
  301. }
  302. public boolean setIndividualParamValueFromXML(Element el) {
  303. String val = el.attributeValue("value");
  304. if (val.length() == 0) {
  305. try {
  306. this.setValue("");
  307. } catch (ConstraintException e) {
  308. System.err
  309. .println("Warning: could not set String Param to empty string from XML");
  310. } catch (ParameterException e) {
  311. System.err
  312. .println("Warning: could not set String Param to empty string from XML");
  313. }
  314. } else {
  315. this.setValue(val);
  316. }
  317. return true;
  318. }
  319. public ParameterEditor getEditor() {
  320. if (paramEdit == null) {
  321. if (constraint == null)
  322. try {
  323. paramEdit = new StringParameterEditor(this);
  324. } catch (Exception e) {
  325. throw new RuntimeException(e);
  326. }
  327. else
  328. paramEdit = new ConstrainedStringParameterEditor(this);
  329. }
  330. return paramEdit;
  331. }
  332. }