I'm overcome with a feeling of propriety and am thus making Tuple immutable and

changing create() to newTuple() to perpetuate "best practices".


git-svn-id: https://samskivert.googlecode.com/svn/trunk@2483 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
samskivert
2008-11-18 21:12:08 +00:00
parent 30d558785a
commit 9f69792fc5
2 changed files with 20 additions and 39 deletions
@@ -35,13 +35,6 @@ public class ComparableTuple<L extends Comparable<? super L>, R extends Comparab
super(left, right); super(left, right);
} }
/**
* Constructs a blank tuple.
*/
public ComparableTuple ()
{
}
// from interface Comparable // from interface Comparable
public int compareTo (ComparableTuple<L, R> other) public int compareTo (ComparableTuple<L, R> other)
{ {
+20 -32
View File
@@ -23,72 +23,60 @@ package com.samskivert.util;
import java.io.Serializable; import java.io.Serializable;
/** /**
* A tuple is a simple object that holds a reference to two other objects. * A tuple is a simple object that holds a reference to two other objects. It provides hashcode and
* It provides hashcode and equality semantics that allow it to be used to * equality semantics that allow it to be used to combine two objects into a single key (for
* combine two objects into a single key (for hashtables, etc.). * hashtables, etc.).
*/ */
public class Tuple<L,R> implements Serializable public class Tuple<L,R> implements Serializable
{ {
/** The left object. */ /** The left object. */
public L left; public final L left;
/** The right object. */ /** The right object. */
public R right; public final R right;
/** /**
* Creates a tuple with the specified two objects. * Creates a tuple with the specified two objects.
*/ */
public static <L, R> Tuple<L, R> create (L left, R right) public static <L, R> Tuple<L, R> newTuple (L left, R right)
{ {
return new Tuple<L, R>(left, right); return new Tuple<L, R>(left, right);
} }
/** Construct a tuple with the specified two objects. */ /**
* Constructs a tuple with the supplied values.
*/
public Tuple (L left, R right) public Tuple (L left, R right)
{ {
this.left = left; this.left = left;
this.right = right; this.right = right;
} }
/** Construct a blank tuple. */ @Override // from Object
public Tuple ()
{
}
/**
* Returns the combined hashcode of the two elements.
*/
@Override
public int hashCode () public int hashCode ()
{ {
return left.hashCode() ^ right.hashCode(); int value = 17;
value = value * 31 + ((left == null) ? 0 : left.hashCode());
value = value * 31 + ((right == null) ? 0 : right.hashCode());
return value;
} }
/** @Override // from Object
* A tuple is equal to another tuple if the left and right elements
* are equal to the left and right elements (respectively) of the
* other tuple.
*/
@Override
public boolean equals (Object other) public boolean equals (Object other)
{ {
if (other instanceof Tuple) { if (!(other instanceof Tuple)) {
Tuple<?, ?> to = (Tuple<?, ?>)other;
return (left.equals(to.left) && right.equals(to.right));
} else {
return false; return false;
} }
Tuple<?, ?> to = (Tuple<?, ?>)other;
return ObjectUtil.equals(left, to.left) && ObjectUtil.equals(right, to.right);
} }
@Override @Override // from Object
public String toString () public String toString ()
{ {
return "[left=" + left + ", right=" + right + "]"; return "[left=" + left + ", right=" + right + "]";
} }
/** Change this if the fields or inheritance hierarchy ever changes /** Don't you go a changin'. */
* (which is extremely unlikely). We override this because I'm tired
* of serialized crap not working depending on whether I compiled with
* jikes or javac. */
private static final long serialVersionUID = 1; private static final long serialVersionUID = 1;
} }