All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.shijingsh.core.utility.KeyValue Maven / Gradle / Ivy

The newest version!
package com.shijingsh.core.utility;

import java.util.Map.Entry;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

import com.shijingsh.core.common.conversion.csv.annotation.CsvConfiguration;

/**
 * 键值对
 *
 * @author Birdy
 *
 * @param 
 * @param 
 */
@CsvConfiguration({ "key", "value" })
public class KeyValue {

    /** 键 */
    private K key;

    /** 值 */
    private V value;

    KeyValue() {
    }

    public KeyValue(Entry term) {
        this.key = term.getKey();
        this.value = term.getValue();
    }

    public KeyValue(K key, V value) {
        this.key = key;
        this.value = value;
    }

    /**
     * 设置键值
     *
     * @param term
     */
    public void setKeyValue(Entry term) {
        this.key = term.getKey();
        this.value = term.getValue();
    }

    /**
     * 获取键
     *
     * @return
     */
    public K getKey() {
        return key;
    }

    /**
     * 设置键
     *
     * @param newKey
     * @return
     */
    public K setKey(K newKey) {
        K oldKey = key;
        key = newKey;
        return oldKey;
    }

    /**
     * 获取值
     *
     * @return
     */
    public V getValue() {
        return value;
    }

    /**
     * 设置值
     *
     * @param newValue
     * @return
     */
    public V setValue(V newValue) {
        V oldValue = value;
        value = newValue;
        return oldValue;
    }

    @Override
    public boolean equals(Object object) {
        if (this == object)
            return true;
        if (object == null)
            return false;
        if (getClass() != object.getClass())
            return false;
        KeyValue that = (KeyValue) object;
        EqualsBuilder equal = new EqualsBuilder();
        equal.append(this.key, that.key);
        equal.append(this.value, that.value);
        return equal.isEquals();
    }

    @Override
    public int hashCode() {
        HashCodeBuilder hash = new HashCodeBuilder();
        hash.append(key);
        hash.append(value);
        return hash.toHashCode();
    }

    @Override
    public String toString() {
        return "KeyValue [key=" + key + ", value=" + value + "]";
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy