Author: akarasulu Date: Sun Jan 30 08:17:05 2005 New Revision: 149121 URL: http://svn.apache.org/viewcvs?view=rev&rev=149121 Log: added some more utilities and change log Added: incubator/directory/ldap/trunk/CHANGES.txt incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/util/ArrayEnumeration.java incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/util/PreferencesDictionary.java incubator/directory/ldap/trunk/common/src/test/org/apache/ldap/common/util/ArrayEnumerationTest.java Modified: incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/util/ArrayUtils.java
Added: incubator/directory/ldap/trunk/CHANGES.txt Url: http://svn.apache.org/viewcvs/incubator/directory/ldap/trunk/CHANGES.txt?view=auto&rev=149121 ============================================================================== --- (empty file) +++ incubator/directory/ldap/trunk/CHANGES.txt Sun Jan 30 08:17:05 2005 @@ -0,0 +1,7 @@ +Changes since 0.8 +----------------- + + o added utilities to commons: + o ArrayEnumeration - wraps array as Enumeration + o PreferencesDictionary - wraps Preferences as Dictionary + Added: incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/util/ArrayEnumeration.java Url: http://svn.apache.org/viewcvs/incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/util/ArrayEnumeration.java?view=auto&rev=149121 ============================================================================== --- (empty file) +++ incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/util/ArrayEnumeration.java Sun Jan 30 08:17:05 2005 @@ -0,0 +1,65 @@ +/* + * 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.util; + + +import java.util.Enumeration; +import java.util.NoSuchElementException; + + +/** + * An enumeration wrapper around an array. + * + * @author <a href="mailto:[EMAIL PROTECTED]">Apache Directory Project</a> + * @version $Rev$ + */ +public class ArrayEnumeration implements Enumeration +{ + /** the index into the array */ + private int index = 0; + + /** Underlying array that is wrapped */ + private final Object[] array; + + + /** + * Constructs an enumeration by wrapping an array. + * + * @param array the underlying array that is wrapped + */ + public ArrayEnumeration( Object[] array ) + { + this.array = array; + } + + + public final boolean hasMoreElements() + { + return array != null && array.length != 0 && index < array.length; + } + + + public Object nextElement() + { + if ( ! hasMoreElements() ) + { + throw new NoSuchElementException( "no more objects in array" ); + } + + return array[index++]; + } +} Modified: incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/util/ArrayUtils.java Url: http://svn.apache.org/viewcvs/incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/util/ArrayUtils.java?view=diff&rev=149121&p1=incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/util/ArrayUtils.java&r1=149120&p2=incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/util/ArrayUtils.java&r2=149121 ============================================================================== --- incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/util/ArrayUtils.java (original) +++ incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/util/ArrayUtils.java Sun Jan 30 08:17:05 2005 @@ -29,6 +29,7 @@ * array input. However, an Object array that contains a <code>null</code> * element may throw an exception. Each method documents its behaviour.</p> * + * @author Alex Karasulu * @author Stephen Colebourne * @author Moritz Petersen * @author <a href="mailto:[EMAIL PROTECTED]">Fredrik Westermarck</a> @@ -40,7 +41,6 @@ * @author <a href="mailto:[EMAIL PROTECTED]">Ashwin S</a> * @author Maarten Coene * @since 2.0 - * @version $Id: ArrayUtils.java,v 1.46 2004/08/15 02:12:51 bayard Exp $ */ public class ArrayUtils { Added: incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/util/PreferencesDictionary.java Url: http://svn.apache.org/viewcvs/incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/util/PreferencesDictionary.java?view=auto&rev=149121 ============================================================================== --- (empty file) +++ incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/util/PreferencesDictionary.java Sun Jan 30 08:17:05 2005 @@ -0,0 +1,188 @@ +/* + * 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.util; + + +import java.util.Dictionary; +import java.util.Enumeration; +import java.util.prefs.Preferences; +import java.util.prefs.BackingStoreException; + + +/** + * A wrapper around Preferences to access it as a Dictionary. + * + * @author <a href="mailto:[EMAIL PROTECTED]">Apache Directory Project</a> + * @version $Rev$ + */ +public class PreferencesDictionary extends Dictionary +{ + /** the underlying wrapped preferences object */ + private final Preferences prefs; + + + // ------------------------------------------------------------------------ + // C O N S T R U C T O R + // ------------------------------------------------------------------------ + + + public PreferencesDictionary( Preferences prefs ) + { + this.prefs = prefs; + } + + + // ------------------------------------------------------------------------ + // E X T R A M E T H O D S + // ------------------------------------------------------------------------ + + + /** + * Gets the Preferences used as the backing store for this Dictionary. + * + * @return the underlying Preferences object + */ + public Preferences getPreferences() + { + return prefs; + } + + + // ------------------------------------------------------------------------ + // D I C T I O N A R Y M E T H O D S + // ------------------------------------------------------------------------ + + + public int size() + { + try + { + return prefs.keys().length; + } + catch ( BackingStoreException e ) + { + throw new NestableRuntimeException( "can't get keys from prefs", e ); + } + } + + + public boolean isEmpty() + { + try + { + return prefs.keys().length == 0; + } + catch ( BackingStoreException e ) + { + throw new NestableRuntimeException( "can't get keys from prefs", e ); + } + } + + + public Enumeration elements() + { + try + { + return new ArrayEnumeration( prefs.keys() ) + { + public Object nextElement() + { + String key = ( String ) super.nextElement(); + + return prefs.get( key, null ); + } + }; + } + catch ( BackingStoreException e ) + { + throw new NestableRuntimeException( "can't get keys from prefs", e ); + } + } + + + public Enumeration keys() + { + try + { + return new ArrayEnumeration( prefs.keys() ); + } + catch ( BackingStoreException e ) + { + throw new NestableRuntimeException( "can't get keys from prefs", e ); + } + } + + + public Object get( Object key ) + { + if ( key instanceof String ) + { + return prefs.get( ( String ) key, null ); + } + + return prefs.get( key.toString(), null ); + } + + + public Object remove( Object key ) + { + Object retval = get( key ); + + if ( key instanceof String ) + { + prefs.remove( ( String ) key ); + } + else + { + prefs.remove( key.toString() ); + } + + return retval; + } + + + public Object put( Object key, Object value ) + { + Object retval = get( key ); + + String skey = null; + + String svalue = null; + + if ( key instanceof String ) + { + skey = ( String ) key; + } + else + { + skey = key.toString(); + } + + if ( value instanceof String ) + { + svalue = ( String ) value; + } + else + { + svalue = value.toString(); + } + + prefs.put( skey, svalue ); + + return retval; + } +} Added: incubator/directory/ldap/trunk/common/src/test/org/apache/ldap/common/util/ArrayEnumerationTest.java Url: http://svn.apache.org/viewcvs/incubator/directory/ldap/trunk/common/src/test/org/apache/ldap/common/util/ArrayEnumerationTest.java?view=auto&rev=149121 ============================================================================== --- (empty file) +++ incubator/directory/ldap/trunk/common/src/test/org/apache/ldap/common/util/ArrayEnumerationTest.java Sun Jan 30 08:17:05 2005 @@ -0,0 +1,158 @@ +/* + * 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.util; + + +import java.util.NoSuchElementException; + +import junit.framework.TestCase; + + +/** + * Tests the ArrayEnumeration class. + * + * @author <a href="mailto:[EMAIL PROTECTED]">Apache Directory Project</a> + * @version $Rev$ + */ +public class ArrayEnumerationTest extends TestCase +{ + public void testAll() + { + // test with null array + + Object[] array = null; + + ArrayEnumeration list = new ArrayEnumeration( array ); + + assertFalse( list.hasMoreElements() ); + + try + { + list.nextElement(); + + fail( "should never get here due to a NoSuchElementException" ); + } + catch( NoSuchElementException e ) + { + } + + + + // test with empty array + + array = new Object[0]; + + list = new ArrayEnumeration( array ); + + assertFalse( list.hasMoreElements() ); + + assertFalse( list.hasMoreElements() ); + + try + { + list.nextElement(); + + fail( "should never get here due to a NoSuchElementException" ); + } + catch( NoSuchElementException e ) + { + } + + + // test with one object + + array = new Object[] { new Object() }; + + list = new ArrayEnumeration( array ); + + assertTrue( list.hasMoreElements() ); + + assertNotNull( list.nextElement() ); + + assertFalse( list.hasMoreElements() ); + + try + { + list.nextElement(); + + fail( "should never get here due to a NoSuchElementException" ); + } + catch( NoSuchElementException e ) + { + } + + + + // test with two objects + + array = new Object[] { new Object(), new Object() }; + + list = new ArrayEnumeration( array ); + + assertTrue( list.hasMoreElements() ); + + assertNotNull( list.nextElement() ); + + assertTrue( list.hasMoreElements() ); + + assertNotNull( list.nextElement() ); + + assertFalse( list.hasMoreElements() ); + + try + { + list.nextElement(); + + fail( "should never get here due to a NoSuchElementException" ); + } + catch( NoSuchElementException e ) + { + } + + + + // test with three elements + + array = new Object[] { new Object(), new Object(), new Object() }; + + list = new ArrayEnumeration( array ); + + assertTrue( list.hasMoreElements() ); + + assertNotNull( list.nextElement() ); + + assertTrue( list.hasMoreElements() ); + + assertNotNull( list.nextElement() ); + + assertTrue( list.hasMoreElements() ); + + assertNotNull( list.nextElement() ); + + assertFalse( list.hasMoreElements() ); + + try + { + list.nextElement(); + + fail( "should never get here due to a NoSuchElementException" ); + } + catch( NoSuchElementException e ) + { + } + } +}
