directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r1083990 - /directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/name/Rdn.java
Date Mon, 21 Mar 2011 22:13:04 GMT
Author: elecharny
Date: Mon Mar 21 22:13:03 2011
New Revision: 1083990

URL: http://svn.apache.org/viewvc?rev=1083990&view=rev
Log:
Defined a pre-computed hashcode stored in the instance

Modified:
    directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/name/Rdn.java

Modified: directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/name/Rdn.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/name/Rdn.java?rev=1083990&r1=1083989&r2=1083990&view=diff
==============================================================================
--- directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/name/Rdn.java
(original)
+++ directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/name/Rdn.java
Mon Mar 21 22:13:03 2011
@@ -188,6 +188,9 @@ public final class Rdn implements Clonea
 
     /** the schema manager */
     private SchemaManager schemaManager;
+    
+    /** The computed hashcode */
+    private volatile int h;
 
 
     /**
@@ -214,6 +217,7 @@ public final class Rdn implements Clonea
         upName = "";
         normName = "";
         normalized = false;
+        h = 0;
     }
 
 
@@ -253,6 +257,8 @@ public final class Rdn implements Clonea
             normName = "";
             normalized = false;
         }
+
+        hashCode();
     }
 
 
@@ -300,6 +306,8 @@ public final class Rdn implements Clonea
             // As strange as it seems, the Rdn is *not* normalized against the schema at
this point
             normalized = false;
         }
+
+        hashCode();
     }
 
 
@@ -334,10 +342,14 @@ public final class Rdn implements Clonea
         switch ( rdn.size() )
         {
             case 0:
+                hashCode();
+
                 return;
 
             case 1:
                 this.ava = (Ava) rdn.ava.clone();
+                hashCode();
+
                 return;
 
             default:
@@ -351,6 +363,8 @@ public final class Rdn implements Clonea
                     avaTypes.put( currentAva.getNormType(), currentAva );
                 }
 
+                hashCode();
+
                 return;
         }
     }
@@ -411,6 +425,8 @@ public final class Rdn implements Clonea
                 normName = sb.toString();
                 break;
         }
+
+        hashCode();
     }
 
 
@@ -435,6 +451,7 @@ public final class Rdn implements Clonea
         this.upName = savedUpName;
         normalized = true;
         this.schemaManager = schemaManager;
+        hashCode();
 
         return this;
     }
@@ -480,6 +497,8 @@ public final class Rdn implements Clonea
                 ava = new Ava( schemaManager, upType, normalizedType, upValue, normalizedValue
);
                 nbAvas = 1;
                 avaType = normalizedType;
+                hashCode();
+
                 return;
 
             case 1:
@@ -503,9 +522,10 @@ public final class Rdn implements Clonea
                 Ava newAva = new Ava( schemaManager, upType, normalizedType, upValue, normalizedValue
);
                 avas.add( newAva );
                 avaTypes.put( normalizedType, newAva );
-
                 nbAvas++;
-                break;
+                hashCode();
+
+                return;
 
         }
     }
@@ -531,6 +551,8 @@ public final class Rdn implements Clonea
                 ava = value;
                 nbAvas = 1;
                 avaType = normalizedType;
+                hashCode();
+
                 return;
 
             case 1:
@@ -553,8 +575,9 @@ public final class Rdn implements Clonea
                 // add a new Ava
                 avas.add( value );
                 avaTypes.put( normalizedType, value );
-
                 nbAvas++;
+                hashCode();
+
                 break;
         }
     }
@@ -576,6 +599,7 @@ public final class Rdn implements Clonea
         normName = "";
         upName = "";
         normalized = false;
+        h = 0;
     }
 
 
@@ -1324,31 +1348,34 @@ public final class Rdn implements Clonea
       */
     public int hashCode()
     {
-        int result = 37;
-
-        switch ( nbAvas )
+        if ( h == 0 )
         {
-            case 0:
-                // An empty Rdn
-                break;
-
-            case 1:
-                // We have a single Ava
-                result = result * 17 + ava.hashCode();
-                break;
-
-            default:
-                // We have more than one Ava
-
-                for ( Ava ata : avas )
-                {
-                    result = result * 17 + ata.hashCode();
-                }
-
-                break;
+            h = 37;
+    
+            switch ( nbAvas )
+            {
+                case 0:
+                    // An empty Rdn
+                    break;
+    
+                case 1:
+                    // We have a single Ava
+                    h = h * 17 + ava.hashCode();
+                    break;
+    
+                default:
+                    // We have more than one Ava
+    
+                    for ( Ava ata : avas )
+                    {
+                        h = h * 17 + ata.hashCode();
+                    }
+    
+                    break;
+            }
         }
 
-        return result;
+        return h;
     }
 
 
@@ -1424,6 +1451,8 @@ public final class Rdn implements Clonea
                 break;
         }
         
+        out.writeInt( h );
+        
         out.flush();
     }
 
@@ -1485,6 +1514,8 @@ public final class Rdn implements Clonea
 
                 break;
         }
+        
+        h = in.readInt();
     }
     
     



Mime
View raw message