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.ParameterException; 26 import de.lmu.ifi.dbs.elki.utilities.optionhandling.WrongParameterValueException; 27 28 /** 29 * Parameter class for a parameter specifying an integer value. 30 * 31 * @author Steffi Wanka 32 * @author Erich Schubert 33 * @since 0.1 34 */ 35 public class IntParameter extends NumberParameter<IntParameter, Integer> { 36 /** 37 * Constructs an integer parameter with the given optionID. 38 * 39 * @param optionID optionID the unique id of the option 40 * @param defaultValue the default value 41 */ IntParameter(OptionID optionID, int defaultValue)42 public IntParameter(OptionID optionID, int defaultValue) { 43 super(optionID, Integer.valueOf(defaultValue)); 44 } 45 46 /** 47 * Constructs an integer parameter with the given optionID. 48 * 49 * @param optionID optionID the unique id of the option 50 */ IntParameter(OptionID optionID)51 public IntParameter(OptionID optionID) { 52 super(optionID); 53 } 54 55 @Override getValueAsString()56 public String getValueAsString() { 57 return getValue().toString(); 58 } 59 60 @Override parseValue(Object obj)61 protected Integer parseValue(Object obj) throws ParameterException { 62 if(obj instanceof Integer) { 63 return (Integer) obj; 64 } 65 if(obj instanceof Number) { 66 return ((Number) obj).intValue(); 67 } 68 try { 69 return ParseUtil.parseIntBase10(obj.toString()); 70 } 71 catch(NumberFormatException e) { 72 throw new WrongParameterValueException("Wrong parameter format! Parameter \"" + getOptionID().getName() + "\" requires an integer value, read: " + obj + "!\n"); 73 } 74 } 75 76 /** 77 * Returns a string representation of the parameter's type. 78 * 79 * @return "<int>" 80 */ 81 @Override getSyntax()82 public String getSyntax() { 83 return "<int>"; 84 } 85 86 /** 87 * Get the parameter value as integer 88 * 89 * @return Parameter value 90 */ intValue()91 public int intValue() { 92 return getValue().intValue(); 93 } 94 } 95