You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by ak...@apache.org on 2004/10/29 03:38:38 UTC

svn commit: rev 55925 - in incubator/directory/ldap/trunk/common/src: java/org/apache/ldap/common/message java/org/apache/ldap/common/util test/org/apache/ldap/common/message

Author: akarasulu
Date: Thu Oct 28 18:38:38 2004
New Revision: 55925

Added:
   incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/message/ArrayNamingEnumeration.java
   incubator/directory/ldap/trunk/common/src/test/org/apache/ldap/common/message/ArrayNamingEnumerationTest.java
Modified:
   incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/message/LockableAttributesImpl.java
   incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/util/DateUtils.java
Log:
Changes ...

 o fixed bug with a ConcurrentModificationException by using an array of elements
   in a naming enumeration
 o added new array based enumeration
 o added test case for array based enumeration
 o added extra DateUtils method to get time now so I don't have to use 
   System.currentTimeMillis all the time



Added: incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/message/ArrayNamingEnumeration.java
==============================================================================
--- (empty file)
+++ incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/message/ArrayNamingEnumeration.java	Thu Oct 28 18:38:38 2004
@@ -0,0 +1,93 @@
+/*
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.ldap.common.message;
+
+
+import java.util.NoSuchElementException;
+import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
+
+
+/**
+ * A NamingEnumeration over an array of objects.
+ *
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ * @version $Rev$
+ */
+public class ArrayNamingEnumeration implements NamingEnumeration
+{
+    /** the objects to enumerate */
+    private final Object[] objects;
+    /** the index pointing into the array */
+    private int index = 0;
+
+
+    /**
+     * Creates a NamingEnumeration over an array of objects.
+     *
+     * @param objects the objects to enumerate over
+     */
+    public ArrayNamingEnumeration( Object[] objects )
+    {
+        this.objects = objects;
+    }
+
+
+    public void close()
+    {
+        if ( objects != null )
+        {
+            index = objects.length;
+        }
+    }
+
+
+    public boolean hasMore()
+    {
+        if ( objects == null || objects.length == 0 )
+        {
+            return false;
+        }
+
+        return index < objects.length;
+    }
+
+
+    public Object next()
+    {
+        if ( objects == null || objects.length == 0 || index >= objects.length )
+        {
+            throw new NoSuchElementException();
+        }
+
+        Object retval = objects[index];
+        index++;
+        return retval;
+    }
+
+
+    public boolean hasMoreElements()
+    {
+        return hasMore();
+    }
+
+
+    public Object nextElement()
+    {
+        return next();
+    }
+}

Modified: incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/message/LockableAttributesImpl.java
==============================================================================
--- incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/message/LockableAttributesImpl.java	(original)
+++ incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/message/LockableAttributesImpl.java	Thu Oct 28 18:38:38 2004
@@ -20,6 +20,7 @@
 import java.util.Map;
 import java.util.HashMap;
 import java.util.Iterator;
+import java.util.Collections;
 
 import javax.naming.NamingException;
 import javax.naming.NamingEnumeration;
@@ -27,6 +28,7 @@
 import javax.naming.directory.Attributes;
 
 import org.apache.ldap.common.util.ExceptionUtils;
+import org.apache.ldap.common.util.ArrayUtils;
 
 import org.apache.ldap.common.Lockable;
 import org.apache.ldap.common.AbstractLockable;
@@ -177,7 +179,7 @@
      */
     public NamingEnumeration getIDs()
     {
-        return new IteratorNamingEnumeration( map.keySet().iterator() );
+        return new ArrayNamingEnumeration( map.keySet().toArray() );
     }
 
 

Modified: incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/util/DateUtils.java
==============================================================================
--- incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/util/DateUtils.java	(original)
+++ incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/util/DateUtils.java	Thu Oct 28 18:38:38 2004
@@ -187,4 +187,16 @@
 
         return buf.toString();
     }
+
+
+    /**
+     * Gets the generalized time right now.
+     *
+     * @see DateUtils#getGeneralizedTime(long)
+     * @return the generalizedTime right now
+     */ 
+    public static String getGeneralizedTime()
+    {
+        return getGeneralizedTime( System.currentTimeMillis() );
+    }
 }

Added: incubator/directory/ldap/trunk/common/src/test/org/apache/ldap/common/message/ArrayNamingEnumerationTest.java
==============================================================================
--- (empty file)
+++ incubator/directory/ldap/trunk/common/src/test/org/apache/ldap/common/message/ArrayNamingEnumerationTest.java	Thu Oct 28 18:38:38 2004
@@ -0,0 +1,120 @@
+/*
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.ldap.common.message;
+
+
+import java.util.NoSuchElementException;
+
+import junit.framework.TestCase;
+import org.apache.ldap.common.util.ArrayUtils;
+
+
+/**
+ * Tests the {@link ArrayNamingEnumeration} class.
+ *
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ * @version $Rev$
+ */
+public class ArrayNamingEnumerationTest extends TestCase
+{
+    /**
+     * Tests ArrayNamingEnumeration using an null array.
+     */
+    public void testUsingNullArray()
+    {
+        ArrayNamingEnumeration enum = new ArrayNamingEnumeration( null );
+        assertFalse( enum.hasMore() );
+
+        try
+        {
+            enum.next();
+            fail( "should blow exception before getting here" );
+        }
+        catch( NoSuchElementException e )
+        {
+            assertNotNull( e );
+        }
+    }
+
+
+    /**
+     * Tests ArrayNamingEnumeration using an array with length = 0.
+     */
+    public void testUsingEmptyArray()
+    {
+        ArrayNamingEnumeration enum = new ArrayNamingEnumeration( ArrayUtils.EMPTY_STRING_ARRAY );
+        assertFalse( enum.hasMore() );
+
+        try
+        {
+            enum.next();
+            fail( "should blow exception before getting here" );
+        }
+        catch( NoSuchElementException e )
+        {
+            assertNotNull( e );
+        }
+    }
+
+
+    /**
+     * Tests ArrayNamingEnumeration using an array with length = 1.
+     */
+    public void testUsingSingleElementArray()
+    {
+        ArrayNamingEnumeration enum ;
+        enum = new ArrayNamingEnumeration( new String[] { "foo" });
+        assertTrue( enum.hasMore() );
+        assertEquals( "foo", enum.next() );
+
+        try
+        {
+            enum.next();
+            fail( "should blow exception before getting here" );
+        }
+        catch( NoSuchElementException e )
+        {
+            assertNotNull( e );
+        }
+    }
+
+
+
+
+    /**
+     * Tests ArrayNamingEnumeration using an array with length = 2.
+     */
+    public void testUsingTwoElementArray()
+    {
+        ArrayNamingEnumeration enum ;
+        enum = new ArrayNamingEnumeration( new String[] { "foo", "bar" });
+        assertTrue( enum.hasMore() );
+        assertEquals( "foo", enum.next() );
+        assertTrue( enum.hasMore() );
+        assertEquals( "bar", enum.next() );
+
+        try
+        {
+            enum.next();
+            fail( "should blow exception before getting here" );
+        }
+        catch( NoSuchElementException e )
+        {
+            assertNotNull( e );
+        }
+    }
+}