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 java.util.regex.Pattern; 24 import java.util.regex.PatternSyntaxException; 25 26 import de.lmu.ifi.dbs.elki.utilities.optionhandling.OptionID; 27 import de.lmu.ifi.dbs.elki.utilities.optionhandling.ParameterException; 28 import de.lmu.ifi.dbs.elki.utilities.optionhandling.UnspecifiedParameterException; 29 import de.lmu.ifi.dbs.elki.utilities.optionhandling.WrongParameterValueException; 30 31 /** 32 * Parameter class for a parameter specifying a pattern. 33 * 34 * @author Steffi Wanka 35 * @author Erich Schubert 36 * @since 0.3 37 */ 38 public class PatternParameter extends AbstractParameter<PatternParameter, Pattern> { 39 /** 40 * Constructs a pattern parameter with the given optionID, and default value. 41 * 42 * @param optionID the unique id of the parameter 43 * @param defaultValue the default value of the parameter 44 */ PatternParameter(OptionID optionID, Pattern defaultValue)45 public PatternParameter(OptionID optionID, Pattern defaultValue) { 46 super(optionID, defaultValue); 47 } 48 49 /** 50 * Constructs a pattern parameter with the given optionID, and default value. 51 * 52 * @param optionID the unique id of the parameter 53 * @param defaultValue the default value of the parameter 54 */ PatternParameter(OptionID optionID, String defaultValue)55 public PatternParameter(OptionID optionID, String defaultValue) { 56 super(optionID, Pattern.compile(defaultValue, Pattern.CASE_INSENSITIVE)); 57 } 58 59 /** 60 * Constructs a pattern parameter with the given optionID. 61 * 62 * @param optionID the unique id of the parameter 63 */ PatternParameter(OptionID optionID)64 public PatternParameter(OptionID optionID) { 65 super(optionID); 66 } 67 68 @Override getValueAsString()69 public String getValueAsString() { 70 return getValue().toString(); 71 } 72 73 @Override parseValue(Object obj)74 protected Pattern parseValue(Object obj) throws ParameterException { 75 if(obj == null) { 76 throw new UnspecifiedParameterException(this); 77 } 78 if(obj instanceof Pattern) { 79 return (Pattern) obj; 80 } 81 if(obj instanceof String) { 82 try { 83 return Pattern.compile((String) obj, Pattern.CASE_INSENSITIVE); 84 } 85 catch(PatternSyntaxException e) { 86 throw new WrongParameterValueException("Given pattern \"" + obj + "\" for parameter \"" + getOptionID().getName() + "\" is no valid regular expression!"); 87 } 88 } 89 throw new WrongParameterValueException("Given pattern \"" + obj + "\" for parameter \"" + getOptionID().getName() + "\" is of unknown type!"); 90 } 91 92 /** 93 * Returns a string representation of the parameter's type. 94 * 95 * @return "<pattern>" 96 */ 97 @Override getSyntax()98 public String getSyntax() { 99 return "<pattern>"; 100 } 101 } 102