view gnv-artifacts/src/main/java/de/intevation/gnv/state/DefaultInputData.java @ 1115:f953c9a559d8

Added license file and license headers. gnv-artifacts/trunk@1260 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 02 Nov 2010 17:46:55 +0000
parents c07d9f9a738c
children
line wrap: on
line source
/*
 * Copyright (c) 2010 by Intevation GmbH
 *
 * This program is free software under the LGPL (>=v2.1)
 * Read the file LGPL.txt coming with the software for details
 * or visit http://www.gnu.org/licenses/ if it does not exist.
 */

package de.intevation.gnv.state;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

/**
 * The default implementation of <code>InputData</code>. This object stores
 * multiple values separated by {@link #VALUE_SEPARATOR} for a specific key.
 *
 * @author <a href="mailto:tim.englich@intevation.de">Tim Englich</a>
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public class DefaultInputData implements InputData {

    /**
     *
     */
    private static final long serialVersionUID = 4308041648698108066L;

    private static final Logger logger = Logger.getLogger(DefaultInputData.class);

    /**
     * The character used to separate the different values.
     */
    public final static String VALUE_SEPARATOR = " , ";

    /**
     * The key/name of this object.
     */
    protected String name;

    /**
     * Values separated by {@link #VALUE_SEPARATOR}.
     */
    protected String value;

    /**
     * Descriptions for values used for user interface creation. Each value
     * should have an own description in this map.
     */
    protected Map    description;

    /**
     * An extra object. Might be everything.
     */
    protected Object object;


    /**
     * Constructor
     */
    public DefaultInputData(String name, String value) {
        this.name = name;
        this.value = value;
    }


    public DefaultInputData(String name, Object object) {
        this.name   = name;
        this.object = object;
    }


    public DefaultInputData(
        String name,
        String value,
        Object object)
    {
        this.name        = name;
        this.object      = object;
        this.value       = value;
    }


    public String getName() {
        return this.name;
    }


    public String getValue() {
        return this.value;
    }


    public void setObject(Object object) {
        this.object = object;
    }


    public Object getObject() {
        return object;
    }

    /**
     *
     * @param key Key needs to be a single value of {@link #value}.
     * @return the description.
     */
    public String getDescription(String key) {
        if (description == null)
            return null;

        return (String) description.get(key);
    }

    /**
     * Return all descriptions as array.
     *
     * @return descriptions as array.
     */
    public String[] getDescription() {
        String[] values      = splitValue();
        int length           = values.length;

        String[] description = new String[length];
        for (int i = 0; i < length; i++) {
            description[i] = (String) this.description.get(values[i]);
        }

        return description;
    }


    public void setDescription(String[] description) {
        if (this.description == null)
            this.description = new HashMap();

        String[] values = splitValue();

        int length     = values.length;
        int descLength = description.length;

        for (int i = 0; i < length; i++) {
            if (i < descLength) {
                this.description.put(values[i], description[i]);
            }
            else {
                break;
            }
        }
    }


    @Override
    public String toString() {
        return this.name + "==> " + this.value;
    }


    public void concartValue(String value) {
        this.value = this.value + VALUE_SEPARATOR + value;
    }


    public String[] splitValue() {
        if (this.value != null){
            return this.value.split(VALUE_SEPARATOR);
        }
        return null;
    }

    @Override
    public int hashCode() {
        logger.debug("*************************************");
        logger.debug("HashCode name: " + name);

        int hash = 0;

        hash ^= name.hashCode();

        if (value != null) {
            hash ^= value.hashCode() << 2;
        }

        if (object != null) {
            hash ^= object.hashCode() << 4;
        }

        logger.debug("HashCode value: " + hash);
        logger.debug("*************************************");

        return hash;
    }


    @Override
    public boolean equals(Object o) {
        if (!(o instanceof DefaultInputData))
            return false;

        DefaultInputData other = (DefaultInputData) o;

        if (!name.equals(other.name))
            return false;

        if ((value == null) && (other.value == null))
            return false;

        if (!value.equals(other.value))
            return false;

        if (!(object == null) && (other.object == null))
            return false;

        if (!(object == other.object))
            return false;

        return true;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org