1 /*
2  * This file is part of ELKI:
3  * Environment for Developing KDD-Applications Supported by Index-Structures
4  *
5  * Copyright (C) 2018
6  * ELKI Development Team
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU Affero General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Affero General Public License for more details.
17  *
18  * You should have received a copy of the GNU Affero General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 package de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters;
22 
23 import de.lmu.ifi.dbs.elki.utilities.io.ParseUtil;
24 import de.lmu.ifi.dbs.elki.utilities.optionhandling.OptionID;
25 import de.lmu.ifi.dbs.elki.utilities.optionhandling.WrongParameterValueException;
26 
27 /**
28  * Parameter class for a parameter specifying a double value.
29  *
30  * @author Steffi Wanka
31  * @author Erich Schubert
32  * @since 0.1
33  */
34 public class DoubleParameter extends NumberParameter<DoubleParameter, Double> {
35   /**
36    * Constructs a double parameter with the given optionID and default value.
37    *
38    * @param optionID the unique optionID
39    * @param defaultValue the default value for this double parameter
40    */
DoubleParameter(OptionID optionID, double defaultValue)41   public DoubleParameter(OptionID optionID, double defaultValue) {
42     super(optionID, defaultValue);
43   }
44 
45   /**
46    * Constructs a double parameter with the given optionID.
47    *
48    * @param optionID the unique id of this parameter
49    */
DoubleParameter(OptionID optionID)50   public DoubleParameter(OptionID optionID) {
51     super(optionID);
52   }
53 
54   @Override
getValueAsString()55   public String getValueAsString() {
56     return getValue().toString();
57   }
58 
59   @Override
parseValue(Object obj)60   protected Double parseValue(Object obj) throws WrongParameterValueException {
61     if(obj instanceof Double) {
62       return (Double) obj;
63     }
64     if(obj instanceof Number) {
65       return ((Number) obj).doubleValue();
66     }
67     try {
68       return ParseUtil.parseDouble(obj.toString());
69     }
70     catch(NumberFormatException e) {
71       throw new WrongParameterValueException("Wrong parameter format! Parameter \"" + getOptionID().getName() + "\" requires a double value, read: " + obj + "!\n");
72     }
73   }
74 
75   /**
76    * Returns a string representation of the parameter's type.
77    *
78    * @return &quot;&lt;double&gt;&quot;
79    */
80   @Override
getSyntax()81   public String getSyntax() {
82     return "<double>";
83   }
84 
85   /**
86    * Get the parameter value as double.
87    *
88    * @return double value
89    */
doubleValue()90   public double doubleValue() {
91     return getValue().doubleValue();
92   }
93 }
94