/*
* Debellor
*
* Copyright (C) 2008-2009 by Marcin Wojnarski
*
* 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 3 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, see http://www.gnu.org/licenses/.
*/
package org.debellor.core.parameters;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import org.debellor.core.Cell;
import org.debellor.core.Parameters;
import org.debellor.core.Parameters.Entry;
import org.debellor.core.util.DeepCopy;
/**
* Holds information about all available parameters of a cell.
* Can be printed to a <code>PrintStream</code> in a readable text form.
*
* @author Marcin Wojnarski
*
*/
public class ParametersInfo implements Serializable {
private static final long serialVersionUID = 1L;
private Parameters defaults = new Parameters();
private Map<String,String> meanings = new HashMap<String,String>();
public ParametersInfo() {}
/**
* Creates ParametersInfo with initial content passed in <code>entries</code>.
*
* @param entries
* Names, default values and meanings of consecutive parameters, given in the following order:
* <p><code>
* name1, default1, meaning1,
* name2, default2, meaning2,
* ...
* </code>
* <p>Caution: do <i>not</i> omit any element of the triple.
* If you do not want to give meaning or a sensible default value for some parameter,
* put <code>null</code> instead.
*/
public ParametersInfo(String... entries) {
for(int i = 0; i < entries.length; i += 3)
set(entries[i], entries[i+1], entries[i+2]);
}
/**
* Add new parameter together with its default value.
*
* @param name
* @param value default value
* @see org.debellor.core.Parameters#set(java.lang.String, java.lang.String)
*/
public void set(String name, String value) {
set(name, value, null);
}
/**
* Add new parameter together with its default value and description of meaning.
*
* @param name
* @param value default value
* @param meaning description of meaning and usage, preferably as a single line of text,
* without end-of-line at the end
* @see org.debellor.core.Parameters#set(java.lang.String, java.lang.String)
*/
public void set(String name, String value, String meaning) {
defaults.set(name, value);
if(meaning != null)
meanings.put(name, meaning);
else
meanings.remove(name);
}
/** Returns default values of parameters as a {@link Parameters} instance
* that can be passed directly to {@link Cell#setParameters}. */
public Parameters getDefaultValues() {
return defaults.copy();
}
/** Returns the number of parameters defined in this ParametersInfo. */
public int size() {
return defaults.size();
}
/** Returns deep copy of this object */
public ParametersInfo copy() {
return (ParametersInfo) DeepCopy.copy(this);
}
@Override
public String toString() {
if(defaults.size() == 0) return "No settable parameters";
String s = ""; // "Available parameters. Default values given in brackets\n";
for(int i = 0; i < defaults.size(); i++) {
Entry e = defaults.getEntry(i);
s += e.name + " [";
s += (e.value == null) ? "null" : e.value;
s += "]";
String m = meanings.get(e.name);
if(m != null)
s += " -- " + m;
s += "\n";
}
return s;
}
}