/java/org/opensha/commons/param/DoubleDiscreteParameter.java

https://github.com/angri/OpenSHA · Java · 419 lines · 154 code · 42 blank · 223 comment · 22 complexity · 193a4758cf3920cc61bd72b02ff167d9 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.ConstrainedDoubleDiscreteParameterEditor;
  25. import org.opensha.commons.param.editor.ParameterEditor;
  26. /**
  27. * <b>Title:</b> DoubleDiscreteParameter
  28. * <p>
  29. *
  30. * <b>Description:</b> Identical to the DoubleParameter except the constraints
  31. * are a finite set of valid Double choices instead of a Min/Max
  32. * DoubleConstraint with all possible values allowed in between.
  33. * <p>
  34. *
  35. * @see DoubleParameter
  36. * @see DependentParameter
  37. * @see DependentParameterAPI
  38. * @see ParameterAPI
  39. * @author Steven W. Rock
  40. * @created February 20, 2002
  41. * @version 1.0
  42. */
  43. public class DoubleDiscreteParameter extends DependentParameter<Double>
  44. implements DependentParameterAPI<Double>, ParameterAPI<Double> {
  45. /** Class name for debugging. */
  46. protected final static String C = "DoubleDiscreteParameter";
  47. /** If true print out debug statements. */
  48. protected final static boolean D = false;
  49. private transient ParameterEditor paramEdit = null;
  50. /**
  51. * Constructor for the DoubleDiscreteParameter object. No constraints
  52. * specified, all values allowed.
  53. *
  54. * @param name
  55. * Name of this parameter
  56. */
  57. public DoubleDiscreteParameter(String name) {
  58. super(name, null, null, null);
  59. }
  60. /**
  61. * Constructor for the DoubleDiscreteParameter object No constraints
  62. * specified, all values allowed.
  63. *
  64. * @param name
  65. * Name of this parameter
  66. * @param units
  67. * Units string for this parameter
  68. */
  69. public DoubleDiscreteParameter(String name, String units) {
  70. super(name, null, units, null);
  71. }
  72. /**
  73. * Constructor for the DoubleDiscreteParameter object
  74. *
  75. * @param name
  76. * Name of this parameter
  77. * @param doubles
  78. * List of allowed doubles
  79. * @exception ConstraintException
  80. * Description of the Exception
  81. */
  82. public DoubleDiscreteParameter(String name, ArrayList doubles)
  83. throws ConstraintException {
  84. super(name, new DoubleDiscreteConstraint(doubles), null, null);
  85. // if( constraint != null ) constraint.setName( name );
  86. }
  87. /**
  88. * Constructor for the DoubleDiscreteParameter object
  89. *
  90. * @param name
  91. * Name of this parameter
  92. * @param doubles
  93. * List of allowed doubles
  94. * @param units
  95. * Units string for this parameter
  96. * @exception ConstraintException
  97. * Description of the Exception
  98. */
  99. public DoubleDiscreteParameter(String name, ArrayList doubles, String units)
  100. throws ConstraintException {
  101. super(name, new DoubleDiscreteConstraint(doubles), units, null);
  102. // if( constraint != null ) constraint.setName( name );
  103. }
  104. /**
  105. * Constructor for the DoubleDiscreteParameter object
  106. *
  107. * @param name
  108. * Name of this parameter
  109. * @param constraint
  110. * List of allowed doubles
  111. * @exception ConstraintException
  112. * Description of the Exception
  113. */
  114. public DoubleDiscreteParameter(String name,
  115. DoubleDiscreteConstraint constraint) throws ConstraintException {
  116. super(name, constraint, null, null);
  117. // if( (constraint != null) && (constraint.getName() == null) )
  118. // constraint.setName( name );
  119. }
  120. /**
  121. * Constructor for the DoubleDiscreteParameter object
  122. *
  123. * @param name
  124. * Name of this parameter
  125. * @param constraint
  126. * List of allowed doubles
  127. * @param units
  128. * Units string for this parameter
  129. * @exception ConstraintException
  130. * Description of the Exception
  131. */
  132. public DoubleDiscreteParameter(String name,
  133. DoubleDiscreteConstraint constraint, String units)
  134. throws ConstraintException {
  135. super(name, constraint, units, null);
  136. // if( (constraint != null) && (constraint.getName() == null) )
  137. // constraint.setName( name );
  138. }
  139. /**
  140. * No constraints specified, all values allowed *
  141. *
  142. * @param name
  143. * Name for this parameter
  144. * @param value
  145. * The value to set this parameter to
  146. */
  147. public DoubleDiscreteParameter(String name, Double value) {
  148. super(name, null, null, value);
  149. }
  150. /**
  151. * Constructor for the DoubleDiscreteParameter object
  152. *
  153. * @param name
  154. * Name for this parameter
  155. * @param units
  156. * Units for this parameter
  157. * @param value
  158. * The value to set this parameter to
  159. * @exception ConstraintException
  160. * Thrown if value not allowed
  161. */
  162. public DoubleDiscreteParameter(String name, String units, Double value)
  163. throws ConstraintException {
  164. super(name, null, units, value);
  165. }
  166. /**
  167. * Constructor for the DoubleDiscreteParameter object
  168. *
  169. * @param name
  170. * Name for this parameter
  171. * @param value
  172. * The value to set this parameter to
  173. * @param doubles
  174. * list of allowed values
  175. * @exception ConstraintException
  176. * Thrown if value not allowed
  177. */
  178. public DoubleDiscreteParameter(String name, ArrayList doubles, Double value)
  179. throws ConstraintException {
  180. super(name, new DoubleDiscreteConstraint(doubles), null, value);
  181. // if( constraint != null ) constraint.setName( name );
  182. }
  183. /**
  184. * Constructor for the DoubleDiscreteParameter object
  185. *
  186. * @param name
  187. * Name for this parameter
  188. * @param units
  189. * Units for this parameter
  190. * @param value
  191. * The value to set this parameter to
  192. * @param doubles
  193. * list of allowed values
  194. * @exception ConstraintException
  195. * Thrown if value not allowed
  196. */
  197. public DoubleDiscreteParameter(String name, ArrayList doubles,
  198. String units, Double value) throws ConstraintException {
  199. super(name, new DoubleDiscreteConstraint(doubles), units, value);
  200. // if( constraint != null ) constraint.setName( name );
  201. }
  202. /**
  203. * Constructor for the DoubleDiscreteParameter object
  204. *
  205. * @param name
  206. * Name for this parameter
  207. * @param constraint
  208. * List of allowed values
  209. * @param value
  210. * The value to set this parameter to
  211. * @exception ConstraintException
  212. * Thrown if value not allowed
  213. */
  214. public DoubleDiscreteParameter(String name,
  215. DoubleDiscreteConstraint constraint, Double value)
  216. throws ConstraintException {
  217. super(name, constraint, null, value);
  218. // if( (constraint != null) && (constraint.getName() == null) )
  219. // constraint.setName( name );
  220. }
  221. /**
  222. * This is the main constructor. All other constructors call this one.
  223. * Constraints must be set first, because the value may not be an allowed
  224. * one. Null values are always allowed in the constructor
  225. *
  226. * @param name
  227. * Name for this parameter
  228. * @param constraint
  229. * List of allowed values
  230. * @param units
  231. * Units for this parameter
  232. * @param value
  233. * The value to set this parameter to
  234. * @exception ConstraintException
  235. * Thrown if value not allowed
  236. */
  237. public DoubleDiscreteParameter(String name,
  238. DoubleDiscreteConstraint constraint, String units, Double value)
  239. throws ConstraintException {
  240. super(name, constraint, units, value);
  241. // if( (constraint != null) && (constraint.getName() == null) )
  242. // constraint.setName( name );
  243. }
  244. /**
  245. *
  246. */
  247. /**
  248. * Sets the constraint if it is a DoubleDiscreteConstraint and the parameter
  249. * is currently editable.
  250. *
  251. * @param constraint
  252. * The new constraint object
  253. * @throws ParameterException
  254. * Thrown if constraint is not a DoubleDiscreteConstraint
  255. * @throws EditableException
  256. * Thrown if Parameter is currently uneditable.
  257. */
  258. public void setConstraint(ParameterConstraintAPI constraint)
  259. throws ParameterException, EditableException {
  260. String S = C + ": setConstraint( ): ";
  261. checkEditable(S);
  262. if (!(constraint instanceof DoubleDiscreteConstraint)) {
  263. throw new ParameterException(
  264. S
  265. + "This parameter only accepts DoubleDiscreteConstraints, unable to set the constraint.");
  266. } else
  267. super.setConstraint(constraint);
  268. // if( (constraint != null) && (constraint.getName() == null) )
  269. // constraint.setName( name );
  270. }
  271. /**
  272. * Gets the type attribute of the DoubleDiscreteParameter object. This is
  273. * used to determine which gui editor to use for this parameter.
  274. */
  275. public String getType() {
  276. String type = C;
  277. // Modify if constrained
  278. ParameterConstraintAPI constraint = this.constraint;
  279. if (constraint != null)
  280. type = "Constrained" + type;
  281. return type;
  282. }
  283. /** Returns a clone of all allowed values. Proxy to constraint object. */
  284. public ArrayList<Double> getAllowedDoubles() {
  285. return ((DoubleDiscreteConstraint) this.constraint).getAllowedDoubles();
  286. }
  287. /**
  288. * Compares the parameter values to see if they are the same
  289. *
  290. * @param obj
  291. * Double Parameter to compare to
  292. * @return -1 if this < obj, 0 if this = obj, +1 if this > obj
  293. * @exception ClassCastException
  294. * Thrown if passed in value is not a DoubleParameter
  295. */
  296. public int compareTo(Object obj) throws ClassCastException {
  297. String S = C + ":compareTo(): ";
  298. if (!(obj instanceof DoubleParameter)
  299. && !(obj instanceof DoubleDiscreteParameter)) {
  300. throw new ClassCastException(
  301. S
  302. + "Object not a DoubleParameter, or DoubleDiscreteParameter, unable to compare");
  303. }
  304. int result = 0;
  305. Double n1 = (Double) this.getValue();
  306. Double n2 = null;
  307. if (obj instanceof DoubleParameter) {
  308. DoubleParameter param = (DoubleParameter) obj;
  309. n2 = (Double) param.getValue();
  310. } else if (obj instanceof DoubleDiscreteParameter) {
  311. DoubleDiscreteParameter param = (DoubleDiscreteParameter) obj;
  312. n2 = (Double) param.getValue();
  313. }
  314. return n1.compareTo(n2);
  315. }
  316. /**
  317. * Compares value to see if equal
  318. *
  319. * @param obj
  320. * Double Parameter to compare to
  321. * @return true if these two parameter values are the same
  322. * @exception ClassCastException
  323. * Thrown if passed in value is not a DoubleParameter
  324. */
  325. public boolean equals(Object obj) throws ClassCastException {
  326. String S = C + ":equals(): ";
  327. if (!(obj instanceof DoubleParameter)
  328. && !(obj instanceof DoubleDiscreteParameter)) {
  329. throw new ClassCastException(
  330. S
  331. + "Object not a DoubleParameter, or DoubleDiscreteParameter, unable to compare");
  332. }
  333. String otherName = ((ParameterAPI) obj).getName();
  334. if ((compareTo(obj) == 0) && getName().equals(otherName)) {
  335. return true;
  336. } else {
  337. return false;
  338. }
  339. }
  340. /** Returns a copy so you can't edit or damage the original. */
  341. public Object clone() {
  342. DoubleDiscreteConstraint c1 = null;
  343. if (constraint != null)
  344. c1 = (DoubleDiscreteConstraint) constraint.clone();
  345. DoubleDiscreteParameter param = null;
  346. if (value == null)
  347. param = new DoubleDiscreteParameter(name, c1, units);
  348. else
  349. param =
  350. new DoubleDiscreteParameter(name, c1, units, new Double(
  351. this.value.toString()));
  352. param.editable = true;
  353. param.info = info;
  354. return param;
  355. }
  356. public boolean setIndividualParamValueFromXML(Element el) {
  357. try {
  358. Double val = Double.parseDouble(el.attributeValue("value"));
  359. this.setValue(val);
  360. return true;
  361. } catch (NumberFormatException e) {
  362. return false;
  363. }
  364. }
  365. public ParameterEditor getEditor() {
  366. if (paramEdit == null) {
  367. if (constraint != null)
  368. try {
  369. paramEdit =
  370. new ConstrainedDoubleDiscreteParameterEditor(this);
  371. } catch (Exception e) {
  372. throw new RuntimeException(e);
  373. }
  374. }
  375. return paramEdit;
  376. }
  377. }