directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r544541 - /directory/shared/trunk/asn1/src/main/java/org/apache/directory/shared/asn1/primitives/OID.java
Date Tue, 05 Jun 2007 16:20:36 GMT
Author: elecharny
Date: Tue Jun  5 09:20:35 2007
New Revision: 544541

URL: http://svn.apache.org/viewvc?view=rev&rev=544541
Log:
Reformated and completed some javadoc
Added a hashCode and equals methods

Modified:
    directory/shared/trunk/asn1/src/main/java/org/apache/directory/shared/asn1/primitives/OID.java

Modified: directory/shared/trunk/asn1/src/main/java/org/apache/directory/shared/asn1/primitives/OID.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/asn1/src/main/java/org/apache/directory/shared/asn1/primitives/OID.java?view=diff&rev=544541&r1=544540&r2=544541
==============================================================================
--- directory/shared/trunk/asn1/src/main/java/org/apache/directory/shared/asn1/primitives/OID.java
(original)
+++ directory/shared/trunk/asn1/src/main/java/org/apache/directory/shared/asn1/primitives/OID.java
Tue Jun  5 09:20:35 2007
@@ -21,6 +21,7 @@
 
 
 import java.io.Serializable;
+import java.util.Arrays;
 
 import org.apache.directory.shared.asn1.codec.DecoderException;
 import org.apache.directory.shared.asn1.util.Asn1StringUtils;
@@ -52,7 +53,10 @@
  * 1.2   -> 0x2A (1*40 + 2 = 42 = 0x2A) 
  * 840   -> 0x86 0x48 (840 = 6 * 128 + 72 = (0x06 | 0x80) 0x48 = 0x86 0x48
  * 48018 -> 0x82 0xF7 0x12 (2 * 128 * 128 + 119 * 128 + 18 = (0x02 | 0x80) (0x77 | 0x80)
0x12
- * 
+ * .1    -> 0x01
+ * .2    -> 0x02
+ * .2    -> 0x02
+ *  
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  */
 public class OID implements Serializable
@@ -64,6 +68,9 @@
 
     /** The OID as a array of int */
     private long[] oidValues;
+    
+    /** Th hashcode, computed only once */
+    private int hash;
 
 
     // ~ Constructors
@@ -74,9 +81,9 @@
      */
     public OID()
     {
-
         // We should not create this kind of object directly, it must
         // be created through the factory.
+    	hash = 0;
     }
 
 
@@ -85,9 +92,10 @@
      * 
      * @param oid
      */
-    public OID(byte[] oid) throws DecoderException
+    public OID( byte[] oid ) throws DecoderException
     {
         setOID( oid );
+        hash = computeHashCode();
     }
 
 
@@ -96,9 +104,10 @@
      * 
      * @param oid The String which is supposed to be an OID
      */
-    public OID(String oid) throws DecoderException
+    public OID( String oid ) throws DecoderException
     {
         setOID( oid );
+        hash = computeHashCode();
     }
 
 
@@ -195,6 +204,8 @@
 
             pos++;
         }
+        
+        hash = computeHashCode();
     }
 
 
@@ -326,6 +337,8 @@
         }
 
         oidValues[intPos++] = value;
+
+        hash = computeHashCode();
     }
 
 
@@ -493,28 +506,25 @@
 
 
     /**
-     * Get the OID as a String
-     * 
-     * @return A String representing the OID
+     * Compute the hashcode for this object. No need to copute
+     * it live when calling the hashCode() method, as an OID
+     * never change.
      */
-    public String toString()
+    private int computeHashCode()
     {
-
-        StringBuffer sb = new StringBuffer();
-
-        if ( oidValues != null )
-        {
-            sb.append( oidValues[0] );
-
-            for ( int i = 1; i < oidValues.length; i++ )
-            {
-                sb.append( '.' ).append( oidValues[i] );
-            }
-        }
-
-        return sb.toString();
+    	int h = 37;
+    	
+    	for ( long val:oidValues )
+    	{
+    		int low = (int)(val & 0x0000FFFFL);
+    		int high = (int)(val >> 32);
+    		h = h*17 + high;
+    		h = h*17 + low;
+    	}
+    	
+    	return h;
     }
-
+    
     /**
      * Check that an OID is valid
      * @param oid The oid to be checked
@@ -632,4 +642,65 @@
 
         return !dotSeen;
     }
-}
+    
+    /**
+     * Get the OID as a String
+     * 
+     * @return A String representing the OID
+     */
+    public String toString()
+    {
+
+        StringBuffer sb = new StringBuffer();
+
+        if ( oidValues != null )
+        {
+            sb.append( oidValues[0] );
+
+            for ( int i = 1; i < oidValues.length; i++ )
+            {
+                sb.append( '.' ).append( oidValues[i] );
+            }
+        }
+
+        return sb.toString();
+    }
+
+    /**
+     * @see Object#hashCode()
+     */
+    public int hashCode()
+    {
+    	return hash;
+    }
+    
+    public boolean equals( Object oid )
+    {
+        if ( this == oid )
+        {
+            return true;
+        }
+        
+        if ( oid == null )
+        {
+            return false;
+        }
+        
+        if ( oid.getClass() != this.getClass() )
+        {
+            return false;
+        }
+        
+        OID instance = (OID)oid;
+   	
+    	if ( instance.hash != hash )
+    	{
+    		return false;
+    	}
+    	else
+    	{
+    		return Arrays.equals( instance.oidValues, oidValues );
+    	}
+    }
+    
+}
\ No newline at end of file



Mime
View raw message