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

panda.lang.collection.KeyValue Maven / Gradle / Ivy

Go to download

Panda Core is the core module of Panda Framework, it contains commonly used utility classes similar to apache-commons.

There is a newer version: 1.8.0
Show newest version
package panda.lang.collection;

import java.util.Map;
import java.util.Map.Entry;

import panda.lang.Objects;

/**
 * @param  the Key Type
 * @param  the Value Type
 */
public class KeyValue implements Entry {

	/** The key */
	protected K key;
	
	/** The value */
	protected V value;

	/**
	 * Constructs a new pair with the specified key and given value.
	 * 
	 * @param key the key for the entry, may be null
	 * @param value the value for the entry, may be null
	 */
	public KeyValue(K key, V value) {
		super();
		this.key = key;
		this.value = value;
	}

	/**
	 * Gets the key from the pair.
	 * 
	 * @return the key
	 */
	public K getKey() {
		return key;
	}

	/**
	 * @param key the key to set
	 */
	public void setKey(K key) {
		this.key = key;
	}

	/**
	 * Gets the value from the pair.
	 * 
	 * @return the value
	 */
	public V getValue() {
		return value;
	}

	/**
	 * {@inheritDoc}
	 */
	public V setValue(V value) {
		V old = value;
		this.value = value;
		return old;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public int hashCode() {
		return Objects.hashCodes(key, value);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof Map.Entry)) {
			return false;
		}

		Map.Entry rhs = (Map.Entry)obj;
		return Objects.equalsBuilder().append(key, rhs.getKey()).append(value, rhs.getValue()).isEquals();
	}

	/**
	 * Gets a debugging String view of the pair.
	 * 
	 * @return a String view of the entry
	 */
	@Override
	public String toString() {
		return key + "=" + value;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy