Add equals() and hashCode() implementations that allow a Tuple to be used
as a HashMap key. git-svn-id: https://samskivert.googlecode.com/svn/trunk@470 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
//
|
||||
// $Id: Tuple.java,v 1.2 2001/08/11 22:43:29 mdb Exp $
|
||||
// $Id: Tuple.java,v 1.3 2001/11/26 19:22:12 mdb Exp $
|
||||
//
|
||||
// samskivert library - useful routines for java programs
|
||||
// Copyright (C) 2001 Michael Bayne
|
||||
@@ -22,6 +22,8 @@ package com.samskivert.util;
|
||||
|
||||
/**
|
||||
* A tuple is a simple object that holds a reference to two other objects.
|
||||
* It provides hashcode and equality semantics that allow it to be used to
|
||||
* combine two objects into a single key (for hashtables, etc.).
|
||||
*/
|
||||
public class Tuple
|
||||
{
|
||||
@@ -43,6 +45,32 @@ public class Tuple
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the combined hashcode of the two elements.
|
||||
*/
|
||||
public int hashCode ()
|
||||
{
|
||||
return left.hashCode() ^ right.hashCode();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
public boolean equals (Object other)
|
||||
{
|
||||
if (other instanceof Tuple) {
|
||||
Tuple to = (Tuple)other;
|
||||
return (left.equals(to.left) && right.equals(to.right));
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a string representation of this tuple.
|
||||
*/
|
||||
public String toString ()
|
||||
{
|
||||
return "[left=" + left + ", right=" + right + "]";
|
||||
|
||||
Reference in New Issue
Block a user