directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From akaras...@apache.org
Subject svn commit: r151131 [9/9] - in incubator/directory/asn1/branches/rewrite/ber: ./ src/java/org/apache/asn1/ber/ src/java/org/apache/asn1/ber/digester/ src/java/org/apache/asn1/ber/digester/rules/ src/java/org/apache/asn1/ber/primitives/ src/java/org/apache/asn1/tuples/ src/java/org/apache/asn1/util/ src/test/org/apache/asn1/ber/ src/test/org/apache/asn1/ber/primitives/
Date Thu, 03 Feb 2005 07:18:55 GMT
Added: incubator/directory/asn1/branches/rewrite/ber/src/java/org/apache/asn1/util/Validate.java
URL: http://svn.apache.org/viewcvs/incubator/directory/asn1/branches/rewrite/ber/src/java/org/apache/asn1/util/Validate.java?view=auto&rev=151131
==============================================================================
--- incubator/directory/asn1/branches/rewrite/ber/src/java/org/apache/asn1/util/Validate.java
(added)
+++ incubator/directory/asn1/branches/rewrite/ber/src/java/org/apache/asn1/util/Validate.java
Wed Feb  2 23:18:42 2005
@@ -0,0 +1,645 @@
+/*
+ * Copyright 2002-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.asn1.util;
+
+
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.Map;
+
+
+/**
+ * <p>Assists in validating arguments.</p>
+ * <p/>
+ * <p>The class is based along the lines of JUnit. If an argument value is
+ * deemed invalid, an IllegalArgumentException is thrown. For example:</p>
+ * <p/>
+ * <pre>
+ * Validate.isTrue( i > 0, "The value must be greater than zero: ", i);
+ * Validate.notNull( surname, "The surname must not be null");
+ * </pre>
+ *
+ * @author <a href="mailto:ola.berg@arkitema.se">Ola Berg</a>
+ * @author Stephen Colebourne
+ * @author Gary Gregory
+ * @author Norm Deane
+ * @version $Id: Validate.java,v 1.13 2004/10/08 21:44:41 scolebourne Exp $
+ * @since 2.0
+ */
+public class Validate
+{
+    // Validate has no dependencies on other classes in Commons Lang at present
+    
+    /**
+     * Constructor. This class should not normally be instantiated.
+     */
+    public Validate()
+    {
+    }
+    
+    // isTrue
+    //---------------------------------------------------------------------------------
+
+    /**
+     * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
+     * if the test result is <code>false</code>.</p>
+     * <p/>
+     * <p>This is used when validating according to an arbitrary boolean
+     * expression, such as validating a primitive number or using your own
+     * custom validation expression.</p>
+     * <p/>
+     * <pre>
+     * Validate.isTrue( myObject.isOk(), "The object is not OK: ", myObject);
+     * </pre>
+     * <p/>
+     * <p>For performance reasons, the object is passed as a separate parameter
+     * and appended to the message string only in the case of an error.</p>
+     *
+     * @param expression a boolean expression
+     * @param message    the exception message you would like to see if the
+     *                   expression is <code>false</code>
+     * @param value      the value to append to the message in case of error
+     * @throws IllegalArgumentException if expression is <code>false</code>
+     */
+    public static void isTrue( boolean expression, String message, Object value )
+    {
+        if ( expression == false )
+        {
+            throw new IllegalArgumentException( message + value );
+        }
+    }
+
+
+    /**
+     * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
+     * if the test result is <code>false</code>.</p>
+     * <p/>
+     * <p>This is used when validating according to an arbitrary boolean
+     * expression, such as validating a primitive number or using your own
+     * custom validation expression.</p>
+     * <p/>
+     * <pre>
+     * Validate.isTrue( i > 0, "The value must be greater than zero: ", i);
+     * </pre>
+     * <p/>
+     * <p>For performance reasons, the long value is passed as a separate
+     * parameter and appended to the message string only in the case of an
+     * error.</p>
+     *
+     * @param expression a boolean expression
+     * @param message    the exception message you would like to see if the
+     *                   expression is <code>false</code>
+     * @param value      the value to append to the message in case of error
+     * @throws IllegalArgumentException if expression is <code>false</code>
+     */
+    public static void isTrue( boolean expression, String message, long value )
+    {
+        if ( expression == false )
+        {
+            throw new IllegalArgumentException( message + value );
+        }
+    }
+
+
+    /**
+     * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
+     * if the test result is <code>false</code>.</p>
+     * <p/>
+     * <p>This is used when validating according to an arbitrary boolean
+     * expression, such as validating a primitive number or using your own
+     * custom validation expression.</p>
+     * <p/>
+     * <pre>
+     * Validate.isTrue( d > 0.0, "The value must be greater than zero: ", d);
+     * </pre>
+     * <p/>
+     * <p>For performance reasons, the double value is passed as a separate
+     * parameter and appended to the message string only in the case of an
+     * error.</p>
+     *
+     * @param expression a boolean expression
+     * @param message    the exception message you would like to see if the
+     *                   expression is <code>false</code>
+     * @param value      the value to append to the message in case of error
+     * @throws IllegalArgumentException if expression is <code>false</code>
+     */
+    public static void isTrue( boolean expression, String message, double value )
+    {
+        if ( expression == false )
+        {
+            throw new IllegalArgumentException( message + value );
+        }
+    }
+
+
+    /**
+     * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
+     * if the test result is <code>false</code>.</p>
+     * <p/>
+     * <p>This is used when validating according to an arbitrary boolean
+     * expression, such as validating a primitive number or using your own
+     * custom validation expression.</p>
+     * <p/>
+     * <pre>
+     * Validate.isTrue( (i > 0), "The value must be greater than zero");
+     * Validate.isTrue( myObject.isOk(), "The object is not OK");
+     * </pre>
+     * <p/>
+     * <p>For performance reasons, the message string should not involve a
+     * string append, instead use the {@link #isTrue(boolean, String, Object)}
+     * method.</p>
+     *
+     * @param expression a boolean expression
+     * @param message    the exception message you would like to see if the
+     *                   expression is <code>false</code>
+     * @throws IllegalArgumentException if expression is <code>false</code>
+     */
+    public static void isTrue( boolean expression, String message )
+    {
+        if ( expression == false )
+        {
+            throw new IllegalArgumentException( message );
+        }
+    }
+
+
+    /**
+     * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
+     * if the test result is <code>false</code>.</p>
+     * <p/>
+     * <p>This is used when validating according to an arbitrary boolean
+     * expression, such as validating a primitive number or using your own
+     * custom validation expression.</p>
+     * <p/>
+     * <pre>
+     * Validate.isTrue( i > 0 );
+     * Validate.isTrue( myObject.isOk() );
+     * </pre>
+     * <p/>
+     * <p>The message in the exception is 'The validated expression is
+     * false'.</p>
+     *
+     * @param expression a boolean expression
+     * @throws IllegalArgumentException if expression is <code>false</code>
+     */
+    public static void isTrue( boolean expression )
+    {
+        if ( expression == false )
+        {
+            throw new IllegalArgumentException( "The validated expression is false" );
+        }
+    }
+
+    // notNull
+    //---------------------------------------------------------------------------------
+
+    /**
+     * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
+     * if the argument is <code>null</code>.</p>
+     * <p/>
+     * <pre>
+     * Validate.notNull(myObject, "The object must not be null");
+     * </pre>
+     *
+     * @param object  the object to check is not <code>null</code>
+     * @param message the exception message you would like to see if the object
+     *                is <code>null</code>
+     * @throws IllegalArgumentException if the object is <code>null</code>
+     */
+    public static void notNull( Object object, String message )
+    {
+        if ( object == null )
+        {
+            throw new IllegalArgumentException( message );
+        }
+    }
+
+
+    /**
+     * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
+     * if the argument is <code>null</code>.</p>
+     * <p/>
+     * <pre>
+     * Validate.notNull(myObject);
+     * </pre>
+     * <p/>
+     * <p>The message in the exception is 'The validated object is null'.</p>
+     *
+     * @param object the object to check is not <code>null</code>
+     * @throws IllegalArgumentException if the object is <code>null</code>
+     */
+    public static void notNull( Object object )
+    {
+        if ( object == null )
+        {
+            throw new IllegalArgumentException( "The validated object is null" );
+        }
+    }
+
+    // notEmpty array
+    //---------------------------------------------------------------------------------
+
+    /**
+     * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
+     * if the argument array is empty (<code>null</code> or no elements).</p>
+     * <p/>
+     * <pre>
+     * Validate.notEmpty(myArray, "The array must not be empty");
+     * </pre>
+     *
+     * @param array   the array to check is not empty
+     * @param message the exception message you would like to see if the array
+     *                is empty
+     * @throws IllegalArgumentException if the array is empty
+     */
+    public static void notEmpty( Object[] array, String message )
+    {
+        if ( array == null || array.length == 0 )
+        {
+            throw new IllegalArgumentException( message );
+        }
+    }
+
+
+    /**
+     * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
+     * if the argument array is empty (<code>null</code> or no elements).</p>
+     * <p/>
+     * <pre>
+     * Validate.notEmpty(myArray);
+     * </pre>
+     * <p/>
+     * <p>The message in the exception is 'The validated array is empty'.
+     *
+     * @param array the array to check is not empty
+     * @throws IllegalArgumentException if the array is empty
+     */
+    public static void notEmpty( Object[] array )
+    {
+        if ( array == null || array.length == 0 )
+        {
+            throw new IllegalArgumentException( "The validated array is empty" );
+        }
+    }
+
+    // notEmpty collection
+    //---------------------------------------------------------------------------------
+
+    /**
+     * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
+     * if the argument Collection is empty (<code>null</code> or no
+     * elements).</p>
+     * <p/>
+     * <pre>
+     * Validate.notEmpty(myCollection, "The collection must not be empty");
+     * </pre>
+     *
+     * @param collection the collection to check is not empty
+     * @param message    the exception message you would like to see if the
+     *                   collection is empty
+     * @throws IllegalArgumentException if the collection is empty
+     */
+    public static void notEmpty( Collection collection, String message )
+    {
+        if ( collection == null || collection.size() == 0 )
+        {
+            throw new IllegalArgumentException( message );
+        }
+    }
+
+
+    /**
+     * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
+     * if the argument Collection is empty (<code>null</code> or no
+     * elements).</p>
+     * <p/>
+     * <pre>
+     * Validate.notEmpty(myCollection);
+     * </pre>
+     * <p/>
+     * <p>The message in the exception is 'The validated collection is
+     * empty'.</p>
+     *
+     * @param collection the collection to check is not empty
+     * @throws IllegalArgumentException if the collection is empty
+     */
+    public static void notEmpty( Collection collection )
+    {
+        if ( collection == null || collection.size() == 0 )
+        {
+            throw new IllegalArgumentException( "The validated collection is empty" );
+        }
+    }
+
+    // notEmpty map
+    //---------------------------------------------------------------------------------
+
+    /**
+     * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
+     * if the argument Map is empty (<code>null</code> or no elements).</p>
+     * <p/>
+     * <pre>
+     * Validate.notEmpty(myMap, "The map must not be empty");
+     * </pre>
+     *
+     * @param map     the map to check is not empty
+     * @param message the exception message you would like to see if the map is
+     *                empty
+     * @throws IllegalArgumentException if the map is empty
+     */
+    public static void notEmpty( Map map, String message )
+    {
+        if ( map == null || map.size() == 0 )
+        {
+            throw new IllegalArgumentException( message );
+        }
+    }
+
+
+    /**
+     * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
+     * if the argument Map is empty (<code>null</code> or no elements).</p>
+     * <p/>
+     * <pre>
+     * Validate.notEmpty(myMap);
+     * </pre>
+     * <p/>
+     * <p>The message in the exception is 'The validated map is empty'.</p>
+     *
+     * @param map the map to check is not empty
+     * @throws IllegalArgumentException if the map is empty
+     */
+    public static void notEmpty( Map map )
+    {
+        if ( map == null || map.size() == 0 )
+        {
+            throw new IllegalArgumentException( "The validated map is empty" );
+        }
+    }
+
+    // notEmpty string
+    //---------------------------------------------------------------------------------
+
+    /**
+     * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
+     * if the argument String is empty (<code>null</code> or zero length).</p>
+     * <p/>
+     * <pre>
+     * Validate.notEmpty(myString, "The string must not be empty");
+     * </pre>
+     *
+     * @param string  the string to check is not empty
+     * @param message the exception message you would like to see if the string
+     *                is empty
+     * @throws IllegalArgumentException if the string is empty
+     */
+    public static void notEmpty( String string, String message )
+    {
+        if ( string == null || string.length() == 0 )
+        {
+            throw new IllegalArgumentException( message );
+        }
+    }
+
+
+    /**
+     * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
+     * if the argument String is empty (<code>null</code> or zero length).</p>
+     * <p/>
+     * <pre>
+     * Validate.notEmpty(myString);
+     * </pre>
+     * <p/>
+     * <p>The message in the exception is 'The validated string is empty'.</p>
+     *
+     * @param string the string to check is not empty
+     * @throws IllegalArgumentException if the string is empty
+     */
+    public static void notEmpty( String string )
+    {
+        if ( string == null || string.length() == 0 )
+        {
+            throw new IllegalArgumentException( "The validated string is empty" );
+        }
+    }
+
+    // notNullElements array
+    //---------------------------------------------------------------------------------
+
+    /**
+     * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
+     * if the argument array has <code>null</code> elements or is
+     * <code>null</code>.</p>
+     * <p/>
+     * <pre>
+     * Validate.noNullElements(myArray, "The array must not contain null
+     * elements");
+     * </pre>
+     * <p/>
+     * <p>If the array is null then the message in the exception is 'The
+     * validated object is null'.</p>
+     *
+     * @param array   the array to check
+     * @param message the exception message if the array has <code>null</code>
+     *                elements
+     * @throws IllegalArgumentException if the array has <code>null</code>
+     *                                  elements or is <code>null</code>
+     */
+    public static void noNullElements( Object[] array, String message )
+    {
+        Validate.notNull( array );
+
+        for ( int i = 0; i < array.length; i++ )
+        {
+            if ( array[i] == null )
+            {
+                throw new IllegalArgumentException( message );
+            }
+        }
+    }
+
+
+    /**
+     * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
+     * if the argument array has <code>null</code> elements or is
+     * <code>null</code>.</p>
+     * <p/>
+     * <pre>
+     * Validate.noNullElements(myArray);
+     * </pre>
+     * <p/>
+     * <p>If the array has a null element the message in the exception is 'The
+     * validated array contains null element at index: '.</p>
+     * <p/>
+     * <p>If the array is null then the message in the exception is 'The
+     * validated object is null'.</p>
+     *
+     * @param array the array to check
+     * @throws IllegalArgumentException if the array has <code>null</code>
+     *                                  elements or is <code>null</code>
+     */
+    public static void noNullElements( Object[] array )
+    {
+        Validate.notNull( array );
+
+        for ( int i = 0; i < array.length; i++ )
+        {
+            if ( array[i] == null )
+            {
+                throw new IllegalArgumentException( "The validated array contains null element
at index: " + i );
+            }
+        }
+    }
+
+    // notNullElements collection
+    //---------------------------------------------------------------------------------
+
+    /**
+     * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
+     * if the argument Collection has <code>null</code> elements or is
+     * <code>null</code>.</p>
+     * <p/>
+     * <pre>
+     * Validate.noNullElements(myCollection, "The collection must not contain
+     * null elements");
+     * </pre>
+     * <p/>
+     * <p>If the collection is null then the message in the exception is 'The
+     * validated object is null'.</p>
+     *
+     * @param collection the collection to check
+     * @param message    the exception message if the collection has
+     *                   <code>null</code> elements
+     * @throws IllegalArgumentException if the collection has <code>null</code>
+     *                                  elements or is <code>null</code>
+     */
+    public static void noNullElements( Collection collection, String message )
+    {
+        Validate.notNull( collection );
+
+        for ( Iterator it = collection.iterator(); it.hasNext(); )
+        {
+            if ( it.next() == null )
+            {
+                throw new IllegalArgumentException( message );
+            }
+        }
+    }
+
+
+    /**
+     * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
+     * if the argument Collection has <code>null</code> elements or is
+     * <code>null</code>.</p>
+     * <p/>
+     * <pre>
+     * Validate.noNullElements(myCollection);
+     * </pre>
+     * <p/>
+     * <p>The message in the exception is 'The validated collection contains
+     * null element at index: '.</p>
+     * <p/>
+     * <p>If the collection is null then the message in the exception is 'The
+     * validated object is null'.</p>
+     *
+     * @param collection the collection to check
+     * @throws IllegalArgumentException if the collection has <code>null</code>
+     *                                  elements or is <code>null</code>
+     */
+    public static void noNullElements( Collection collection )
+    {
+        Validate.notNull( collection );
+
+        int i = 0;
+
+        for ( Iterator it = collection.iterator(); it.hasNext(); i++ )
+        {
+            if ( it.next() == null )
+            {
+                throw new IllegalArgumentException( "The validated collection contains null
element at index: " + i );
+            }
+        }
+    }
+
+
+    /**
+     * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
+     * if the argument collection  is <code>null</code> or has elements that
are
+     * not of type <code>clazz</code> or a subclass.</p>
+     * <p/>
+     * <pre>
+     * Validate.allElementsOfType(collection, String.class, "Collection has
+     * invalid elements");
+     * </pre>
+     *
+     * @param collection the collection to check, not null
+     * @param clazz      the <code>Class</code> which the collection's elements
+     *                   are expected to be, not null
+     * @param message    the exception message if the <code>Collection</code>
+     *                   has elements not of type <code>clazz</code>
+     * @since 2.1
+     */
+    public static void allElementsOfType( Collection collection, Class clazz, String message
)
+    {
+        Validate.notNull( collection );
+
+        Validate.notNull( clazz );
+
+        for ( Iterator it = collection.iterator(); it.hasNext(); )
+        {
+            if ( clazz.isInstance( it.next() ) == false )
+            {
+                throw new IllegalArgumentException( message );
+            }
+        }
+    }
+
+
+    /**
+     * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
+     * if the argument collection  is <code>null</code> or has elements that
are
+     * not of type <code>clazz</code> or a subclass.</p>
+     * <p/>
+     * <pre>
+     * Validate.allElementsOfType(collection, String.class);
+     * </pre>
+     * <p/>
+     * <p>The message in the exception is 'The validated collection contains an
+     * element not of type clazz at index: '.</p>
+     *
+     * @param collection the collection to check, not null
+     * @param clazz      the <code>Class</code> which the collection's elements
+     *                   are expected to be, not null
+     * @since 2.1
+     */
+    public static void allElementsOfType( Collection collection, Class clazz )
+    {
+        Validate.notNull( collection );
+
+        Validate.notNull( clazz );
+
+        int i = 0;
+
+        for ( Iterator it = collection.iterator(); it.hasNext(); i++ )
+        {
+            if ( clazz.isInstance( it.next() ) == false )
+            {
+                throw new IllegalArgumentException( "The validated collection contains an
element not of type " + clazz.getName() + " at index: " + i );
+            }
+        }
+    }
+
+}

Added: incubator/directory/asn1/branches/rewrite/ber/src/java/org/apache/asn1/util/ValuedEnum.java
URL: http://svn.apache.org/viewcvs/incubator/directory/asn1/branches/rewrite/ber/src/java/org/apache/asn1/util/ValuedEnum.java?view=auto&rev=151131
==============================================================================
--- incubator/directory/asn1/branches/rewrite/ber/src/java/org/apache/asn1/util/ValuedEnum.java
(added)
+++ incubator/directory/asn1/branches/rewrite/ber/src/java/org/apache/asn1/util/ValuedEnum.java
Wed Feb  2 23:18:42 2005
@@ -0,0 +1,207 @@
+/*
+ * Copyright 2002-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.asn1.util;
+
+
+import java.util.Iterator;
+import java.util.List;
+
+
+/**
+ * <p>Abstract superclass for type-safe enums with integer values suitable for
+ * use in <code>switch</code> statements.</p>
+ * <p/>
+ * <p><em>NOTE:</em>Due to the way in which Java ClassLoaders work, comparing
+ * <code>Enum</code> objects should always be done using the equals() method,
+ * not <code>==</code>. The equals() method will try <code>==</code>
first so in
+ * most cases the effect is the same.</p>
+ * <p/>
+ * <p>To use this class, it must be subclassed. For example:</p>
+ * <p/>
+ * <pre>
+ * public final class JavaVersionEnum extends ValuedEnum {
+ *   //standard enums for version of JVM
+ *   public static final int  JAVA1_0_VALUE  = 100;
+ *   public static final int  JAVA1_1_VALUE  = 110;
+ *   public static final int  JAVA1_2_VALUE  = 120;
+ *   public static final int  JAVA1_3_VALUE  = 130;
+ *   public static final JavaVersionEnum  JAVA1_0  = new JavaVersionEnum( "Java
+ * 1.0", JAVA1_0_VALUE );
+ *   public static final JavaVersionEnum  JAVA1_1  = new JavaVersionEnum( "Java
+ * 1.1", JAVA1_1_VALUE );
+ *   public static final JavaVersionEnum  JAVA1_2  = new JavaVersionEnum( "Java
+ * 1.2", JAVA1_2_VALUE );
+ *   public static final JavaVersionEnum  JAVA1_3  = new JavaVersionEnum( "Java
+ * 1.3", JAVA1_3_VALUE );
+ * <p/>
+ *   private JavaVersionEnum(String name, int value) {
+ *     super( name, value );
+ *   }
+ * <p/>
+ *   public static JavaVersionEnum getEnum(String javaVersion) {
+ *     return (JavaVersionEnum) getEnum(JavaVersionEnum.class, javaVersion);
+ *   }
+ * <p/>
+ *   public static JavaVersionEnum getEnum(int javaVersion) {
+ *     return (JavaVersionEnum) getEnum(JavaVersionEnum.class, javaVersion);
+ *   }
+ * <p/>
+ *   public static Map getEnumMap() {
+ *     return getEnumMap(JavaVersionEnum.class);
+ *   }
+ * <p/>
+ *   public static List getEnumList() {
+ *     return getEnumList(JavaVersionEnum.class);
+ *   }
+ * <p/>
+ *   public static Iterator iterator() {
+ *     return iterator(JavaVersionEnum.class);
+ *   }
+ * }
+ * </pre>
+ * <p/>
+ * <p>The above class could then be used as follows:</p>
+ * <p/>
+ * <pre>
+ * public void doSomething(JavaVersion ver) {
+ *   switch (ver.getValue()) {
+ *     case JAVA1_0_VALUE:
+ *       // ...
+ *       break;
+ *     case JAVA1_1_VALUE:
+ *       // ...
+ *       break;
+ *     //...
+ *   }
+ * }
+ * </pre>
+ * <p/>
+ * <p>As shown, each enum has a name and a value. These can be accessed using
+ * <code>getName</code> and <code>getValue</code>.</p>
+ * <p/>
+ * <p>The <code>getEnum</code> and <code>iterator</code> methods
are
+ * recommended. Unfortunately, Java restrictions require these to be coded as
+ * shown in each subclass. An alternative choice is to use the {@link }
+ * class.</p>
+ *
+ * @author Apache Avalon project
+ * @author Stephen Colebourne
+ * @version $Id: ValuedEnum.java,v 1.16 2004/02/23 04:34:20 ggregory Exp $
+ * @since 1.0
+ */
+public abstract class ValuedEnum extends Enum
+{
+    /**
+     * The value contained in enum.
+     */
+    private final int iValue;
+
+
+    /**
+     * Constructor for enum item.
+     *
+     * @param name  the name of enum item
+     * @param value the value of enum item
+     */
+    protected ValuedEnum( String name, int value )
+    {
+        super( name );
+        iValue = value;
+    }
+
+
+    /**
+     * <p>Gets an <code>Enum</code> object by class and value.</p>
+     * <p/>
+     * <p>This method loops through the list of <code>Enum</code>, thus
if there
+     * are many <code>Enum</code>s this will be slow.</p>
+     *
+     * @param enumClass the class of the <code>Enum</code> to get
+     * @param value     the value of the <code>Enum</code> to get
+     * @return the enum object, or null if the enum does not exist
+     * @throws IllegalArgumentException if the enum class is <code>null</code>
+     */
+    protected static Enum getEnum( Class enumClass, int value )
+    {
+        if ( enumClass == null )
+        {
+            throw new IllegalArgumentException( "The Enum Class must not be null" );
+        }
+
+        List list = Enum.getEnumList( enumClass );
+
+        for ( Iterator it = list.iterator(); it.hasNext(); )
+        {
+            ValuedEnum enum = ( ValuedEnum ) it.next();
+
+            if ( enum.getValue() == value )
+            {
+                return enum;
+            }
+        }
+        return null;
+    }
+
+
+    /**
+     * <p>Get value of enum item.</p>
+     *
+     * @return the enum item's value.
+     */
+    public final int getValue()
+    {
+        return iValue;
+    }
+
+
+    /**
+     * <p>Tests for order.</p>
+     * <p/>
+     * <p>The default ordering is numeric by value, but this can be overridden
+     * by subclasses.</p>
+     *
+     * @param other the other object to compare to
+     * @return -ve if this is less than the other object, +ve if greater than,
+     *         <code>0</code> of equal
+     * @throws ClassCastException   if other is not an <code>Enum</code>
+     * @throws NullPointerException if other is <code>null</code>
+     * @see java.lang.Comparable#compareTo(Object)
+     */
+    public int compareTo( Object other )
+    {
+        return iValue - ( ( ValuedEnum ) other ).iValue;
+    }
+
+
+    /**
+     * <p>Human readable description of this <code>Enum</code> item.</p>
+     *
+     * @return String in the form <code>type[name=value]</code>, for example:
+     *         <code>JavaVersion[Java 1.0=100]</code>. Note that the package
+     *         name is stripped from the type name.
+     */
+    public String toString()
+    {
+        if ( iToString == null )
+        {
+            String shortName = getShortName( getEnumClass() );
+
+            iToString = shortName + "[" + getName() + "=" + getValue() + "]";
+        }
+
+        return iToString;
+    }
+}

Modified: incubator/directory/asn1/branches/rewrite/ber/src/test/org/apache/asn1/ber/AbstractDecoderTestCaseTest.java
URL: http://svn.apache.org/viewcvs/incubator/directory/asn1/branches/rewrite/ber/src/test/org/apache/asn1/ber/AbstractDecoderTestCaseTest.java?view=diff&r1=151130&r2=151131
==============================================================================
--- incubator/directory/asn1/branches/rewrite/ber/src/test/org/apache/asn1/ber/AbstractDecoderTestCaseTest.java
(original)
+++ incubator/directory/asn1/branches/rewrite/ber/src/test/org/apache/asn1/ber/AbstractDecoderTestCaseTest.java
Wed Feb  2 23:18:42 2005
@@ -19,8 +19,7 @@
 
 import java.nio.ByteBuffer;
 
-import org.apache.commons.lang.ArrayUtils ;
-import org.apache.asn1.ber.AbstractDecoderTestCase;
+import org.apache.asn1.util.ArrayUtils;
 
 
 /**

Modified: incubator/directory/asn1/branches/rewrite/ber/src/test/org/apache/asn1/ber/BERDecoderTest.java
URL: http://svn.apache.org/viewcvs/incubator/directory/asn1/branches/rewrite/ber/src/test/org/apache/asn1/ber/BERDecoderTest.java?view=diff&r1=151130&r2=151131
==============================================================================
--- incubator/directory/asn1/branches/rewrite/ber/src/test/org/apache/asn1/ber/BERDecoderTest.java
(original)
+++ incubator/directory/asn1/branches/rewrite/ber/src/test/org/apache/asn1/ber/BERDecoderTest.java
Wed Feb  2 23:18:42 2005
@@ -20,15 +20,12 @@
 import java.nio.ByteBuffer ;
 import java.util.ArrayList ;
 
-import org.apache.commons.lang.ArrayUtils ;
-import org.apache.commons.lang.RandomStringUtils ;
-
 import org.apache.asn1.codec.stateful.StatefulDecoder ;
 import org.apache.asn1.codec.stateful.DecoderCallback;
 import org.apache.asn1.codec.stateful.DecoderMonitorAdapter;
 import org.apache.asn1.codec.stateful.*;
-import org.apache.asn1.ber.AbstractDecoderTestCase;
-import org.apache.asn1.ber.BERDecoderMonitor;
+import org.apache.asn1.util.ArrayUtils;
+import org.apache.asn1.util.RandomStringUtils;
 
 
 /**

Modified: incubator/directory/asn1/branches/rewrite/ber/src/test/org/apache/asn1/ber/BEREncoderTest.java
URL: http://svn.apache.org/viewcvs/incubator/directory/asn1/branches/rewrite/ber/src/test/org/apache/asn1/ber/BEREncoderTest.java?view=diff&r1=151130&r2=151131
==============================================================================
--- incubator/directory/asn1/branches/rewrite/ber/src/test/org/apache/asn1/ber/BEREncoderTest.java
(original)
+++ incubator/directory/asn1/branches/rewrite/ber/src/test/org/apache/asn1/ber/BEREncoderTest.java
Wed Feb  2 23:18:42 2005
@@ -21,10 +21,7 @@
 import org.apache.asn1.ber.primitives.UniversalTag;
 import org.apache.asn1.codec.stateful.EncoderCallback;
 import org.apache.asn1.codec.stateful.StatefulEncoder;
-import org.apache.asn1.codec.stateful.StatefulEncoder;
-import org.apache.asn1.ber.primitives.UniversalTag;
-import org.apache.asn1.ber.BEREncoder;
-import org.apache.commons.lang.ArrayUtils;
+import org.apache.asn1.util.ArrayUtils;
 
 import java.nio.ByteBuffer;
 import java.util.ArrayList;

Modified: incubator/directory/asn1/branches/rewrite/ber/src/test/org/apache/asn1/ber/TagTest.java
URL: http://svn.apache.org/viewcvs/incubator/directory/asn1/branches/rewrite/ber/src/test/org/apache/asn1/ber/TagTest.java?view=diff&r1=151130&r2=151131
==============================================================================
--- incubator/directory/asn1/branches/rewrite/ber/src/test/org/apache/asn1/ber/TagTest.java
(original)
+++ incubator/directory/asn1/branches/rewrite/ber/src/test/org/apache/asn1/ber/TagTest.java
Wed Feb  2 23:18:42 2005
@@ -17,10 +17,8 @@
 package org.apache.asn1.ber ;
 
 
-import org.apache.commons.lang.ArrayUtils ;
 import org.apache.asn1.ber.primitives.UniversalTag;
-import org.apache.asn1.ber.primitives.UniversalTag;
-import org.apache.asn1.ber.Tag;
+import org.apache.asn1.util.ArrayUtils;
 
 import junit.framework.TestCase ;
 

Modified: incubator/directory/asn1/branches/rewrite/ber/src/test/org/apache/asn1/ber/TupleTest.java
URL: http://svn.apache.org/viewcvs/incubator/directory/asn1/branches/rewrite/ber/src/test/org/apache/asn1/ber/TupleTest.java?view=diff&r1=151130&r2=151131
==============================================================================
--- incubator/directory/asn1/branches/rewrite/ber/src/test/org/apache/asn1/ber/TupleTest.java
(original)
+++ incubator/directory/asn1/branches/rewrite/ber/src/test/org/apache/asn1/ber/TupleTest.java
Wed Feb  2 23:18:42 2005
@@ -21,10 +21,8 @@
 import java.util.ArrayList;
 import java.util.Collections;
 
-import org.apache.commons.lang.ArrayUtils ;
 import org.apache.asn1.codec.binary.BinaryCodec;
-import org.apache.asn1.ber.Length;
-import org.apache.asn1.ber.Tuple;
+import org.apache.asn1.util.ArrayUtils;
 
 import junit.framework.TestCase ;
 

Modified: incubator/directory/asn1/branches/rewrite/ber/src/test/org/apache/asn1/ber/TupleTreeDecoderTest.java
URL: http://svn.apache.org/viewcvs/incubator/directory/asn1/branches/rewrite/ber/src/test/org/apache/asn1/ber/TupleTreeDecoderTest.java?view=diff&r1=151130&r2=151131
==============================================================================
--- incubator/directory/asn1/branches/rewrite/ber/src/test/org/apache/asn1/ber/TupleTreeDecoderTest.java
(original)
+++ incubator/directory/asn1/branches/rewrite/ber/src/test/org/apache/asn1/ber/TupleTreeDecoderTest.java
Wed Feb  2 23:18:42 2005
@@ -26,12 +26,7 @@
 import org.apache.asn1.codec.stateful.DecoderCallback ;
 import org.apache.asn1.codec.stateful.DecoderMonitorAdapter;
 import org.apache.asn1.codec.stateful.StatefulDecoder ;
-import org.apache.asn1.codec.stateful.DecoderMonitorAdapter ;
-import org.apache.asn1.ber.DefaultMutableTupleNode;
-import org.apache.asn1.ber.Tuple;
-import org.apache.asn1.ber.TupleTreeDecoder;
-
-import org.apache.commons.lang.time.StopWatch ;
+import org.apache.asn1.util.StopWatch;
 
 import org.apache.ldap.common.message.MessageEncoder ;
 import org.apache.ldap.common.message.ModifyRequestImpl ;

Modified: incubator/directory/asn1/branches/rewrite/ber/src/test/org/apache/asn1/ber/primitives/PrimitiveUtilsTest.java
URL: http://svn.apache.org/viewcvs/incubator/directory/asn1/branches/rewrite/ber/src/test/org/apache/asn1/ber/primitives/PrimitiveUtilsTest.java?view=diff&r1=151130&r2=151131
==============================================================================
--- incubator/directory/asn1/branches/rewrite/ber/src/test/org/apache/asn1/ber/primitives/PrimitiveUtilsTest.java
(original)
+++ incubator/directory/asn1/branches/rewrite/ber/src/test/org/apache/asn1/ber/primitives/PrimitiveUtilsTest.java
Wed Feb  2 23:18:42 2005
@@ -21,8 +21,7 @@
 
 import java.math.BigInteger ;
 
-import org.apache.commons.lang.ArrayUtils;
-import org.apache.asn1.ber.primitives.PrimitiveUtils;
+import org.apache.asn1.util.ArrayUtils;
 
 
 /**



Mime
View raw message