tim@335: package de.intevation.gnv.state;
tim@335:
ingo@634: import java.util.HashMap;
ingo@634: import java.util.Map;
ingo@634:
ingo@634: import org.apache.log4j.Logger;
ingo@634:
tim@335: /**
ingo@796: * The default implementation of InputData
. This object stores
ingo@796: * multiple values separated by {@link #VALUE_SEPARATOR} for a specific key.
sascha@803: *
sascha@780: * @author Tim Englich
sascha@780: * @author Ingo Weinzierl
tim@335: */
tim@335: public class DefaultInputData implements InputData {
tim@335:
tim@335: /**
tim@335: *
tim@335: */
tim@335: private static final long serialVersionUID = 4308041648698108066L;
ingo@634:
ingo@634: private static final Logger logger = Logger.getLogger(DefaultInputData.class);
ingo@634:
ingo@796: /**
ingo@796: * The character used to separate the different values.
ingo@796: */
ingo@785: public final static String VALUE_SEPARATOR = " , ";
tim@598:
ingo@796: /**
ingo@796: * The key/name of this object.
ingo@796: */
ingo@634: protected String name;
ingo@796:
ingo@796: /**
ingo@796: * Values separated by {@link #VALUE_SEPARATOR}.
ingo@796: */
ingo@634: protected String value;
ingo@796:
ingo@796: /**
ingo@796: * Descriptions for values used for user interface creation. Each value
ingo@796: * should have an own description in this map.
ingo@796: */
ingo@634: protected Map description;
sascha@803:
ingo@796: /**
ingo@796: * An extra object. Might be everything.
ingo@796: */
ingo@634: protected Object object;
ingo@634:
tim@335:
tim@335: /**
tim@335: * Constructor
tim@335: */
tim@335: public DefaultInputData(String name, String value) {
tim@335: this.name = name;
tim@335: this.value = value;
tim@335: }
tim@335:
ingo@815:
ingo@607: public DefaultInputData(String name, Object object) {
ingo@607: this.name = name;
ingo@607: this.object = object;
ingo@607: }
ingo@607:
ingo@815:
ingo@785: public DefaultInputData(
ingo@785: String name,
ingo@785: String value,
ingo@785: Object object)
ingo@785: {
ingo@785: this.name = name;
ingo@785: this.object = object;
ingo@785: this.value = value;
ingo@785: }
ingo@785:
ingo@815:
tim@335: public String getName() {
tim@335: return this.name;
tim@335: }
tim@335:
ingo@815:
tim@335: public String getValue() {
tim@335: return this.value;
tim@335: }
tim@335:
ingo@815:
ingo@607: public void setObject(Object object) {
ingo@607: this.object = object;
ingo@607: }
ingo@607:
ingo@815:
ingo@607: public Object getObject() {
ingo@607: return object;
ingo@607: }
ingo@607:
ingo@796: /**
ingo@796: *
ingo@796: * @param key Key needs to be a single value of {@link #value}.
ingo@796: * @return the description.
ingo@796: */
ingo@634: public String getDescription(String key) {
ingo@634: if (description == null)
ingo@634: return null;
ingo@634:
ingo@634: return (String) description.get(key);
ingo@634: }
ingo@634:
ingo@796: /**
ingo@796: * Return all descriptions as array.
sascha@803: *
ingo@796: * @return descriptions as array.
ingo@796: */
ingo@615: public String[] getDescription() {
ingo@634: String[] values = splitValue();
ingo@634: int length = values.length;
ingo@634:
ingo@634: String[] description = new String[length];
ingo@634: for (int i = 0; i < length; i++) {
ingo@634: description[i] = (String) this.description.get(values[i]);
ingo@634: }
ingo@634:
ingo@607: return description;
ingo@607: }
ingo@607:
ingo@815:
ingo@615: public void setDescription(String[] description) {
ingo@634: if (this.description == null)
ingo@634: this.description = new HashMap();
ingo@634:
ingo@634: String[] values = splitValue();
ingo@634:
ingo@634: int length = values.length;
ingo@634: int descLength = description.length;
ingo@634:
ingo@634: for (int i = 0; i < length; i++) {
ingo@634: if (i < descLength) {
ingo@634: this.description.put(values[i], description[i]);
ingo@634: }
ingo@634: else {
ingo@634: break;
ingo@634: }
ingo@634: }
ingo@607: }
ingo@607:
ingo@815:
tim@335: @Override
tim@335: public String toString() {
tim@335: return this.name + "==> " + this.value;
tim@335: }
tim@335:
ingo@815:
tim@335: public void concartValue(String value) {
tim@598: this.value = this.value + VALUE_SEPARATOR + value;
tim@598: }
tim@598:
ingo@815:
tim@598: public String[] splitValue() {
tim@598: if (this.value != null){
tim@598: return this.value.split(VALUE_SEPARATOR);
tim@598: }
tim@598: return null;
tim@335: }
tim@335:
ingo@1030: @Override
ingo@1030: public int hashCode() {
ingo@1030: logger.debug("*************************************");
ingo@1030: logger.debug("HashCode name: " + name);
ingo@1030:
ingo@1030: int hash = 0;
ingo@1030:
ingo@1030: hash ^= name.hashCode();
ingo@1030:
ingo@1030: if (value != null) {
ingo@1030: hash ^= value.hashCode() << 2;
ingo@1030: }
ingo@1030:
ingo@1030: if (object != null) {
ingo@1030: hash ^= object.hashCode() << 4;
ingo@1030: }
ingo@1030:
ingo@1030: logger.debug("HashCode value: " + hash);
ingo@1030: logger.debug("*************************************");
ingo@1030:
ingo@1030: return hash;
ingo@1030: }
ingo@1030:
ingo@1030:
ingo@1030: @Override
ingo@1030: public boolean equals(Object o) {
ingo@1030: if (!(o instanceof DefaultInputData))
ingo@1030: return false;
ingo@1030:
ingo@1030: DefaultInputData other = (DefaultInputData) o;
ingo@1030:
ingo@1030: if (!name.equals(other.name))
ingo@1030: return false;
ingo@1030:
ingo@1030: if ((value == null) && (other.value == null))
ingo@1030: return false;
ingo@1030:
ingo@1030: if (!value.equals(other.value))
ingo@1030: return false;
ingo@1030:
ingo@1030: if (!(object == null) && (other.object == null))
ingo@1030: return false;
ingo@1030:
ingo@1030: if (!(object == other.object))
ingo@1030: return false;
ingo@1030:
ingo@1030: return true;
ingo@1030: }
tim@335: }
sascha@836: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :