You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by el...@apache.org on 2007/11/22 00:09:40 UTC

svn commit: r597245 - /directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/message/AttributeImpl.java

Author: elecharny
Date: Wed Nov 21 15:09:40 2007
New Revision: 597245

URL: http://svn.apache.org/viewvc?rev=597245&view=rev
Log:
Fixed a bug in the way we handled the removal of byte[] values in an attribute.

Modified:
    directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/message/AttributeImpl.java

Modified: directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/message/AttributeImpl.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/message/AttributeImpl.java?rev=597245&r1=597244&r2=597245&view=diff
==============================================================================
--- directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/message/AttributeImpl.java (original)
+++ directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/message/AttributeImpl.java Wed Nov 21 15:09:40 2007
@@ -57,13 +57,14 @@
 
     /** In case we have only one value, just use this container */
     private Object value;
-    
+
     /** the list of attribute values, if unordered */
     private List<Object> list;
-    
+
     /** The number of values stored */
     private int size = 0;
 
+
     // ------------------------------------------------------------------------
     // Constructors
     // ------------------------------------------------------------------------
@@ -117,6 +118,7 @@
         size = 1;
     }
 
+
     /**
      * Create a copy of an Attribute, be it an AttributeImpl
      * instance of a BasicAttribute instance
@@ -132,9 +134,9 @@
         }
         else if ( attribute instanceof AttributeImpl )
         {
-            AttributeImpl clone = (AttributeImpl)attribute.clone();
-            
-            upId  = clone.upId;
+            AttributeImpl clone = ( AttributeImpl ) attribute.clone();
+
+            upId = clone.upId;
             list = clone.list;
             size = clone.size;
             value = clone.value;
@@ -142,9 +144,9 @@
         else if ( attribute instanceof BasicAttribute )
         {
             upId = attribute.getID();
-            
+
             NamingEnumeration<?> values = attribute.getAll();
-            
+
             while ( values.hasMoreElements() )
             {
                 add( values.nextElement() );
@@ -156,6 +158,7 @@
         }
     }
 
+
     // ------------------------------------------------------------------------
     // javax.naming.directory.Attribute Interface Method Implementations
     // ------------------------------------------------------------------------
@@ -171,26 +174,29 @@
         {
             return new IteratorNamingEnumeration<Object>( new Iterator<Object>()
             {
-                private boolean more = (size != 0);
-                    
-                public boolean hasNext() 
+                private boolean more = ( size != 0 );
+
+
+                public boolean hasNext()
                 {
                     return more;
                 }
-                
-                public Object next() 
+
+
+                public Object next()
                 {
                     more = false;
                     return value;
                 }
-                
-                public void remove() 
+
+
+                public void remove()
                 {
                     value = null;
                     more = true;
                     size = 0;
                 }
-            });
+            } );
         }
         else
         {
@@ -248,17 +254,17 @@
      */
     public boolean contains( Object attrVal )
     {
-        switch (size)
+        switch ( size )
         {
-            case 0 :
+            case 0:
                 return false;
-                
-            case 1 :
+
+            case 1:
                 return AttributeUtils.equals( value, attrVal );
-                
-            default :
+
+            default:
                 Iterator<Object> values = list.iterator();
-            
+
                 while ( values.hasNext() )
                 {
                     if ( AttributeUtils.equals( values.next(), attrVal ) )
@@ -266,7 +272,7 @@
                         return true;
                     }
                 }
-                
+
                 return false;
         }
     }
@@ -284,24 +290,24 @@
     public boolean add( Object attrVal )
     {
         boolean exists = false;
-        
+
         if ( contains( attrVal ) )
         {
             // Do not duplicate values
             return true;
         }
-        
+
         // First copy the value
         attrVal = AttributeUtils.cloneValue( attrVal );
-        
+
         switch ( size )
         {
-            case 0 :
+            case 0:
                 value = attrVal;
                 size++;
                 return true;
-                
-            case 1 :
+
+            case 1:
                 exists = value.equals( attrVal );
 
                 if ( exists )
@@ -318,10 +324,10 @@
                     value = null;
                     return true;
                 }
-                
-            default :
-                exists = list.contains( attrVal ); 
-            
+
+            default:
+                exists = list.contains( attrVal );
+
                 list.add( attrVal );
                 size++;
                 return exists;
@@ -341,45 +347,116 @@
     {
         switch ( size )
         {
-            case 0 :
+            case 0:
                 return false;
-                
-            case 1 :
-                if ( value.equals(  attrVal ) )
+
+            case 1:
+                if ( attrVal instanceof byte[] )
                 {
-                    value = null;
-                    size--;
-                    return true;
+                    if ( value instanceof byte[] )
+                    {
+                        byte[] valByte = ( byte[] ) value;
+
+                        if ( Arrays.equals( valByte, ( byte[] ) attrVal ) )
+                        {
+                            value = null;
+                            size--;
+                            return true;
+                        }
+                        else
+                        {
+                            return false;
+                        }
+                    }
+                    else
+                    {
+                        return false;
+                    }
                 }
                 else
                 {
-                    return false;
+                    if ( value.equals( attrVal ) )
+                    {
+                        value = null;
+                        size--;
+                        return true;
+                    }
+                    else
+                    {
+                        return false;
+                    }
                 }
-                
-            case 2 : 
-                if ( list.contains( attrVal ) )
-                {
-                    list.remove( attrVal );
-                    value = list.get(0);
-                    size = 1;
-                    list = null;
-                    return true;
+
+            case 2:
+                if ( attrVal instanceof byte[] )
+                {
+                    for ( Object val : list )
+                    {
+                        if ( val instanceof byte[] )
+                        {
+                            byte[] valByte = ( byte[] ) val;
+
+                            if ( Arrays.equals( valByte, ( byte[] ) attrVal ) )
+                            {
+                                list.remove( valByte );
+                                value = list.get( 0 );
+                                size = 1;
+                                list = null;
+                                return true;
+                            }
+                        }
+                    }
+
+                    return false;
                 }
                 else
                 {
-                    return false;
+                    if ( list.contains( attrVal ) )
+                    {
+                        list.remove( attrVal );
+                        value = list.get( 0 );
+                        size = 1;
+                        list = null;
+                        return true;
+                    }
+                    else
+                    {
+                        return false;
+                    }
                 }
-                
-            default :
-                if ( list.contains( attrVal ) )
+
+            default:
+                if ( attrVal instanceof byte[] )
                 {
-                    list.remove( attrVal );
-                    size--;
-                    return true;
+                    for ( Object val : list )
+                    {
+                        if ( val instanceof byte[] )
+                        {
+                            byte[] valByte = ( byte[] ) val;
+
+                            if ( Arrays.equals( valByte, ( byte[] ) attrVal ) )
+                            {
+                                list.remove( attrVal );
+                                size--;
+                                return true;
+                            }
+                        }
+                    }
+
+                    return false;
                 }
                 else
                 {
-                    return false;
+                    if ( list.contains( attrVal ) )
+                    {
+                        list.remove( attrVal );
+                        size--;
+                        return true;
+                    }
+                    else
+                    {
+                        return false;
+                    }
                 }
         }
     }
@@ -392,15 +469,15 @@
     {
         switch ( size )
         {
-            case 0 :
+            case 0:
                 return;
-                
-            case 1 :
+
+            case 1:
                 value = null;
                 size = 0;
                 return;
-                
-            default :
+
+            default:
                 list = null;
                 size = 0;
                 return;
@@ -440,8 +517,8 @@
     {
         try
         {
-            AttributeImpl clone = (AttributeImpl)super.clone();
-            
+            AttributeImpl clone = ( AttributeImpl ) super.clone();
+
             if ( size < 2 )
             {
                 clone.value = AttributeUtils.cloneValue( value );
@@ -449,14 +526,14 @@
             else
             {
                 clone.list = new ArrayList<Object>( size );
-                
+
                 for ( int i = 0; i < size; i++ )
                 {
                     Object newValue = AttributeUtils.cloneValue( list.get( i ) );
                     clone.list.add( newValue );
                 }
             }
-            
+
             return clone;
         }
         catch ( CloneNotSupportedException cnse )
@@ -487,20 +564,20 @@
      */
     public Object get( int index )
     {
-        if ( (index < 0 ) || ( index > size + 1 ) )
+        if ( ( index < 0 ) || ( index > size + 1 ) )
         {
             return null;
         }
-        
+
         switch ( size )
         {
-            case 0 :
+            case 0:
                 return null;
-                
-            case 1 :
+
+            case 1:
                 return value;
-                
-            default :
+
+            default:
                 return list.get( index );
         }
     }
@@ -516,33 +593,32 @@
      */
     public Object remove( int index )
     {
-        if ( (index < 0 ) || ( index > size + 1 ) )
+        if ( ( index < 0 ) || ( index > size + 1 ) )
         {
             return null;
         }
-        
+
         switch ( size )
         {
-            case 0 :
+            case 0:
                 return null;
-                
-            case 1 :
+
+            case 1:
                 Object result = value;
                 value = null;
                 size = 0;
                 return result;
-                
-            case 2 :
+
+            case 2:
                 Object removed = list.remove( index );
-                value = list.get(0);
+                value = list.get( 0 );
                 size = 1;
                 list = null;
                 return removed;
-                
-                
-            default :
+
+            default:
                 size--;
-            
+
                 return list.remove( index );
         }
     }
@@ -561,17 +637,17 @@
     {
         // First copy the value
         attrVal = AttributeUtils.cloneValue( attrVal );
-        
+
         switch ( size )
         {
-            case 0 :
+            case 0:
                 size++;
                 value = attrVal;
                 return;
-                
-            case 1 :
+
+            case 1:
                 list = new ArrayList<Object>();
-                
+
                 if ( index == 0 )
                 {
                     list.add( attrVal );
@@ -586,8 +662,8 @@
                 size++;
                 value = null;
                 return;
-                
-            default :
+
+            default:
                 list.add( index, attrVal );
                 size++;
                 return;
@@ -608,15 +684,15 @@
     {
         // First copy the value
         attrVal = AttributeUtils.cloneValue( attrVal );
-        
+
         switch ( size )
         {
-            case 0 :
+            case 0:
                 size++;
                 value = attrVal;
                 return null;
-                
-            case 1 :
+
+            case 1:
                 if ( index == 0 )
                 {
                     Object result = value;
@@ -632,8 +708,8 @@
                     value = null;
                     return null;
                 }
-                
-            default :
+
+            default:
                 Object oldValue = list.get( index );
                 list.set( index, attrVal );
                 return oldValue;
@@ -668,7 +744,7 @@
         }
 
         Attribute attr = ( Attribute ) obj;
-        
+
         if ( !upId.equals( attr.getID() ) )
         {
             return false;
@@ -708,40 +784,40 @@
             // We have to do that because attribute's values are
             // not ordered.
             Map<Integer, Object> hash = new HashMap<Integer, Object>();
-            
-            for ( Object v:list )
+
+            for ( Object v : list )
             {
                 int h = 0;
-                
+
                 if ( v instanceof String )
                 {
                     h = v.hashCode();
                 }
                 else if ( v instanceof byte[] )
                 {
-                    byte[] bv = (byte[])v;
+                    byte[] bv = ( byte[] ) v;
                     h = Arrays.hashCode( bv );
                 }
                 else
                 {
                     return false;
                 }
-                
+
                 hash.put( Integer.valueOf( h ), v );
             }
-            
+
             try
             {
                 NamingEnumeration<?> attrValues = attr.getAll();
-                
+
                 while ( attrValues.hasMoreElements() )
                 {
                     Object val = attrValues.next();
-                    
+
                     if ( val instanceof String )
                     {
                         Integer h = Integer.valueOf( val.hashCode() );
-                        
+
                         if ( !hash.containsKey( h ) )
                         {
                             return false;
@@ -749,7 +825,7 @@
                         else
                         {
                             Object val2 = hash.remove( h );
-                            
+
                             if ( !val.equals( val2 ) )
                             {
                                 return false;
@@ -758,7 +834,7 @@
                     }
                     else if ( val instanceof byte[] )
                     {
-                        Integer h = Integer.valueOf( Arrays.hashCode( (byte[])val ) );
+                        Integer h = Integer.valueOf( Arrays.hashCode( ( byte[] ) val ) );
 
                         if ( !hash.containsKey( h ) )
                         {
@@ -767,8 +843,8 @@
                         else
                         {
                             Object val2 = hash.remove( h );
-                            
-                            if ( !Arrays.equals( (byte[])val, (byte[])val2 ) )
+
+                            if ( !Arrays.equals( ( byte[] ) val, ( byte[] ) val2 ) )
                             {
                                 return false;
                             }
@@ -779,7 +855,7 @@
                         return false;
                     }
                 }
-                
+
                 if ( hash.size() != 0 )
                 {
                     return false;
@@ -796,45 +872,46 @@
             }
         }
     }
-    
+
+
     /**
      * @see Object#toString()
      */
     public String toString()
     {
         StringBuffer sb = new StringBuffer();
-        
+
         sb.append( "Attribute id : '" ).append( upId ).append( "', " );
-        sb.append( " Values : [");
-        
-        switch (size)
+        sb.append( " Values : [" );
+
+        switch ( size )
         {
-            case 0 :
+            case 0:
                 sb.append( "]\n" );
                 break;
-                
-            case 1 :
-                if ( value instanceof String ) 
+
+            case 1:
+                if ( value instanceof String )
                 {
                     sb.append( '\'' ).append( value ).append( '\'' );
                 }
                 else
                 {
-                    sb.append( StringTools.dumpBytes( (byte[])value ) );
+                    sb.append( StringTools.dumpBytes( ( byte[] ) value ) );
                 }
-                
+
                 sb.append( "]\n" );
                 break;
-                
-            default :
+
+            default:
                 boolean isFirst = true;
-            
+
                 Iterator<Object> values = list.iterator();
-                
+
                 while ( values.hasNext() )
                 {
                     Object v = values.next();
-                    
+
                     if ( isFirst == false )
                     {
                         sb.append( ", " );
@@ -843,21 +920,21 @@
                     {
                         isFirst = false;
                     }
-                    
-                    if ( v instanceof String ) 
+
+                    if ( v instanceof String )
                     {
                         sb.append( '\'' ).append( v ).append( '\'' );
                     }
                     else
                     {
-                        sb.append( StringTools.dumpBytes( (byte[])v ) );
+                        sb.append( StringTools.dumpBytes( ( byte[] ) v ) );
                     }
                 }
-                
+
                 sb.append( "]\n" );
                 break;
         }
-        
+
         return sb.toString();
     }
 }