📄 parameters.java
字号:
/*
* YALE - Yet Another Learning Environment
* Copyright (C) 2001-2004
* Simon Fischer, Ralf Klinkenberg, Ingo Mierswa,
* Katharina Morik, Oliver Ritthoff
* Artificial Intelligence Unit
* Computer Science Department
* University of Dortmund
* 44221 Dortmund, Germany
* email: yale-team@lists.sourceforge.net
* web: http://yale.cs.uni-dortmund.de/
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*/
package edu.udo.cs.yale.operator.parameter;
import edu.udo.cs.yale.tools.LogService;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.Set;
/** This class is a collection of the parameter values of a single operator.
* Instances of <code>Parameters</code> are created with respect to the
* declared list of <code>ParameterTypes</code> of an operator. If parameters
* are set using the <code>setParameter()</code> method and the value exceeds
* the range, it is automatically corrected. If parameters are queried that
* are not set, their default value is returned.
*
* @version $Id: Parameters.java,v 2.9 2004/08/27 11:57:42 ingomierswa Exp $
*/
public class Parameters {
/** Maps parameter keys (i.e. Strings) to their value (Objects). */
private Map keyToValueMap = new HashMap();
/** Maps parameter keys (i.e. Strings) to their <code>ParameterType</code>. */
private Map keyToTypeMap = new HashMap();
/** Constructs an instance of <code>Parameters</code> for the given list of
* <code>ParameterTypes</code>. */
public Parameters(List parameterTypes) {
Iterator i = parameterTypes.iterator();
while (i.hasNext()) {
ParameterType type = (ParameterType)i.next();
keyToTypeMap.put(type.getKey(), type);
}
}
/** Performs a deep clone on this parameters. */
public Object clone() {
Parameters clone = new Parameters(new java.util.LinkedList());
Iterator i = keyToValueMap.keySet().iterator();
while (i.hasNext()) {
Object key = i.next();
clone.keyToValueMap.put(key, keyToValueMap.get(key));
}
i = keyToTypeMap.keySet().iterator();
while (i.hasNext()) {
Object key = i.next();
clone.keyToTypeMap.put(key, keyToTypeMap.get(key));
}
return clone;
}
/** Returns the type of the parameter with the given type. */
public ParameterType getParameterType(String key) {
return (ParameterType)keyToTypeMap.get(key);
}
/** Sets the parameter for the given key after performing a range-check. */
public void setParameter(String key, Object value) {
ParameterType type = (ParameterType)keyToTypeMap.get(key);
if (type == null) {
LogService.logMessage("Illegal key: '"+key+"'", LogService.WARNING);
if (value instanceof List) {
type = new ParameterTypeList(key, "guessed", new ParameterTypeString(null, null));
} else {
type = new ParameterTypeString(key, "guessed");
}
keyToTypeMap.put(key, type);
}
keyToValueMap.put(key, type.checkValue(value));
}
/** Sets the parameter without performing a range check. */
public void setParameterWithoutCheck(String key, Object value) {
if (value == null) {
keyToValueMap.remove(key);
} else {
keyToValueMap.put(key, value);
}
}
/** Returns the value of the given parameter. If it was not yet set, the default value is
* set now and a log message is issued. If the <code>ParameterType</code> does not
* provide a default value, this may result in an error message. In subsequent calls
* of this method, the parameter will be set. */
public Object getParameter(String key) {
if (keyToValueMap.containsKey(key)) {
return keyToValueMap.get(key);
} else {
ParameterType type = (ParameterType)keyToTypeMap.get(key);
if (type == null) { return null; }
Object value = type.getDefaultValue();
if ((value == null) && !type.isOptional()) {
LogService.logMessage("Parameter '"+key+"' is not set and has no default value.", LogService.ERROR);
} else {
keyToValueMap.put(key, value);
LogService.logMessage("Parameter '"+key+"' is not set. Using default ('"+type.toString(value)+"').", LogService.MINIMUM);
}
return value;
}
}
/** Returns a set view of all parameter keys. */
public Set getKeys() {
return keyToValueMap.keySet();
}
/** Returns true if the given parameters are not null and are the same like this parameters. */
public boolean equals(Object o) {
if (o == null)
return false;
if (o instanceof Parameters) {
Parameters params = (Parameters)o;
if (params.keyToValueMap.size() != this.keyToValueMap.size())
return false;
Iterator i = this.keyToValueMap.keySet().iterator();
while (i.hasNext()) {
Object key = i.next();
Object value = params.keyToValueMap.get(key);
if ((value == null) || !value.equals(this.keyToValueMap.get(key)))
return false;
}
return true;
} else {
return false;
}
}
/** Writes a portion of the xml configuration file specifying the parameters
* that differ from their default value. */
public String getXML(String indent) {
StringBuffer result = new StringBuffer();
Iterator i = keyToValueMap.keySet().iterator();
while (i.hasNext()) {
String key = (String)i.next();
Object value = keyToValueMap.get(key);
ParameterType type = (ParameterType)keyToTypeMap.get(key);
if (type != null) {
result.append(type.getXML(indent, key, value));
} else {
result.append(indent + "<parameter key=\""+key+"\"\tvalue=\"" + value.toString() + "\"/>\n");
}
}
return result.toString();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -