Skip to content

Instantly share code, notes, and snippets.

@rmcdouga
Created February 26, 2021 18:44
Show Gist options
  • Save rmcdouga/56f33c70f38268ff87f844dc3cc50cc7 to your computer and use it in GitHub Desktop.
Save rmcdouga/56f33c70f38268ff87f844dc3cc50cc7 to your computer and use it in GitHub Desktop.
package com.github.rmcdouga.gist;
import java.io.Serializable;
/**
* <p>A convenience class to represent name-value pairs.</p>
*
* Code was blatantly stolen from javafx.util.Pair class
*
* @param <K>
* @param <V>
*/
public class Pair<K, V> implements Serializable {
/**
* Key of this <code>Pair</code>.
*/
private K key;
/**
* Gets the key for this pair.
* @return key for this pair
*/
public K getKey() {
return key;
}
/**
* Value of this this <code>Pair</code>.
*/
private V value;
/**
* Gets the value for this pair.
* @return value for this pair
*/
public V getValue() {
return value;
}
/**
* Creates a new pair
* @param key The key for this pair
* @param value The value to use for this pair
*/
public Pair(K key, V value) {
this.key = key;
this.value = value;
}
/**
* <p><code>String</code> representation of this
* <code>Pair</code>.</p>
*
* <p>The default name/value delimiter '=' is always used.</p>
*
* @return <code>String</code> representation of this <code>Pair</code>
*/
@Override
public String toString() {
return key + "=" + value;
}
// /**
// * <p>Generate a hash code for this <code>Pair</code>.</p>
// *
// * <p>The hash code is calculated using both the name and
// * the value of the <code>Pair</code>.</p>
// *
// * @return hash code for this <code>Pair</code>
// */
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((key == null) ? 0 : key.hashCode());
result = prime * result + ((value == null) ? 0 : value.hashCode());
return result;
}
/**
* <p>Test this <code>Pair</code> for equality with another
* <code>Object</code>.</p>
*
* <p>If the <code>Object</code> to be tested is not a
* <code>Pair</code> or is <code>null</code>, then this method
* returns <code>false</code>.</p>
*
* <p>Two <code>Pair</code>s are considered equal if and only if
* both the names and values are equal.</p>
*
* @param o the <code>Object</code> to test for
* equality with this <code>Pair</code>
* @return <code>true</code> if the given <code>Object</code> is
* equal to this <code>Pair</code> else <code>false</code>
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Pair other = (Pair) obj;
if (key == null) {
if (other.key != null)
return false;
} else if (!key.equals(other.key))
return false;
if (value == null) {
if (other.value != null)
return false;
} else if (!value.equals(other.value))
return false;
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment