directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r411342 - in /directory/branches/shared/optimization/ldap/src: main/java/org/apache/directory/shared/ldap/name/ test/java/org/apache/directory/shared/ldap/name/
Date Fri, 02 Jun 2006 23:57:58 GMT
Author: elecharny
Date: Fri Jun  2 16:57:57 2006
New Revision: 411342

URL: http://svn.apache.org/viewvc?rev=411342&view=rev
Log:
- Added a hashCode() method in the three classes
- Modified the hashCode() testCase

The hashcode now depend on all the RDN's atavs, instead of the String 
of the DN

Modified:
    directory/branches/shared/optimization/ldap/src/main/java/org/apache/directory/shared/ldap/name/AttributeTypeAndValue.java
    directory/branches/shared/optimization/ldap/src/main/java/org/apache/directory/shared/ldap/name/LdapDN.java
    directory/branches/shared/optimization/ldap/src/main/java/org/apache/directory/shared/ldap/name/Rdn.java
    directory/branches/shared/optimization/ldap/src/test/java/org/apache/directory/shared/ldap/name/LdapDNTest.java

Modified: directory/branches/shared/optimization/ldap/src/main/java/org/apache/directory/shared/ldap/name/AttributeTypeAndValue.java
URL: http://svn.apache.org/viewvc/directory/branches/shared/optimization/ldap/src/main/java/org/apache/directory/shared/ldap/name/AttributeTypeAndValue.java?rev=411342&r1=411341&r2=411342&view=diff
==============================================================================
--- directory/branches/shared/optimization/ldap/src/main/java/org/apache/directory/shared/ldap/name/AttributeTypeAndValue.java
(original)
+++ directory/branches/shared/optimization/ldap/src/main/java/org/apache/directory/shared/ldap/name/AttributeTypeAndValue.java
Fri Jun  2 16:57:57 2006
@@ -343,6 +343,20 @@
         return StringTools.lowerCase( StringTools.trim( type ) ) + '=' + StringTools.trim(
value );
     }
 
+    /**
+     * Gets the hashcode of this object.
+     * 
+     * @see java.lang.Object#hashCode()
+     */
+    public int hashCode()
+    {
+        int result = 17;
+        
+        result = result * 37 + ( type != null ? type.hashCode() : 0 ); 
+        result = result * 37 + ( value != null ? type.hashCode() : 0 ); 
+            
+        return result;
+    }
 
     /**
      * A String representation of a AttributeTypeAndValue.

Modified: directory/branches/shared/optimization/ldap/src/main/java/org/apache/directory/shared/ldap/name/LdapDN.java
URL: http://svn.apache.org/viewvc/directory/branches/shared/optimization/ldap/src/main/java/org/apache/directory/shared/ldap/name/LdapDN.java?rev=411342&r1=411341&r2=411342&view=diff
==============================================================================
--- directory/branches/shared/optimization/ldap/src/main/java/org/apache/directory/shared/ldap/name/LdapDN.java
(original)
+++ directory/branches/shared/optimization/ldap/src/main/java/org/apache/directory/shared/ldap/name/LdapDN.java
Fri Jun  2 16:57:57 2006
@@ -40,11 +40,18 @@
 
 
 /**
- * The LdapDN class contains a DN (Distinguished Name). Its specification can be
- * found in RFC 2253, "UTF-8 String Representation of Distinguished Names". We
- * will store two representation of a DN : - a user Provider represeentation,
- * which is the parsed String given by a user - an internal representation. A DN
- * is formed of RDNs, in a specific order : RDN[n], RDN[n-1], ... RDN[1], RDN[0]
+ * The LdapDN class contains a DN (Distinguished Name).
+ * 
+ * Its specification can be found in RFC 2253, 
+ * "UTF-8 String Representation of Distinguished Names". 
+ * 
+ * We will store two representation of a DN : 
+ * - a user Provider represeentation, which is the parsed String given by a user 
+ * - an internal representation. 
+ * 
+ * A DN is formed of RDNs, in a specific order : 
+ *  RDN[n], RDN[n-1], ... RDN[1], RDN[0]
+ *  
  * It represents a tree, in which the root is the last RDN (RDN[0]) and the leaf
  * is the first RDN (RDN[n]).
  * 
@@ -307,10 +314,17 @@
 
     /**
      * Return the User Provided prefix representation of the DN starting at the
-     * posn position. If posn = 0, return an empty string. for DN : sn=smith,
-     * dc=apache, dc=org getUpname(0) -> "" getUpName(1) -> "dc=org"
-     * getUpname(3) -> "sn=smith, dc=apache, dc=org" getUpName(4) ->
-     * ArrayOutOfBoundException Warning ! The returned String is not exactly the
+     * posn position. 
+     * 
+     * If posn = 0, return an empty string. 
+     * 
+     * for DN : sn=smith, dc=apache, dc=org 
+     * getUpname(0) -> "" 
+     * getUpName(1) -> "dc=org"
+     * getUpname(3) -> "sn=smith, dc=apache, dc=org" 
+     * getUpName(4) -> ArrayOutOfBoundException 
+     * 
+     * Warning ! The returned String is not exactly the
      * user provided DN, as spaces before and after each RDNs have been trimmed.
      * 
      * @param posn
@@ -355,14 +369,19 @@
 
     /**
      * Return the User Provided suffix representation of the DN starting at the
-     * posn position. If posn = 0, return an empty string. for DN : sn=smith,
-     * dc=apache, dc=org getUpname(0) -> "sn=smith, dc=apache, dc=org"
-     * getUpName(1) -> "sn=smith, dc=apache" getUpname(3) -> "sn=smith"
-     * getUpName(4) -> "" Warning ! The returned String is not exactly the user
+     * posn position. 
+     * If posn = 0, return an empty string. 
+     * 
+     * for DN : sn=smith, dc=apache, dc=org 
+     * getUpname(0) -> "sn=smith, dc=apache, dc=org"
+     * getUpName(1) -> "sn=smith, dc=apache" 
+     * getUpname(3) -> "sn=smith"
+     * getUpName(4) -> "" 
+     * 
+     * Warning ! The returned String is not exactly the user
      * provided DN, as spaces before and after each RDNs have been trimmed.
      * 
-     * @param posn
-     *            The starting position
+     * @param posn The starting position
      * @return The truncated DN
      */
     private String getUpNameSuffix( int posn )
@@ -395,13 +414,23 @@
 
 
     /**
-     * Gets the hashcode of the string representation of this name.
+     * Gets the hashcode of this name.
      * 
      * @see java.lang.Object#hashCode()
      */
     public int hashCode()
     {
-        return upName.hashCode();
+    	int result = 17;
+    	
+    	if ( ( rdns != null ) || ( rdns.size() == 0 ) )
+    	{
+    		for ( Iterator rdnsIter = rdns.iterator(); rdnsIter.hasNext(); ) 
+    		{
+    			result = result * 37 + rdnsIter.next().hashCode(); 
+    		}
+    	}
+    	
+        return result;
     }
 
 
@@ -462,8 +491,11 @@
     /**
      * Determines whether this name starts with a specified prefix. A name
      * <tt>name</tt> is a prefix if it is equal to
-     * <tt>getPrefix(name.size())</tt>. Be aware that for a specific DN like
:
-     * cn=xxx, ou=yyy the startsWith method will retourn true with ou=yyy, and
+     * <tt>getPrefix(name.size())</tt>. 
+     * 
+     * Be aware that for a specific DN like :
+     * cn=xxx, ou=yyy 
+     * the startsWith method will return true with ou=yyy, and
      * false with cn=xxx
      * 
      * @param name
@@ -514,8 +546,10 @@
     /**
      * Determines whether this name ends with a specified suffix. A name
      * <tt>name</tt> is a suffix if it is equal to
-     * <tt>getSuffix(size()-name.size())</tt>. Be aware that for a specific
-     * DN like : cn=xxx, ou=yyy the endsWith method will retourn true with
+     * <tt>getSuffix(size()-name.size())</tt>. 
+     * 
+     * Be aware that for a specific
+     * DN like : cn=xxx, ou=yyy the endsWith method will return true with
      * cn=xxx, and false with ou=yyy
      * 
      * @param name
@@ -1244,7 +1278,6 @@
         return normalize( dn, DnOidContainer.getOids() );
     }
 
-
     /**
      * Substitute OIDs and aliases for the simplest alias
      * 
@@ -1267,13 +1300,4 @@
 
         return type;
     }
-
-    /*
-     * public Name toLdapName() throws InvalidNameException { Name name = new
-     * LdapName(); Enumeration rdns = getAll(); while ( rdns.hasMoreElements() ) {
-     * name.add( (String)rdns.nextElement() ); } return name; } public LdapDN(
-     * LdapName name ) throws InvalidNameException { Name newName = new
-     * LdapDN(); Enumeration comps = name.getAll(); while (
-     * comps.hasMoreElements() ) { newName.add( (String)comps.nextElement() ); } }
-     */
 }

Modified: directory/branches/shared/optimization/ldap/src/main/java/org/apache/directory/shared/ldap/name/Rdn.java
URL: http://svn.apache.org/viewvc/directory/branches/shared/optimization/ldap/src/main/java/org/apache/directory/shared/ldap/name/Rdn.java?rev=411342&r1=411341&r2=411342&view=diff
==============================================================================
--- directory/branches/shared/optimization/ldap/src/main/java/org/apache/directory/shared/ldap/name/Rdn.java
(original)
+++ directory/branches/shared/optimization/ldap/src/main/java/org/apache/directory/shared/ldap/name/Rdn.java
Fri Jun  2 16:57:57 2006
@@ -1158,4 +1158,37 @@
 
         return new String( newChars, 0, pos );
     }
+
+    /**
+     * Gets the hashcode of this rdn.
+     * 
+     * @see java.lang.Object#hashCode()
+     */
+    public int hashCode()
+    {
+    	int result = 17;
+    	
+        switch ( nbAtavs )
+        {
+            case 0:
+                // An empty RDN
+                break;
+
+            case 1:
+                // We have a single AttributeTypeAndValue
+                result = result * 37 + atav.hashCode();
+                break;
+
+            default:
+                // We have more than one AttributeTypeAndValue
+
+                for ( Iterator elems = atavs.iterator();elems.hasNext(); )
+                {
+                    AttributeTypeAndValue ata = ( AttributeTypeAndValue ) elems.next();
+                    result = result * 37 + ata.hashCode();
+                }
+        }
+    	
+    	return result;
+    }
 }

Modified: directory/branches/shared/optimization/ldap/src/test/java/org/apache/directory/shared/ldap/name/LdapDNTest.java
URL: http://svn.apache.org/viewvc/directory/branches/shared/optimization/ldap/src/test/java/org/apache/directory/shared/ldap/name/LdapDNTest.java?rev=411342&r1=411341&r2=411342&view=diff
==============================================================================
--- directory/branches/shared/optimization/ldap/src/test/java/org/apache/directory/shared/ldap/name/LdapDNTest.java
(original)
+++ directory/branches/shared/optimization/ldap/src/test/java/org/apache/directory/shared/ldap/name/LdapDNTest.java
Fri Jun  2 16:57:57 2006
@@ -1188,18 +1188,6 @@
 
 
     /**
-     * @throws Exception
-     *             if anything goes wrong.
-     */
-    public void testHashCode() throws Exception
-    {
-        String strName = "cn=HomeDir,cn=John,ou=Marketing,ou=East";
-        Name name = new LdapDN( strName );
-        assertEquals( name.hashCode(), strName.hashCode() );
-    }
-
-
-    /**
      * Class to test for void LdapName(String)
      * 
      * @throws Exception
@@ -2212,5 +2200,19 @@
                 throw ioe;
             }
         }
+    }
+
+    /**
+     * Class to test for hashCode()
+     */
+    public void testLdapNameHashCode() throws Exception
+    {
+        Name name1 = new LdapDN(
+            "2.5.4.11= Some   People   + domainComponent=  And   Some anImAls,DomainComponent
= eXample,0.9.2342.19200300.100.1.25= cOm" );
+
+        Name name2 = new LdapDN(
+            "2.5.4.11=some people+domainComponent=and some animals,DomainComponent=example,0.9.2342.19200300.100.1.25=com"
);
+
+        assertEquals( name1.hashCode(), name2.hashCode() );
     }
 }



Mime
View raw message