Pair.java
/***************************************************************************
Copyright 2008 Emily Estes
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
***************************************************************************/
package net.metanotion.util;
import java.util.Map;
/** Sometimes you need a tuple/pair of values. This class also provides a
concrete implementation of Map.Entry.
@param <A> The type of the first value/key.
@param <B> The type of the second value/value.
*/
public final class Pair<A,B> implements Map.Entry<A,B> {
private final A a;
private B b;
/** Construct a tuple/pair of values.
@param first The first member of the tuple.
@param second The second member of the tuple.
*/
public Pair(final A first, final B second) {
a = first;
b = second;
}
@Override public A getKey() { return a; }
@Override public B getValue() { return b; }
@Override public B setValue(final B value) {
final B b1 = b;
b = value;
return b1;
}
@Override public boolean equals(final Object o) {
if(o == this) { return true; }
if(!(o instanceof Map.Entry)) { return false; }
final Map.Entry c = (Map.Entry) o;
final Object a2 = c.getKey();
final Object b2 = c.getValue();
return ((a!=null && a.equals(a2)) || (a==null && a2==null))
&& ((b!=null && b.equals(b2)) || (b==null && b2==null));
}
@Override public int hashCode() { return ((a != null) ? a.hashCode() : 1) + ((b != null) ? b.hashCode() : 2); }
}