com.github.TKnudsen.ComplexDataObject.data.entry.EntryWithComparableKey Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of complex-data-object Show documentation
Show all versions of complex-data-object Show documentation
A library that models real-world objects in Java, referred to as ComplexDataObjects. Other features: IO and preprocessing of ComplexDataObjects.
The newest version!
package com.github.TKnudsen.ComplexDataObject.data.entry;
import java.io.Serializable;
import java.util.Map;
/**
*
* Title: EntryWithComparableKey
*
*
*
* Description: Key-Value pair implementation of {@link Map.Entry}, with a key
* that extends {@link Comparable}.
*
* Tip: you can also use AbstractMap.SimpleEntry as a simple alternative. Then,
* a comparable key can be obtained by Collections.sort(collection,
* Entry.comparingByKey());
*
*
*
* Copyright: Copyright (c) 2015-2016
*
*
* @author Juergen Bernard
* @version 1.03
*/
public class EntryWithComparableKey, V>
implements Comparable>, Map.Entry, Serializable {
/**
*
*/
private static final long serialVersionUID = -1835568089597747740L;
private K key;
private V value;
public EntryWithComparableKey(K key, V value) {
this.key = key;
this.value = value;
}
@Override
public K getKey() {
return key;
}
public void setKey(K key) {
this.key = key;
}
@Override
public V getValue() {
return value;
}
@Override
public V setValue(V value) {
this.value = value;
return this.value;
}
@Override
public int compareTo(EntryWithComparableKey o) {
return key.compareTo(o.getKey());
}
@Override
public boolean equals(Object o) {
if (o instanceof EntryWithComparableKey) {
try {
@SuppressWarnings("unchecked")
EntryWithComparableKey p = (EntryWithComparableKey) o;
return p.getValue().equals(getValue()) && p.getKey().equals(getKey());
} catch (Exception e) {
return false;
}
}
return false;
}
public String toString() {
return "[Entry with comparable key. Key: " + key + ", Value: " + value + "]";
}
public String getName() {
return toString();
}
@Override
public int hashCode() {
int result = 19;
if (key != null)
result = 29 * result + key.hashCode();
if (value != null)
result = 29 * result + value.hashCode();
return result;
}
public String getDescription() {
return toString();
}
}