directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r1520710 - in /directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/name: Dn.java Rdn.java
Date Fri, 06 Sep 2013 22:41:15 GMT
Author: elecharny
Date: Fri Sep  6 22:41:15 2013
New Revision: 1520710

URL: http://svn.apache.org/r1520710
Log:
o Removed many calls to clone(), as the class is immutable
o Modified the rdnOidToName() method to update the RDN on place, instead of copying it before
updating it
o Added a package protected replaceAva() method in the RDN class

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

Modified: directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/name/Dn.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/name/Dn.java?rev=1520710&r1=1520709&r2=1520710&view=diff
==============================================================================
--- directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/name/Dn.java
(original)
+++ directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/name/Dn.java
Fri Sep  6 22:41:15 2013
@@ -323,7 +323,7 @@ public class Dn implements Iterable<Rdn>
 
         for ( Rdn rdn : rdns )
         {
-            this.rdns.add( rdn.clone() );
+            this.rdns.add( rdn );
         }
 
         apply( null );
@@ -373,7 +373,7 @@ public class Dn implements Iterable<Rdn>
 
         for ( Rdn rdn : rdns )
         {
-            this.rdns.add( rdn.clone() );
+            this.rdns.add( rdn );
         }
 
         apply( schemaManager );
@@ -674,7 +674,7 @@ public class Dn implements Iterable<Rdn>
 
         Rdn rdn = rdns.get( posn );
 
-        return rdn.clone();
+        return rdn;
     }
 
 
@@ -690,7 +690,7 @@ public class Dn implements Iterable<Rdn>
             return Rdn.EMPTY_RDN;
         }
 
-        return rdns.get( 0 ).clone();
+        return rdns.get( 0 );
     }
 
 
@@ -777,8 +777,7 @@ public class Dn implements Iterable<Rdn>
 
         for ( int i = 0; i < rdns.size() - length; i++ )
         {
-            // Don't forget to clone the rdns !
-            newDn.rdns.add( rdns.get( i ).clone() );
+            newDn.rdns.add( rdns.get( i ) );
         }
 
         newDn.toUpName();
@@ -859,8 +858,7 @@ public class Dn implements Iterable<Rdn>
 
         for ( int i = length; i < rdns.size(); i++ )
         {
-            // Don't forget to clone the rdns !
-            newDn.rdns.add( rdns.get( i ).clone() );
+            newDn.rdns.add( rdns.get( i ) );
         }
 
         newDn.toUpName();
@@ -944,7 +942,7 @@ public class Dn implements Iterable<Rdn>
 
         Dn clonedDn = copy();
 
-        clonedDn.rdns.add( 0, newRdn.clone() );
+        clonedDn.rdns.add( 0, newRdn );
         clonedDn.apply( schemaManager, true );
         clonedDn.toUpName();
 
@@ -972,8 +970,7 @@ public class Dn implements Iterable<Rdn>
 
         for ( int i = rdns.size() - posn; i < rdns.size(); i++ )
         {
-            // Don't forget to clone the rdns !
-            newDn.rdns.add( rdns.get( i ).clone() );
+            newDn.rdns.add( rdns.get( i ) );
         }
 
         try
@@ -1001,7 +998,7 @@ public class Dn implements Iterable<Rdn>
 
         for ( Rdn rdn : rdns )
         {
-            dn.rdns.add( rdn.clone() );
+            dn.rdns.add( rdn );
         }
 
         return dn;
@@ -1135,13 +1132,13 @@ public class Dn implements Iterable<Rdn>
     {
         // We have more than one ATAV for this Rdn. We will loop on all
         // ATAVs
-        Rdn rdnCopy = rdn.clone();
-        rdn.clear();
+        //Rdn rdnCopy = rdn.clone();
+        //rdn.clear();
 
-        if ( rdnCopy.size() < 2 )
+        if ( rdn.size() < 2 )
         {
-            Ava newAtav = atavOidToName( rdnCopy.getAva(), schemaManager );
-            rdn.addAVA( schemaManager, newAtav );
+            Ava newAtav = atavOidToName( rdn.getAva(), schemaManager );
+            rdn.replaceAva( newAtav, 0 );
         }
         else
         {
@@ -1149,7 +1146,7 @@ public class Dn implements Iterable<Rdn>
             Map<String, Ava> avas = new HashMap<String, Ava>();
 
             // Sort the OIDs
-            for ( Ava val : rdnCopy )
+            for ( Ava val : rdn )
             {
                 Ava newAtav = atavOidToName( val, schemaManager );
                 String oid = newAtav.getAttributeType().getOid();
@@ -1158,9 +1155,11 @@ public class Dn implements Iterable<Rdn>
             }
 
             // And create the Rdn
+            int pos = 0;
+
             for ( String oid : sortedOids )
             {
-                rdn.addAVA( schemaManager, avas.get( oid ) );
+                rdn.replaceAva( avas.get( oid ), pos++ );
             }
         }
     }

Modified: directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/name/Rdn.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/name/Rdn.java?rev=1520710&r1=1520709&r2=1520710&view=diff
==============================================================================
--- directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/name/Rdn.java
(original)
+++ directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/name/Rdn.java
Fri Sep  6 22:41:15 2013
@@ -253,7 +253,7 @@ public class Rdn implements Cloneable, E
             {
                 throw new LdapInvalidDnException( "Invalid RDN" );
             }
-            
+
             upName = rdn;
         }
         else
@@ -437,7 +437,8 @@ public class Rdn implements Cloneable, E
 
     /**
      * Transform a Rdn by changing the value to its OID counterpart and
-     * normalizing the value accordingly to its type.
+     * normalizing the value accordingly to its type. The modified Rdn is
+     * a new instance, as the Rdn class is immutable.
      *
      * @param schemaManager the SchemaManager
      * @return this Rdn, normalized
@@ -537,6 +538,48 @@ public class Rdn implements Cloneable, E
 
 
     /**
+     * Replace an Ava into a Rdn at a given position
+     *
+     * @param value The modified Ava
+     * @param pos The position of the Ava in the Rdn
+     * @exception LdapInvalidDnException If the position is not valid
+     */
+    // WARNING : The protection level is left unspecified intentionally.
+    // We need this method to be visible from the DnParser class, but not
+    // from outside this package.
+    /* Unspecified protection */void replaceAva( Ava value, int pos ) throws LdapInvalidDnException
+    {
+        if ( ( pos < 0 ) || ( pos > nbAvas ) )
+        {
+            throw new LdapInvalidDnException( "Cannot set the AVA at position " + pos );
+        }
+
+        String normalizedType = value.getNormType();
+
+        switch ( nbAvas )
+        {
+            case 1:
+                // This is the first Ava. Just stores it.
+                ava = value;
+                avaType = normalizedType;
+
+                break;
+
+            default:
+                Ava oldAva = avas.get( pos );
+                avas.set( pos, value );
+                avaTypes.remove( oldAva.getType() );
+                avaTypes.put( normalizedType, value );
+
+                break;
+        }
+
+        h = 0;
+        hashCode();
+    }
+
+
+    /**
      * Add an Ava to the current schema aware Rdn
      *
      * @param value The added Ava
@@ -566,9 +609,10 @@ public class Rdn implements Cloneable, E
                 // Check that the first AVA is not for the same attribute
                 if ( avaType.equals( normalizedType ) )
                 {
-                    throw new LdapInvalidDnException( "Invalid RDN: the " + normalizedType
+ " is already present in the RDN" );
+                    throw new LdapInvalidDnException( "Invalid RDN: the " + normalizedType
+                        + " is already present in the RDN" );
                 }
-                
+
                 // First, create the HashMap,
                 avas = new ArrayList<Ava>();
 
@@ -586,9 +630,10 @@ public class Rdn implements Cloneable, E
                 // Check that the AT is not already present
                 if ( avaTypes.containsKey( normalizedType ) )
                 {
-                    throw new LdapInvalidDnException( "Invalid RDN: the " + normalizedType
+ " is already present in the RDN" );
+                    throw new LdapInvalidDnException( "Invalid RDN: the " + normalizedType
+                        + " is already present in the RDN" );
                 }
-                
+
                 // add a new Ava
                 avas.add( value );
                 avaTypes.put( normalizedType, value );
@@ -864,7 +909,7 @@ public class Rdn implements Cloneable, E
                 return ava;
 
             default:
-                return avas.get( 0 ).clone();
+                return avas.get( 0 );
         }
     }
 
@@ -976,7 +1021,7 @@ public class Rdn implements Cloneable, E
         }
 
         Rdn rdn = ( Rdn ) that;
-        
+
         // Short cut : compare the normalized Rdn
         if ( normName.equals( rdn.normName ) )
         {
@@ -1077,7 +1122,7 @@ public class Rdn implements Cloneable, E
         }
 
         char[] chars = value.toCharArray();
-        
+
         // If the value is contained into double quotes, return it as is.
         if ( ( chars[0] == '\"' ) && ( chars[chars.length - 1] == '\"' ) )
         {
@@ -1249,8 +1294,8 @@ public class Rdn implements Cloneable, E
             if ( ( bytes[i] & 0x0080 ) != 0 )
             {
                 newBytes[pos++] = '\\';
-                newBytes[pos++] = (byte)Strings.dumpHex( (byte)( (bytes[i] & 0x00F0 )
 >> 4 ) );
-                newBytes[pos++] = (byte)Strings.dumpHex( (byte)(bytes[i] & 0x000F) );
+                newBytes[pos++] = ( byte ) Strings.dumpHex( ( byte ) ( ( bytes[i] & 0x00F0
) >> 4 ) );
+                newBytes[pos++] = ( byte ) Strings.dumpHex( ( byte ) ( bytes[i] & 0x000F
) );
             }
             else
             {
@@ -1266,9 +1311,9 @@ public class Rdn implements Cloneable, E
                             newBytes[pos++] = '\\';
                             newBytes[pos++] = bytes[i];
                         }
-    
+
                         break;
-    
+
                     case '#':
                         if ( i != 0 )
                         {
@@ -1279,9 +1324,9 @@ public class Rdn implements Cloneable, E
                             newBytes[pos++] = '\\';
                             newBytes[pos++] = bytes[i];
                         }
-    
+
                         break;
-    
+
                     case '"':
                     case '+':
                     case ',':
@@ -1293,13 +1338,13 @@ public class Rdn implements Cloneable, E
                         newBytes[pos++] = '\\';
                         newBytes[pos++] = bytes[i];
                         break;
-    
+
                     case 0x7F:
                         newBytes[pos++] = '\\';
                         newBytes[pos++] = '7';
                         newBytes[pos++] = 'F';
                         break;
-    
+
                     case 0x00:
                     case 0x01:
                     case 0x02:
@@ -1318,9 +1363,9 @@ public class Rdn implements Cloneable, E
                     case 0x0F:
                         newBytes[pos++] = '\\';
                         newBytes[pos++] = '0';
-                        newBytes[pos++] = (byte)Strings.dumpHex( ( byte ) ( bytes[i] &
0x0F ) );
+                        newBytes[pos++] = ( byte ) Strings.dumpHex( ( byte ) ( bytes[i] &
0x0F ) );
                         break;
-    
+
                     case 0x10:
                     case 0x11:
                     case 0x12:
@@ -1339,9 +1384,9 @@ public class Rdn implements Cloneable, E
                     case 0x1F:
                         newBytes[pos++] = '\\';
                         newBytes[pos++] = '1';
-                        newBytes[pos++] = (byte)Strings.dumpHex( ( byte ) ( bytes[i] &
0x0F ) );
+                        newBytes[pos++] = ( byte ) Strings.dumpHex( ( byte ) ( bytes[i] &
0x0F ) );
                         break;
-    
+
                     default:
                         newBytes[pos++] = bytes[i];
                         break;



Mime
View raw message