directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r1626937 - /directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/Tuple.java
Date Tue, 23 Sep 2014 04:46:58 GMT
Author: elecharny
Date: Tue Sep 23 04:46:58 2014
New Revision: 1626937

URL: http://svn.apache.org/r1626937
Log:
o The class now implements Comparable
o Added a Comparator for the keys
o Added the missing hasCode() and equals() methods

Modified:
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/Tuple.java

Modified: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/Tuple.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/Tuple.java?rev=1626937&r1=1626936&r2=1626937&view=diff
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/Tuple.java
(original)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/Tuple.java
Tue Sep 23 04:46:58 2014
@@ -20,6 +20,9 @@
 package org.apache.directory.mavibot.btree;
 
 
+import java.util.Comparator;
+
+
 /**
  * The Tuple class is used when we browse a btree, it will contain the results
  * fetched from the btree.
@@ -29,7 +32,7 @@ package org.apache.directory.mavibot.btr
  * @param <K> The type for the Key
  * @param <V> The type for the stored value
  */
-public class Tuple<K, V>
+public class Tuple<K, V> implements Comparable<Tuple<K, V>>
 {
     /** The key */
     protected K key;
@@ -37,6 +40,9 @@ public class Tuple<K, V>
     /** The value */
     protected V value;
 
+    /** The key comparator */
+    protected Comparator<K> keyComparator;
+
 
     /**
      * Creates a Tuple with no content
@@ -59,6 +65,19 @@ public class Tuple<K, V>
 
 
     /**
+     * Creates a Tuple containing a key and its associated value.
+     * @param key The key
+     * @param value The associated value
+     */
+    public Tuple( K key, V value, Comparator<K> keyComparator )
+    {
+        this.key = key;
+        this.value = value;
+        this.keyComparator = keyComparator;
+    }
+
+
+    /**
      * @return the key
      */
     public K getKey()
@@ -95,6 +114,77 @@ public class Tuple<K, V>
 
 
     /**
+     * @see Object#hashCode()
+     */
+    @Override
+    public int hashCode()
+    {
+        return key.hashCode();
+    }
+
+
+    /**
+     * @see Object#equals()
+     */
+    @SuppressWarnings("unchecked")
+    @Override
+    public boolean equals( Object obj )
+    {
+        if ( this == obj )
+        {
+            return true;
+        }
+
+        if ( !( obj instanceof Tuple ) )
+        {
+            return false;
+        }
+
+        if ( this.key == null )
+        {
+            return ( ( Tuple<K, V> ) obj ).key == null;
+        }
+
+        return this.key.equals( ( ( Tuple<K, V> ) obj ).key );
+    }
+
+
+    /**
+     * @see java.lang.Comparable#compareTo(java.lang.Object)
+     */
+    @Override
+    public int compareTo( Tuple<K, V> t )
+    {
+        if ( keyComparator != null )
+        {
+            return keyComparator.compare( key, t.key );
+        }
+        else
+        {
+            return 0;
+        }
+    }
+
+
+    /**
+     * @return the keyComparator
+     */
+    public Comparator<K> getKeyComparator()
+    {
+        return keyComparator;
+    }
+
+
+    /**
+     * @param keyComparator the keyComparator to set
+     */
+    public void setKeyComparator( Comparator<K> keyComparator )
+    {
+        this.keyComparator = keyComparator;
+    }
+
+
+    /**
      * @see Object#toString()
      */
     public String toString()



Mime
View raw message