You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by tr...@apache.org on 2006/01/20 14:48:55 UTC

svn commit: r370807 [8/22] - in /directory/sandbox/trustin/mina-spi: ./ core/src/main/java/org/apache/mina/common/ core/src/main/java/org/apache/mina/common/support/ core/src/main/java/org/apache/mina/common/support/discovery/ core/src/main/java/org/ap...

Added: directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/IntHashMap.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/IntHashMap.java?rev=370807&view=auto
==============================================================================
--- directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/IntHashMap.java (added)
+++ directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/IntHashMap.java Fri Jan 20 05:47:50 2006
@@ -0,0 +1,363 @@
+/*
+ * Copyright 2002-2005 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.
+ */
+
+/*
+ * Note: originally released under the GNU LGPL v2.1, 
+ * but rereleased by the original author under the ASF license (above).
+ */
+package org.apache.mina.common.support.lang;
+
+/**
+ * <p>A hash map that uses primitive ints for the key rather than objects.</p>
+ *
+ * <p>Note that this class is for internal optimization purposes only, and may
+ * not be supported in future releases of Jakarta Commons Lang.  Utilities of
+ * this sort may be included in future releases of Jakarta Commons Collections.</p>
+ *
+ * @author Justin Couch
+ * @author Alex Chaffee (alex@apache.org)
+ * @author Stephen Colebourne
+ * @since 2.0
+ * @version $Revision$
+ * @see java.util.HashMap
+ */
+class IntHashMap {
+
+    /**
+     * The hash table data.
+     */
+    private transient Entry table[];
+
+    /**
+     * The total number of entries in the hash table.
+     */
+    private transient int count;
+
+    /**
+     * The table is rehashed when its size exceeds this threshold.  (The
+     * value of this field is (int)(capacity * loadFactor).)
+     *
+     * @serial
+     */
+    private int threshold;
+
+    /**
+     * The load factor for the hashtable.
+     *
+     * @serial
+     */
+    private float loadFactor;
+
+    /**
+     * <p>Innerclass that acts as a datastructure to create a new entry in the
+     * table.</p>
+     */
+    private static class Entry {
+        int hash;
+        int key;
+        Object value;
+        Entry next;
+
+        /**
+         * <p>Create a new entry with the given values.</p>
+         *
+         * @param hash The code used to hash the object with
+         * @param key The key used to enter this in the table
+         * @param value The value for this key
+         * @param next A reference to the next entry in the table
+         */
+        protected Entry(int hash, int key, Object value, Entry next) {
+            this.hash = hash;
+            this.key = key;
+            this.value = value;
+            this.next = next;
+        }
+    }
+
+    /**
+     * <p>Constructs a new, empty hashtable with a default capacity and load
+     * factor, which is <code>20</code> and <code>0.75</code> respectively.</p>
+     */
+    public IntHashMap() {
+        this(20, 0.75f);
+    }
+
+    /**
+     * <p>Constructs a new, empty hashtable with the specified initial capacity
+     * and default load factor, which is <code>0.75</code>.</p>
+     *
+     * @param  initialCapacity the initial capacity of the hashtable.
+     * @throws IllegalArgumentException if the initial capacity is less
+     *   than zero.
+     */
+    public IntHashMap(int initialCapacity) {
+        this(initialCapacity, 0.75f);
+    }
+
+    /**
+     * <p>Constructs a new, empty hashtable with the specified initial
+     * capacity and the specified load factor.</p>
+     *
+     * @param initialCapacity the initial capacity of the hashtable.
+     * @param loadFactor the load factor of the hashtable.
+     * @throws IllegalArgumentException  if the initial capacity is less
+     *             than zero, or if the load factor is nonpositive.
+     */
+    public IntHashMap(int initialCapacity, float loadFactor) {
+        super();
+        if (initialCapacity < 0) {
+            throw new IllegalArgumentException("Illegal Capacity: " + initialCapacity);
+        }
+        if (loadFactor <= 0) {
+            throw new IllegalArgumentException("Illegal Load: " + loadFactor);
+        }
+        if (initialCapacity == 0) {
+            initialCapacity = 1;
+        }
+
+        this.loadFactor = loadFactor;
+        table = new Entry[initialCapacity];
+        threshold = (int) (initialCapacity * loadFactor);
+    }
+
+    /**
+     * <p>Returns the number of keys in this hashtable.</p>
+     *
+     * @return  the number of keys in this hashtable.
+     */
+    public int size() {
+        return count;
+    }
+
+    /**
+     * <p>Tests if this hashtable maps no keys to values.</p>
+     *
+     * @return  <code>true</code> if this hashtable maps no keys to values;
+     *          <code>false</code> otherwise.
+     */
+    public boolean isEmpty() {
+        return count == 0;
+    }
+
+    /**
+     * <p>Tests if some key maps into the specified value in this hashtable.
+     * This operation is more expensive than the <code>containsKey</code>
+     * method.</p>
+     *
+     * <p>Note that this method is identical in functionality to containsValue,
+     * (which is part of the Map interface in the collections framework).</p>
+     *
+     * @param      value   a value to search for.
+     * @return     <code>true</code> if and only if some key maps to the
+     *             <code>value</code> argument in this hashtable as
+     *             determined by the <tt>equals</tt> method;
+     *             <code>false</code> otherwise.
+     * @throws  NullPointerException  if the value is <code>null</code>.
+     * @see        #containsKey(int)
+     * @see        #containsValue(Object)
+     * @see        java.util.Map
+     */
+    public boolean contains(Object value) {
+        if (value == null) {
+            throw new NullPointerException();
+        }
+
+        Entry tab[] = table;
+        for (int i = tab.length; i-- > 0;) {
+            for (Entry e = tab[i]; e != null; e = e.next) {
+                if (e.value.equals(value)) {
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
+
+    /**
+     * <p>Returns <code>true</code> if this HashMap maps one or more keys
+     * to this value.</p>
+     *
+     * <p>Note that this method is identical in functionality to contains
+     * (which predates the Map interface).</p>
+     *
+     * @param value value whose presence in this HashMap is to be tested.
+     * @return boolean <code>true</code> if the value is contained
+     * @see    java.util.Map
+     * @since JDK1.2
+     */
+    public boolean containsValue(Object value) {
+        return contains(value);
+    }
+
+    /**
+     * <p>Tests if the specified object is a key in this hashtable.</p>
+     *
+     * @param  key  possible key.
+     * @return <code>true</code> if and only if the specified object is a
+     *    key in this hashtable, as determined by the <tt>equals</tt>
+     *    method; <code>false</code> otherwise.
+     * @see #contains(Object)
+     */
+    public boolean containsKey(int key) {
+        Entry tab[] = table;
+        int hash = key;
+        int index = (hash & 0x7FFFFFFF) % tab.length;
+        for (Entry e = tab[index]; e != null; e = e.next) {
+            if (e.hash == hash) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    /**
+     * <p>Returns the value to which the specified key is mapped in this map.</p>
+     *
+     * @param   key   a key in the hashtable.
+     * @return  the value to which the key is mapped in this hashtable;
+     *          <code>null</code> if the key is not mapped to any value in
+     *          this hashtable.
+     * @see     #put(int, Object)
+     */
+    public Object get(int key) {
+        Entry tab[] = table;
+        int hash = key;
+        int index = (hash & 0x7FFFFFFF) % tab.length;
+        for (Entry e = tab[index]; e != null; e = e.next) {
+            if (e.hash == hash) {
+                return e.value;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * <p>Increases the capacity of and internally reorganizes this
+     * hashtable, in order to accommodate and access its entries more
+     * efficiently.</p>
+     *
+     * <p>This method is called automatically when the number of keys
+     * in the hashtable exceeds this hashtable's capacity and load
+     * factor.</p>
+     */
+    protected void rehash() {
+        int oldCapacity = table.length;
+        Entry oldMap[] = table;
+
+        int newCapacity = oldCapacity * 2 + 1;
+        Entry newMap[] = new Entry[newCapacity];
+
+        threshold = (int) (newCapacity * loadFactor);
+        table = newMap;
+
+        for (int i = oldCapacity; i-- > 0;) {
+            for (Entry old = oldMap[i]; old != null;) {
+                Entry e = old;
+                old = old.next;
+
+                int index = (e.hash & 0x7FFFFFFF) % newCapacity;
+                e.next = newMap[index];
+                newMap[index] = e;
+            }
+        }
+    }
+
+    /**
+     * <p>Maps the specified <code>key</code> to the specified
+     * <code>value</code> in this hashtable. The key cannot be
+     * <code>null</code>. </p>
+     *
+     * <p>The value can be retrieved by calling the <code>get</code> method
+     * with a key that is equal to the original key.</p>
+     *
+     * @param key     the hashtable key.
+     * @param value   the value.
+     * @return the previous value of the specified key in this hashtable,
+     *         or <code>null</code> if it did not have one.
+     * @throws  NullPointerException  if the key is <code>null</code>.
+     * @see     #get(int)
+     */
+    public Object put(int key, Object value) {
+        // Makes sure the key is not already in the hashtable.
+        Entry tab[] = table;
+        int hash = key;
+        int index = (hash & 0x7FFFFFFF) % tab.length;
+        for (Entry e = tab[index]; e != null; e = e.next) {
+            if (e.hash == hash) {
+                Object old = e.value;
+                e.value = value;
+                return old;
+            }
+        }
+
+        if (count >= threshold) {
+            // Rehash the table if the threshold is exceeded
+            rehash();
+
+            tab = table;
+            index = (hash & 0x7FFFFFFF) % tab.length;
+        }
+
+        // Creates the new entry.
+        Entry e = new Entry(hash, key, value, tab[index]);
+        tab[index] = e;
+        count++;
+        return null;
+    }
+
+    /**
+     * <p>Removes the key (and its corresponding value) from this
+     * hashtable.</p>
+     *
+     * <p>This method does nothing if the key is not present in the
+     * hashtable.</p>
+     *
+     * @param   key   the key that needs to be removed.
+     * @return  the value to which the key had been mapped in this hashtable,
+     *          or <code>null</code> if the key did not have a mapping.
+     */
+    public Object remove(int key) {
+        Entry tab[] = table;
+        int hash = key;
+        int index = (hash & 0x7FFFFFFF) % tab.length;
+        for (Entry e = tab[index], prev = null; e != null; prev = e, e = e.next) {
+            if (e.hash == hash) {
+                if (prev != null) {
+                    prev.next = e.next;
+                } else {
+                    tab[index] = e.next;
+                }
+                count--;
+                Object oldValue = e.value;
+                e.value = null;
+                return oldValue;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * <p>Clears this hashtable so that it contains no keys.</p>
+     */
+    public synchronized void clear() {
+        Entry tab[] = table;
+        for (int index = tab.length; --index >= 0;) {
+            tab[index] = null;
+        }
+        count = 0;
+    }
+    
+}

Propchange: directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/IntHashMap.java
------------------------------------------------------------------------------
    svn:keywords = HeadURL Id LastChangedBy LastChangedDate LastChangedRevision

Added: directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/NotImplementedException.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/NotImplementedException.java?rev=370807&view=auto
==============================================================================
--- directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/NotImplementedException.java (added)
+++ directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/NotImplementedException.java Fri Jan 20 05:47:50 2006
@@ -0,0 +1,288 @@
+/*
+ * Copyright 2002-2005 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.mina.common.support.lang;
+
+import java.io.PrintStream;
+import java.io.PrintWriter;
+
+import org.apache.mina.common.support.lang.exception.Nestable;
+import org.apache.mina.common.support.lang.exception.NestableDelegate;
+
+/**
+ * <p>Thrown to indicate that a block of code has not been implemented.
+ * This exception supplements <code>UnsupportedOperationException</code>
+ * by providing a more semantically rich description of the problem.</p>
+ * 
+ * <p><code>NotImplementedException</code> represents the case where the
+ * author has yet to implement the logic at this point in the program.
+ * This can act as an exception based TODO tag.
+ * Because this logic might be within a catch block, this exception
+ * suports exception chaining.</p>
+ * 
+ * <pre>
+ * public void foo() {
+ *   try {
+ *     // do something that throws an Exception
+ *   } catch (Exception ex) {
+ *     // don't know what to do here yet
+ *     throw new NotImplementedException("TODO", ex);
+ *   }
+ * }
+ * </pre>
+ * 
+ * @author Matthew Hawthorne
+ * @author Stephen Colebourne
+ * @since 2.0
+ * @version $Id$
+ */
+public class NotImplementedException
+        extends UnsupportedOperationException implements Nestable {
+
+    /**
+     * The exception helper to delegate nested exception handling to.
+     */
+    private NestableDelegate delegate = new NestableDelegate(this);
+
+    /**
+     * Holds the reference to the exception or error that caused
+     * this exception to be thrown.
+     */
+    private Throwable cause;
+
+    //-----------------------------------------------------------------------
+    /**
+     * Constructs a new <code>NotImplementedException</code> with default message.
+     * 
+     * @since 2.1
+     */
+    public NotImplementedException() {
+        super("Code is not implemented");
+    }
+
+    /**
+     * Constructs a new <code>NotImplementedException</code> with specified
+     * detail message.
+     *
+     * @param msg  the error message.
+     */
+    public NotImplementedException(String msg) {
+        super(msg == null ? "Code is not implemented" : msg);
+    }
+
+    /**
+     * Constructs a new <code>NotImplementedException</code> with specified
+     * nested <code>Throwable</code> and default message.
+     *
+     * @param cause  the exception that caused this exception to be thrown
+     * @since 2.1
+     */
+    public NotImplementedException(Throwable cause) {
+        super("Code is not implemented");
+        this.cause = cause;
+    }
+
+    /**
+     * Constructs a new <code>NotImplementedException</code> with specified
+     * detail message and nested <code>Throwable</code>.
+     *
+     * @param msg  the error message
+     * @param cause  the exception that caused this exception to be thrown
+     * @since 2.1
+     */
+    public NotImplementedException(String msg, Throwable cause) {
+        super(msg == null ? "Code is not implemented" : msg);
+        this.cause = cause;
+    }
+
+    /**
+     * Constructs a new <code>NotImplementedException</code> referencing
+     * the specified class.
+     * 
+     * @param clazz  the <code>Class</code> that has not implemented the method
+     */
+    public NotImplementedException(Class clazz) {
+        super((clazz == null ?
+                "Code is not implemented" :
+                "Code is not implemented in " + clazz));
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * Gets the root cause of this exception.
+     * @return the root cause of this exception.
+     * 
+     * @since 2.1
+     */
+    public Throwable getCause() {
+        return cause;
+    }
+
+    /**
+     * Gets the combined the error message of this and any nested errors.
+     *
+     * @return the error message
+     * @since 2.1
+     */
+    public String getMessage() {
+        if (super.getMessage() != null) {
+            return super.getMessage();
+        } else if (cause != null) {
+            return cause.toString();
+        } else {
+            return null;
+        }
+    }
+
+    /**
+     * Returns the error message of the <code>Throwable</code> in the chain
+     * of <code>Throwable</code>s at the specified index, numbered from 0.
+     *
+     * @param index  the index of the <code>Throwable</code> in the chain
+     * @return the error message, or null if the <code>Throwable</code> at the
+     *  specified index in the chain does not contain a message
+     * @throws IndexOutOfBoundsException if the <code>index</code> argument is
+     *  negative or not less than the count of <code>Throwable</code>s in the chain
+     * @since 2.1
+     */
+    public String getMessage(int index) {
+        if (index == 0) {
+            return super.getMessage();
+        } else {
+            return delegate.getMessage(index);
+        }
+    }
+
+    /**
+     * Returns the error message of this and any nested <code>Throwable</code> objects.
+     * Each throwable returns a message, a null string is included in the array if
+     * there is no message for a particular <code>Throwable</code>.
+     *
+     * @return the error messages
+     * @since 2.1
+     */
+    public String[] getMessages() {
+        return delegate.getMessages();
+    }
+
+    /**
+     * Returns the <code>Throwable</code> in the chain by index.
+     *
+     * @param index  the index to retrieve
+     * @return the <code>Throwable</code>
+     * @throws IndexOutOfBoundsException if the <code>index</code> argument is
+     *  negative or not less than the count of <code>Throwable</code>s in the chain
+     * @since 2.1
+     */
+    public Throwable getThrowable(int index) {
+        return delegate.getThrowable(index);
+    }
+
+    /**
+     * Returns the number of nested <code>Throwable</code>s represented by
+     * this <code>Nestable</code>, including this <code>Nestable</code>.
+     *
+     * @return the throwable count
+     * @since 2.1
+     */
+    public int getThrowableCount() {
+        return delegate.getThrowableCount();
+    }
+
+    /**
+     * Returns this <code>Nestable</code> and any nested <code>Throwable</code>s
+     * in an array of <code>Throwable</code>s, one element for each
+     * <code>Throwable</code>.
+     *
+     * @return the <code>Throwable</code>s
+     * @since 2.1
+     */
+    public Throwable[] getThrowables() {
+        return delegate.getThrowables();
+    }
+
+    /**
+     * Returns the index of the first occurrence of the specified type.
+     * If there is no match, <code>-1</code> is returned.
+     *
+     * @param type  the type to search for
+     * @return index of the first occurrence of the type in the chain, or -1 if
+     *  the type is not found
+     * @since 2.1
+     */
+    public int indexOfThrowable(Class type) {
+        return delegate.indexOfThrowable(type, 0);
+    }
+
+    /**
+     * Returns the index of the first occurrence of the specified type starting
+     * from the specified index. If there is no match, <code>-1</code> is returned.
+     *
+     * @param type  the type to search for
+     * @param fromIndex  the index of the starting position in the chain to be searched
+     * @return index of the first occurrence of the type in the chain, or -1 if
+     *  the type is not found
+     * @throws IndexOutOfBoundsException if the <code>fromIndex</code> argument
+     *  is negative or not less than the count of <code>Throwable</code>s in the chain
+     * @since 2.1
+     */
+    public int indexOfThrowable(Class type, int fromIndex) {
+        return delegate.indexOfThrowable(type, fromIndex);
+    }
+
+    /**
+     * Prints the stack trace of this exception.
+     * Includes information from the exception, if any, which caused this exception.
+     * 
+     * @since 2.1
+     */
+    public void printStackTrace() {
+        delegate.printStackTrace();
+    }
+
+    /**
+     * Prints the stack trace of this exception to the specified stream.
+     * Includes information from the exception, if any, which caused this exception.
+     *
+     * @param out  the stream to write to
+     * @since 2.1
+     */
+    public void printStackTrace(PrintStream out) {
+        delegate.printStackTrace(out);
+    }
+
+    /**
+     * Prints the stack trace of this exception to the specified writer.
+     * Includes information from the exception, if any, which caused this exception.
+     *
+     * @param out  the writer to write to
+     * @since 2.1
+     */
+    public void printStackTrace(PrintWriter out) {
+        delegate.printStackTrace(out);
+    }
+
+    /**
+     * Prints the stack trace for this exception only (root cause not included)
+     * using the specified writer.
+     * 
+     * @param out  the writer to write to
+     * @since 2.1
+     */
+    public final void printPartialStackTrace(PrintWriter out) {
+        super.printStackTrace(out);
+    }
+
+}

Propchange: directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/NotImplementedException.java
------------------------------------------------------------------------------
    svn:keywords = HeadURL Id LastChangedBy LastChangedDate LastChangedRevision

Added: directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/NullArgumentException.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/NullArgumentException.java?rev=370807&view=auto
==============================================================================
--- directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/NullArgumentException.java (added)
+++ directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/NullArgumentException.java Fri Jan 20 05:47:50 2006
@@ -0,0 +1,56 @@
+/*
+ * Copyright 2002-2005 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.mina.common.support.lang;
+
+/**
+ * <p>Thrown to indicate that an argument was <code>null</code> and should
+ * not have been.
+ * This exception supplements the standard <code>IllegalArgumentException</code>
+ * by providing a more semantically rich description of the problem.</p>
+ * 
+ * <p><code>NullArgumentException</code> represents the case where a method takes
+ * in a parameter that must not be <code>null</code>.
+ * Some coding standards would use <code>NullPointerException</code> for this case,
+ * others will use <code>IllegalArgumentException</code>.
+ * Thus this exception would be used in place of
+ * <code>IllegalArgumentException</code>, yet it still extends it.</p>
+ * 
+ * <pre>
+ * public void foo(String str) {
+ *   if (str == null) {
+ *     throw new NullArgumentException("str");
+ *   }
+ *   // do something with the string
+ * }
+ * </pre>
+ * 
+ * @author Matthew Hawthorne
+ * @author Stephen Colebourne
+ * @since 2.0
+ * @version $Id$
+ */
+public class NullArgumentException extends IllegalArgumentException {
+
+   /**
+    * <p>Instantiates with the given argument name.</p>
+    *
+    * @param argName  the name of the argument that was <code>null</code>.
+    */
+    public NullArgumentException(String argName) {
+        super((argName == null ? "Argument" : argName) + " must not be null.");
+    }
+
+}

Propchange: directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/NullArgumentException.java
------------------------------------------------------------------------------
    svn:keywords = HeadURL Id LastChangedBy LastChangedDate LastChangedRevision

Added: directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/NumberRange.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/NumberRange.java?rev=370807&view=auto
==============================================================================
--- directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/NumberRange.java (added)
+++ directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/NumberRange.java Fri Jan 20 05:47:50 2006
@@ -0,0 +1,219 @@
+/*
+ * Copyright 2002-2005 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.mina.common.support.lang;
+
+/**
+ * <p>Represents a range of {@link Number} objects.</p>
+ * 
+ * <p>This class uses <code>double</code> comparisons. This means that it
+ * is unsuitable for dealing with large <code>Long</code>, <code>BigDecimal</code>
+ * or <code>BigInteger</code> numbers.</p>
+ *
+ * @author <a href="mailto:chrise@esha.com">Christopher Elkins</a>
+ * @author Stephen Colebourne
+ * @since 1.0
+ * @version $Revision$ $Date$
+ * 
+ * @deprecated Use one of the Range classes in org.apache.mina.common.support.lang.math.
+ *             Class will be removed in Commons Lang 3.0.
+ * 
+ */
+public final class NumberRange {
+
+    /* The minimum number in this range. */
+    private final Number min;
+
+    /* The maximum number in this range. */
+    private final Number max;
+
+
+    /**
+     * <p>Constructs a new <code>NumberRange</code> using
+     * <code>number</code> as both the minimum and maximum in
+     * this range.</p>
+     *
+     * @param num the number to use for this range
+     * @throws NullPointerException if the number is <code>null</code>
+     */
+    public NumberRange(Number num) {
+        if (num == null) {
+            throw new NullPointerException("The number must not be null");
+        }
+
+        this.min = num;
+        this.max = num;
+    }
+
+    /**
+     * <p>Constructs a new <code>NumberRange</code> with the specified
+     * minimum and maximum numbers.</p>
+     * 
+     * <p><em>If the maximum is less than the minimum, the range will be constructed
+     * from the minimum value to the minimum value, not what you would expect!.</em></p>
+     *
+     * @param min the minimum number in this range
+     * @param max the maximum number in this range
+     * @throws NullPointerException if either the minimum or maximum number is
+     *  <code>null</code>
+     */
+    public NumberRange(Number min, Number max) {
+        if (min == null) {
+            throw new NullPointerException("The minimum value must not be null");
+        } else if (max == null) {
+            throw new NullPointerException("The maximum value must not be null");
+        }
+
+        if (max.doubleValue() < min.doubleValue()) {
+            this.min = this.max = min;
+        } else {
+            this.min = min;
+            this.max = max;
+        }
+    }
+
+    /**
+     * <p>Returns the minimum number in this range.</p>
+     *
+     * @return the minimum number in this range
+     */
+    public Number getMinimum() {
+        return min;
+    }
+
+    /**
+     * <p>Returns the maximum number in this range.</p>
+     *
+     * @return the maximum number in this range
+     */
+    public Number getMaximum() {
+        return max;
+    }
+
+    /**
+     * <p>Tests whether the specified <code>number</code> occurs within
+     * this range using <code>double</code> comparison.</p>
+     *
+     * @param number the number to test
+     * @return <code>true</code> if the specified number occurs within this
+     *  range; otherwise, <code>false</code>
+     */
+    public boolean includesNumber(Number number) {
+        if (number == null) {
+            return false;
+        } else {
+            return !(min.doubleValue() > number.doubleValue()) &&
+                !(max.doubleValue() < number.doubleValue());
+        }
+    }
+
+    /**
+     * <p>Tests whether the specified range occurs entirely within this
+     * range using <code>double</code> comparison.</p>
+     *
+     * @param range the range to test
+     * @return <code>true</code> if the specified range occurs entirely within
+     *  this range; otherwise, <code>false</code>
+     */
+    public boolean includesRange(NumberRange range) {
+        if (range == null) {
+            return false;
+        } else {
+            return includesNumber(range.min) && includesNumber(range.max);
+        }
+    }
+
+    /**
+     * <p>Tests whether the specified range overlaps with this range
+     * using <code>double</code> comparison.</p>
+     *
+     * @param range the range to test
+     * @return <code>true</code> if the specified range overlaps with this
+     *  range; otherwise, <code>false</code>
+     */
+    public boolean overlaps(NumberRange range) {
+        if (range == null) {
+            return false;
+        } else {
+            return range.includesNumber(min) || range.includesNumber(max) || 
+                includesRange(range);
+        }
+    }
+
+    /**
+     * <p>Indicates whether some other <code>Object</code> is
+     * &quot;equal&quot; to this one.</p>
+     *
+     * @param obj the reference object with which to compare
+     * @return <code>true</code> if this object is the same as the obj
+     *  argument; <code>false</code> otherwise
+     */
+    public boolean equals(Object obj) {
+        if (obj == this) {
+            return true;
+        } else if (!(obj instanceof NumberRange)) {
+            return false;
+        } else {
+            NumberRange range = (NumberRange)obj;
+            return min.equals(range.min) && max.equals(range.max);
+        }
+    }
+
+    /**
+     * <p>Returns a hash code value for this object.</p>
+     *
+     * @return a hash code value for this object
+     */
+    public int hashCode() {
+        int result = 17;
+        result = 37 * result + min.hashCode();
+        result = 37 * result + max.hashCode();
+        return result;
+    }
+
+    /**
+     * <p>Returns the string representation of this range.</p>
+     *
+     * <p>This string is the string representation of the minimum and
+     * maximum numbers in the range, separated by a hyphen. If a number
+     * is negative, then it is enclosed in parentheses.</p>
+     *
+     * @return the string representation of this range
+     */
+    public String toString() {
+        StringBuffer sb = new StringBuffer();
+
+        if (min.doubleValue() < 0) {
+            sb.append('(')
+                .append(min)
+                .append(')');
+        } else {
+            sb.append(min);
+        }
+
+        sb.append('-');
+
+        if (max.doubleValue() < 0) {
+            sb.append('(')
+                .append(max)
+                .append(')');
+        } else {
+            sb.append(max);
+        }
+
+        return sb.toString();
+    }
+
+}

Propchange: directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/NumberRange.java
------------------------------------------------------------------------------
    svn:keywords = HeadURL Id LastChangedBy LastChangedDate LastChangedRevision

Added: directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/NumberUtils.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/NumberUtils.java?rev=370807&view=auto
==============================================================================
--- directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/NumberUtils.java (added)
+++ directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/NumberUtils.java Fri Jan 20 05:47:50 2006
@@ -0,0 +1,705 @@
+/*
+ * Copyright 2002-2005 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.mina.common.support.lang;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+
+/**
+ * <p>Provides extra functionality for Java Number classes.</p>
+ *
+ * @author <a href="mailto:bayard@generationjava.com">Henri Yandell</a>
+ * @author <a href="mailto:rand_mcneely@yahoo.com">Rand McNeely</a>
+ * @author Stephen Colebourne
+ * @author <a href="mailto:steve.downey@netfolio.com">Steve Downey</a>
+ * @author Eric Pugh
+ * @author Phil Steitz
+ * @since 1.0
+ * @version $Id$
+ * 
+ * @deprecated Moved to org.apache.mina.common.support.lang.math.
+ *             Class will be removed in Commons Lang 3.0.
+ */
+public final class NumberUtils {
+    // DEPRECATED CLASS !!!
+    
+    /**
+     * <p><code>NumberUtils</code> instances should NOT be constructed in standard programming.
+     * Instead, the class should be used as <code>NumberUtils.stringToInt("6");</code>.</p>
+     *
+     * <p>This constructor is public to permit tools that require a JavaBean instance
+     * to operate.</p>
+     */
+    public NumberUtils() {
+    }
+
+    //--------------------------------------------------------------------
+    
+    /**
+     * <p>Convert a <code>String</code> to an <code>int</code>, returning
+     * <code>zero</code> if the conversion fails.</p>
+     * 
+     * @param str  the string to convert
+     * @return the int represented by the string, or <code>zero</code> if
+     *  conversion fails
+     */
+    public static int stringToInt(String str) {
+        return stringToInt(str, 0);
+    }
+
+    /**
+     * <p>Convert a <code>String</code> to an <code>int</code>, returning a
+     * default value if the conversion fails.</p>
+     * 
+     * @param str  the string to convert
+     * @param defaultValue  the default value
+     * @return the int represented by the string, or the default if conversion fails
+     */
+    public static int stringToInt(String str, int defaultValue) {
+        try {
+            return Integer.parseInt(str);
+        } catch (NumberFormatException nfe) {
+            return defaultValue;
+        }
+    }
+
+    //--------------------------------------------------------------------
+    
+    // must handle Long, Float, Integer, Float, Short,
+    //                  BigDecimal, BigInteger and Byte
+    // useful methods:
+    // Byte.decode(String)
+    // Byte.valueOf(String,int radix)
+    // Byte.valueOf(String)
+    // Double.valueOf(String)
+    // Float.valueOf(String)
+    // new Float(String)
+    // Integer.valueOf(String,int radix)
+    // Integer.valueOf(String)
+    // Integer.decode(String)
+    // Integer.getInteger(String)
+    // Integer.getInteger(String,int val)
+    // Integer.getInteger(String,Integer val)
+    // new Integer(String)
+    // new Double(String)
+    // new Byte(String)
+    // new Long(String)
+    // Long.getLong(String)
+    // Long.getLong(String,int)
+    // Long.getLong(String,Integer)
+    // Long.valueOf(String,int)
+    // Long.valueOf(String)
+    // new Short(String)
+    // Short.decode(String)
+    // Short.valueOf(String,int)
+    // Short.valueOf(String)
+    // new BigDecimal(String)
+    // new BigInteger(String)
+    // new BigInteger(String,int radix)
+    // Possible inputs:
+    // 45 45.5 45E7 4.5E7 Hex Oct Binary xxxF xxxD xxxf xxxd
+    // plus minus everything. Prolly more. A lot are not separable.
+
+    /**
+     * <p>Turns a string value into a java.lang.Number.</p>
+     *
+     * <p>First, the value is examined for a type qualifier on the end
+     * (<code>'f','F','d','D','l','L'</code>).  If it is found, it starts 
+     * trying to create successively larger types from the type specified
+     * until one is found that can hold the value.</p>
+     *
+     * <p>If a type specifier is not found, it will check for a decimal point
+     * and then try successively larger types from <code>Integer</code> to
+     * <code>BigInteger</code> and from <code>Float</code> to
+     * <code>BigDecimal</code>.</p>
+     *
+     * <p>If the string starts with <code>0x</code> or <code>-0x</code>, it
+     * will be interpreted as a hexadecimal integer.  Values with leading
+     * <code>0</code>'s will not be interpreted as octal.</p>
+     *
+     * @param val String containing a number
+     * @return Number created from the string
+     * @throws NumberFormatException if the value cannot be converted
+     */
+    public static Number createNumber(String val) throws NumberFormatException {
+        if (val == null) {
+            return null;
+        }
+        if (val.length() == 0) {
+            throw new NumberFormatException("\"\" is not a valid number.");
+        }
+        if (val.startsWith("--")) {
+            // this is protection for poorness in java.lang.BigDecimal.
+            // it accepts this as a legal value, but it does not appear 
+            // to be in specification of class. OS X Java parses it to 
+            // a wrong value.
+            return null;
+        }
+        if (val.startsWith("0x") || val.startsWith("-0x")) {
+            return createInteger(val);
+        }   
+        char lastChar = val.charAt(val.length() - 1);
+        String mant;
+        String dec;
+        String exp;
+        int decPos = val.indexOf('.');
+        int expPos = val.indexOf('e') + val.indexOf('E') + 1;
+
+        if (decPos > -1) {
+
+            if (expPos > -1) {
+                if (expPos < decPos) {
+                    throw new NumberFormatException(val + " is not a valid number.");
+                }
+                dec = val.substring(decPos + 1, expPos);
+            } else {
+                dec = val.substring(decPos + 1);
+            }
+            mant = val.substring(0, decPos);
+        } else {
+            if (expPos > -1) {
+                mant = val.substring(0, expPos);
+            } else {
+                mant = val;
+            }
+            dec = null;
+        }
+        if (!Character.isDigit(lastChar)) {
+            if (expPos > -1 && expPos < val.length() - 1) {
+                exp = val.substring(expPos + 1, val.length() - 1);
+            } else {
+                exp = null;
+            }
+            //Requesting a specific type..
+            String numeric = val.substring(0, val.length() - 1);
+            boolean allZeros = isAllZeros(mant) && isAllZeros(exp);
+            switch (lastChar) {
+                case 'l' :
+                case 'L' :
+                    if (dec == null
+                        && exp == null
+                        && isDigits(numeric.substring(1))
+                        && (numeric.charAt(0) == '-' || Character.isDigit(numeric.charAt(0)))) {
+                        try {
+                            return createLong(numeric);
+                        } catch (NumberFormatException nfe) {
+                            //Too big for a long
+                        }
+                        return createBigInteger(numeric);
+
+                    }
+                    throw new NumberFormatException(val + " is not a valid number.");
+                case 'f' :
+                case 'F' :
+                    try {
+                        Float f = NumberUtils.createFloat(numeric);
+                        if (!(f.isInfinite() || (f.floatValue() == 0.0F && !allZeros))) {
+                            //If it's too big for a float or the float value = 0 and the string
+                            //has non-zeros in it, then float does not have the precision we want
+                            return f;
+                        }
+
+                    } catch (NumberFormatException nfe) {
+                    }
+                    //Fall through
+                case 'd' :
+                case 'D' :
+                    try {
+                        Double d = NumberUtils.createDouble(numeric);
+                        if (!(d.isInfinite() || (d.floatValue() == 0.0D && !allZeros))) {
+                            return d;
+                        }
+                    } catch (NumberFormatException nfe) {
+                    }
+                    try {
+                        return createBigDecimal(numeric);
+                    } catch (NumberFormatException e) {
+                    }
+                    //Fall through
+                default :
+                    throw new NumberFormatException(val + " is not a valid number.");
+
+            }
+        } else {
+            //User doesn't have a preference on the return type, so let's start
+            //small and go from there...
+            if (expPos > -1 && expPos < val.length() - 1) {
+                exp = val.substring(expPos + 1, val.length());
+            } else {
+                exp = null;
+            }
+            if (dec == null && exp == null) {
+                //Must be an int,long,bigint
+                try {
+                    return createInteger(val);
+                } catch (NumberFormatException nfe) {
+                }
+                try {
+                    return createLong(val);
+                } catch (NumberFormatException nfe) {
+                }
+                return createBigInteger(val);
+
+            } else {
+                //Must be a float,double,BigDec
+                boolean allZeros = isAllZeros(mant) && isAllZeros(exp);
+                try {
+                    Float f = createFloat(val);
+                    if (!(f.isInfinite() || (f.floatValue() == 0.0F && !allZeros))) {
+                        return f;
+                    }
+                } catch (NumberFormatException nfe) {
+                }
+                try {
+                    Double d = createDouble(val);
+                    if (!(d.isInfinite() || (d.doubleValue() == 0.0D && !allZeros))) {
+                        return d;
+                    }
+                } catch (NumberFormatException nfe) {
+                }
+
+                return createBigDecimal(val);
+
+            }
+
+        }
+    }
+
+    /**
+     * <p>Utility method for {@link #createNumber(java.lang.String)}.</p>
+     *
+     * <p>Returns <code>true</code> if s is <code>null</code>.</p>
+     * 
+     * @param s the String to check
+     * @return if it is all zeros or <code>null</code>
+     */
+    private static boolean isAllZeros(String s) {
+        if (s == null) {
+            return true;
+        }
+        for (int i = s.length() - 1; i >= 0; i--) {
+            if (s.charAt(i) != '0') {
+                return false;
+            }
+        }
+        return s.length() > 0;
+    }
+
+    //--------------------------------------------------------------------
+    
+    /**
+     * <p>Convert a <code>String</code> to a <code>Float</code>.</p>
+     * 
+     * @param val  a <code>String</code> to convert
+     * @return converted <code>Float</code>
+     * @throws NumberFormatException if the value cannot be converted
+     */
+    public static Float createFloat(String val) {
+        return Float.valueOf(val);
+    }
+
+    /**
+     * <p>Convert a <code>String</code> to a <code>Double</code>.</p>
+     * 
+     * @param val  a <code>String</code> to convert
+     * @return converted <code>Double</code>
+     * @throws NumberFormatException if the value cannot be converted
+     */
+    public static Double createDouble(String val) {
+        return Double.valueOf(val);
+    }
+
+    /**
+     * <p>Convert a <code>String</code> to a <code>Integer</code>, handling
+     * hex and octal notations.</p>
+     * 
+     * @param val  a <code>String</code> to convert
+     * @return converted <code>Integer</code>
+     * @throws NumberFormatException if the value cannot be converted
+     */
+    public static Integer createInteger(String val) {
+        // decode() handles 0xAABD and 0777 (hex and octal) as well.
+        return Integer.decode(val);
+    }
+
+    /**
+     * <p>Convert a <code>String</code> to a <code>Long</code>.</p>
+     * 
+     * @param val  a <code>String</code> to convert
+     * @return converted <code>Long</code>
+     * @throws NumberFormatException if the value cannot be converted
+     */
+    public static Long createLong(String val) {
+        return Long.valueOf(val);
+    }
+
+    /**
+     * <p>Convert a <code>String</code> to a <code>BigInteger</code>.</p>
+     * 
+     * @param val  a <code>String</code> to convert
+     * @return converted <code>BigInteger</code>
+     * @throws NumberFormatException if the value cannot be converted
+     */
+    public static BigInteger createBigInteger(String val) {
+        BigInteger bi = new BigInteger(val);
+        return bi;
+    }
+
+    /**
+     * <p>Convert a <code>String</code> to a <code>BigDecimal</code>.</p>
+     * 
+     * @param val  a <code>String</code> to convert
+     * @return converted <code>BigDecimal</code>
+     * @throws NumberFormatException if the value cannot be converted
+     */
+    public static BigDecimal createBigDecimal(String val) {
+        BigDecimal bd = new BigDecimal(val);
+        return bd;
+    }
+
+    //--------------------------------------------------------------------
+    
+    /**
+     * <p>Gets the minimum of three <code>long</code> values.</p>
+     * 
+     * @param a  value 1
+     * @param b  value 2
+     * @param c  value 3
+     * @return  the smallest of the values
+     */
+    public static long minimum(long a, long b, long c) {
+        if (b < a) {
+            a = b;
+        }
+        if (c < a) {
+            a = c;
+        }
+        return a;
+    }
+
+    /**
+     * <p>Gets the minimum of three <code>int</code> values.</p>
+     * 
+     * @param a  value 1
+     * @param b  value 2
+     * @param c  value 3
+     * @return  the smallest of the values
+     */
+    public static int minimum(int a, int b, int c) {
+        if (b < a) {
+            a = b;
+        }
+        if (c < a) {
+            a = c;
+        }
+        return a;
+    }
+
+    /**
+     * <p>Gets the maximum of three <code>long</code> values.</p>
+     * 
+     * @param a  value 1
+     * @param b  value 2
+     * @param c  value 3
+     * @return  the largest of the values
+     */
+    public static long maximum(long a, long b, long c) {
+        if (b > a) {
+            a = b;
+        }
+        if (c > a) {
+            a = c;
+        }
+        return a;
+    }
+
+    /**
+     * <p>Gets the maximum of three <code>int</code> values.</p>
+     * 
+     * @param a  value 1
+     * @param b  value 2
+     * @param c  value 3
+     * @return  the largest of the values
+     */
+    public static int maximum(int a, int b, int c) {
+        if (b > a) {
+            a = b;
+        }
+        if (c > a) {
+            a = c;
+        }
+        return a;
+    }
+
+    //--------------------------------------------------------------------
+    
+    /**
+     * <p>Compares two <code>doubles</code> for order.</p>
+     *
+     * <p>This method is more comprehensive than the standard Java greater
+     * than, less than and equals operators.</p>
+     * <ul>
+     *  <li>It returns <code>-1</code> if the first value is less than the second.
+     *  <li>It returns <code>+1</code> if the first value is greater than the second.
+     *  <li>It returns <code>0</code> if the values are equal.
+     * </ul>
+     *
+     * <p>
+     * The ordering is as follows, largest to smallest:
+     * <ul>
+     *  <li>NaN
+     *  <li>Positive infinity
+     *  <li>Maximum double
+     *  <li>Normal positive numbers
+     *  <li>+0.0
+     *  <li>-0.0
+     *  <li>Normal negative numbers
+     *  <li>Minimum double (-Double.MAX_VALUE)
+     *  <li>Negative infinity
+     * </ul>
+     * </p>
+     *
+     * <p>Comparing <code>NaN</code> with <code>NaN</code> will
+     * return <code>0</code>.</p>
+     * 
+     * @param lhs  the first <code>double</code>
+     * @param rhs  the second <code>double</code>
+     * @return <code>-1</code> if lhs is less, <code>+1</code> if greater,
+     *  <code>0</code> if equal to rhs
+     */
+    public static int compare(double lhs, double rhs) {
+        if (lhs < rhs) {
+            return -1;
+        }
+        if (lhs > rhs) {
+            return +1;
+        }
+        // Need to compare bits to handle 0.0 == -0.0 being true
+        // compare should put -0.0 < +0.0
+        // Two NaNs are also == for compare purposes
+        // where NaN == NaN is false
+        long lhsBits = Double.doubleToLongBits(lhs);
+        long rhsBits = Double.doubleToLongBits(rhs);
+        if (lhsBits == rhsBits) {
+            return 0;
+        }
+        // Something exotic! A comparison to NaN or 0.0 vs -0.0
+        // Fortunately NaN's long is > than everything else
+        // Also negzeros bits < poszero
+        // NAN: 9221120237041090560
+        // MAX: 9218868437227405311
+        // NEGZERO: -9223372036854775808
+        if (lhsBits < rhsBits) {
+            return -1;
+        } else {
+            return +1;
+        }
+    }
+    
+    /**
+     * <p>Compares two floats for order.</p>
+     *
+     * <p>This method is more comprehensive than the standard Java greater than,
+     * less than and equals operators.</p>
+     * <ul>
+     *  <li>It returns <code>-1</code> if the first value is less than the second.
+     *  <li>It returns <code>+1</code> if the first value is greater than the second.
+     *  <li>It returns <code>0</code> if the values are equal.
+     * </ul>
+     *
+     * <p> The ordering is as follows, largest to smallest:
+     * <ul>
+     * <li>NaN
+     * <li>Positive infinity
+     * <li>Maximum float
+     * <li>Normal positive numbers
+     * <li>+0.0
+     * <li>-0.0
+     * <li>Normal negative numbers
+     * <li>Minimum float (-Float.MAX_VALUE)
+     * <li>Negative infinity
+     * </ul>
+     *
+     * <p>Comparing <code>NaN</code> with <code>NaN</code> will return
+     * <code>0</code>.</p>
+     * 
+     * @param lhs  the first <code>float</code>
+     * @param rhs  the second <code>float</code>
+     * @return <code>-1</code> if lhs is less, <code>+1</code> if greater,
+     *  <code>0</code> if equal to rhs
+     */
+    public static int compare(float lhs, float rhs) {
+        if (lhs < rhs) {
+            return -1;
+        }
+        if (lhs > rhs) {
+            return +1;
+        }
+        //Need to compare bits to handle 0.0 == -0.0 being true
+        // compare should put -0.0 < +0.0
+        // Two NaNs are also == for compare purposes
+        // where NaN == NaN is false
+        int lhsBits = Float.floatToIntBits(lhs);
+        int rhsBits = Float.floatToIntBits(rhs);
+        if (lhsBits == rhsBits) {
+            return 0;
+        }
+        //Something exotic! A comparison to NaN or 0.0 vs -0.0
+        //Fortunately NaN's int is > than everything else
+        //Also negzeros bits < poszero
+        //NAN: 2143289344
+        //MAX: 2139095039
+        //NEGZERO: -2147483648
+        if (lhsBits < rhsBits) {
+            return -1;
+        } else {
+            return +1;
+        }
+    }
+    
+    //--------------------------------------------------------------------
+    
+    /**
+     * <p>Checks whether the <code>String</code> contains only
+     * digit characters.</p>
+     *
+     * <p><code>Null</code> and empty String will return
+     * <code>false</code>.</p>
+     *
+     * @param str  the <code>String</code> to check
+     * @return <code>true</code> if str contains only unicode numeric
+     */
+    public static boolean isDigits(String str) {
+        if ((str == null) || (str.length() == 0)) {
+            return false;
+        }
+        for (int i = 0; i < str.length(); i++) {
+            if (!Character.isDigit(str.charAt(i))) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    /**
+     * <p>Checks whether the String a valid Java number.</p>
+     *
+     * <p>Valid numbers include hexadecimal marked with the <code>0x</code>
+     * qualifier, scientific notation and numbers marked with a type
+     * qualifier (e.g. 123L).</p>
+     *
+     * <p><code>Null</code> and empty String will return
+     * <code>false</code>.</p>
+     *
+     * @param str  the <code>String</code> to check
+     * @return <code>true</code> if the string is a correctly formatted number
+     */
+    public static boolean isNumber(String str) {
+        if (StringUtils.isEmpty(str)) {
+            return false;
+        }
+        char[] chars = str.toCharArray();
+        int sz = chars.length;
+        boolean hasExp = false;
+        boolean hasDecPoint = false;
+        boolean allowSigns = false;
+        boolean foundDigit = false;
+        // deal with any possible sign up front
+        int start = (chars[0] == '-') ? 1 : 0;
+        if (sz > start + 1) {
+            if (chars[start] == '0' && chars[start + 1] == 'x') {
+                int i = start + 2;
+                if (i == sz) {
+                    return false; // str == "0x"
+                }
+                // checking hex (it can't be anything else)
+                for (; i < chars.length; i++) {
+                    if ((chars[i] < '0' || chars[i] > '9')
+                        && (chars[i] < 'a' || chars[i] > 'f')
+                        && (chars[i] < 'A' || chars[i] > 'F')) {
+                        return false;
+                    }
+                }
+                return true;
+            }
+        }
+        sz--; // don't want to loop to the last char, check it afterwords
+              // for type qualifiers
+        int i = start;
+        // loop to the next to last char or to the last char if we need another digit to
+        // make a valid number (e.g. chars[0..5] = "1234E")
+        while (i < sz || (i < sz + 1 && allowSigns && !foundDigit)) {
+            if (chars[i] >= '0' && chars[i] <= '9') {
+                foundDigit = true;
+                allowSigns = false;
+
+            } else if (chars[i] == '.') {
+                if (hasDecPoint || hasExp) {
+                    // two decimal points or dec in exponent   
+                    return false;
+                }
+                hasDecPoint = true;
+            } else if (chars[i] == 'e' || chars[i] == 'E') {
+                // we've already taken care of hex.
+                if (hasExp) {
+                    // two E's
+                    return false;
+                }
+                if (!foundDigit) {
+                    return false;
+                }
+                hasExp = true;
+                allowSigns = true;
+            } else if (chars[i] == '+' || chars[i] == '-') {
+                if (!allowSigns) {
+                    return false;
+                }
+                allowSigns = false;
+                foundDigit = false; // we need a digit after the E
+            } else {
+                return false;
+            }
+            i++;
+        }
+        if (i < chars.length) {
+            if (chars[i] >= '0' && chars[i] <= '9') {
+                // no type qualifier, OK
+                return true;
+            }
+            if (chars[i] == 'e' || chars[i] == 'E') {
+                // can't have an E at the last byte
+                return false;
+            }
+            if (!allowSigns
+                && (chars[i] == 'd'
+                    || chars[i] == 'D'
+                    || chars[i] == 'f'
+                    || chars[i] == 'F')) {
+                return foundDigit;
+            }
+            if (chars[i] == 'l'
+                || chars[i] == 'L') {
+                // not allowing L with an exponent
+                return foundDigit && !hasExp;
+            }
+            // last character is illegal
+            return false;
+        }
+        // allowSigns is true iff the val ends in 'E'
+        // found digit it to make sure weird stuff like '.' and '1E-' doesn't pass
+        return !allowSigns && foundDigit;
+    }
+}

Propchange: directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/NumberUtils.java
------------------------------------------------------------------------------
    svn:keywords = HeadURL Id LastChangedBy LastChangedDate LastChangedRevision

Added: directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/ObjectUtils.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/ObjectUtils.java?rev=370807&view=auto
==============================================================================
--- directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/ObjectUtils.java (added)
+++ directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/ObjectUtils.java Fri Jan 20 05:47:50 2006
@@ -0,0 +1,273 @@
+/*
+ * Copyright 2002-2005 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.mina.common.support.lang;
+
+import java.io.Serializable;
+
+/**
+ * <p>Operations on <code>Object</code>.</p>
+ * 
+ * <p>This class tries to handle <code>null</code> input gracefully.
+ * An exception will generally not be thrown for a <code>null</code> input.
+ * Each method documents its behaviour in more detail.</p>
+ *
+ * @author <a href="mailto:nissim@nksystems.com">Nissim Karpenstein</a>
+ * @author <a href="mailto:janekdb@yahoo.co.uk">Janek Bogucki</a>
+ * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
+ * @author Stephen Colebourne
+ * @author Gary Gregory
+ * @author Mario Winterer
+ * @since 1.0
+ * @version $Id$
+ */
+public class ObjectUtils {
+    
+    /**
+     * <p>Singleton used as a <code>null</code> placeholder where
+     * <code>null</code> has another meaning.</p>
+     *
+     * <p>For example, in a <code>HashMap</code> the
+     * {@link java.util.HashMap#get(java.lang.Object)} method returns
+     * <code>null</code> if the <code>Map</code> contains
+     * <code>null</code> or if there is no matching key. The
+     * <code>Null</code> placeholder can be used to distinguish between
+     * these two cases.</p>
+     *
+     * <p>Another example is <code>Hashtable</code>, where <code>null</code>
+     * cannot be stored.</p>
+     *
+     * <p>This instance is Serializable.</p>
+     */
+    public static final Null NULL = new Null();
+    
+    /**
+     * <p><code>ObjectUtils</code> instances should NOT be constructed in
+     * standard programming. Instead, the class should be used as
+     * <code>ObjectUtils.defaultIfNull("a","b");</code>.</p>
+     *
+     * <p>This constructor is public to permit tools that require a JavaBean instance
+     * to operate.</p>
+     */
+    public ObjectUtils() {
+    }
+
+    // Defaulting
+    //-----------------------------------------------------------------------
+    /**
+     * <p>Returns a default value if the object passed is
+     * <code>null</code>.</p>
+     * 
+     * <pre>
+     * ObjectUtils.defaultIfNull(null, null)      = null
+     * ObjectUtils.defaultIfNull(null, "")        = ""
+     * ObjectUtils.defaultIfNull(null, "zz")      = "zz"
+     * ObjectUtils.defaultIfNull("abc", *)        = "abc"
+     * ObjectUtils.defaultIfNull(Boolean.TRUE, *) = Boolean.TRUE
+     * </pre>
+     *
+     * @param object  the <code>Object</code> to test, may be <code>null</code>
+     * @param defaultValue  the default value to return, may be <code>null</code>
+     * @return <code>object</code> if it is not <code>null</code>, defaultValue otherwise
+     */
+    public static Object defaultIfNull(Object object, Object defaultValue) {
+        return object != null ? object : defaultValue;
+    }
+
+    /**
+     * <p>Compares two objects for equality, where either one or both
+     * objects may be <code>null</code>.</p>
+     *
+     * <pre>
+     * ObjectUtils.equals(null, null)                  = true
+     * ObjectUtils.equals(null, "")                    = false
+     * ObjectUtils.equals("", null)                    = false
+     * ObjectUtils.equals("", "")                      = true
+     * ObjectUtils.equals(Boolean.TRUE, null)          = false
+     * ObjectUtils.equals(Boolean.TRUE, "true")        = false
+     * ObjectUtils.equals(Boolean.TRUE, Boolean.TRUE)  = true
+     * ObjectUtils.equals(Boolean.TRUE, Boolean.FALSE) = false
+     * </pre>
+     *
+     * @param object1  the first object, may be <code>null</code>
+     * @param object2  the second object, may be <code>null</code>
+     * @return <code>true</code> if the values of both objects are the same
+     */
+    public static boolean equals(Object object1, Object object2) {
+        if (object1 == object2) {
+            return true;
+        }
+        if ((object1 == null) || (object2 == null)) {
+            return false;
+        }
+        return object1.equals(object2);
+    }
+
+    /**
+     * <p>Gets the hash code of an object returning zero when the
+     * object is <code>null</code>.</p>
+     *
+     * <pre>
+     * ObjectUtils.hashCode(null)   = 0
+     * ObjectUtils.hashCode(obj)    = obj.hashCode()
+     * </pre>
+     *
+     * @param obj  the object to obtain the hash code of, may be <code>null</code>
+     * @return the hash code of the object, or zero if null
+     * @since 2.1
+     */
+    public static int hashCode(Object obj) {
+        return (obj == null) ? 0 : obj.hashCode();
+    }
+
+    // Identity ToString
+    //-----------------------------------------------------------------------
+    /**
+     * <p>Gets the toString that would be produced by <code>Object</code>
+     * if a class did not override toString itself. <code>null</code>
+     * will return <code>null</code>.</p>
+     *
+     * <pre>
+     * ObjectUtils.identityToString(null)         = null
+     * ObjectUtils.identityToString("")           = "java.lang.String@1e23"
+     * ObjectUtils.identityToString(Boolean.TRUE) = "java.lang.Boolean@7fa"
+     * </pre>
+     *
+     * @param object  the object to create a toString for, may be
+     *  <code>null</code>
+     * @return the default toString text, or <code>null</code> if
+     *  <code>null</code> passed in
+     */
+    public static String identityToString(Object object) {
+        if (object == null) {
+            return null;
+        }
+        return appendIdentityToString(null, object).toString();
+    }
+
+    /**
+     * <p>Appends the toString that would be produced by <code>Object</code>
+     * if a class did not override toString itself. <code>null</code>
+     * will return <code>null</code>.</p>
+     *
+     * <pre>
+     * ObjectUtils.appendIdentityToString(*, null)            = null
+     * ObjectUtils.appendIdentityToString(null, "")           = "java.lang.String@1e23"
+     * ObjectUtils.appendIdentityToString(null, Boolean.TRUE) = "java.lang.Boolean@7fa"
+     * ObjectUtils.appendIdentityToString(buf, Boolean.TRUE)  = buf.append("java.lang.Boolean@7fa")
+     * </pre>
+     *
+     * @param buffer  the buffer to append to, may be <code>null</code>
+     * @param object  the object to create a toString for, may be <code>null</code>
+     * @return the default toString text, or <code>null</code> if
+     *  <code>null</code> passed in
+     * @since 2.0
+     */
+    public static StringBuffer appendIdentityToString(StringBuffer buffer, Object object) {
+        if (object == null) {
+            return null;
+        }
+        if (buffer == null) {
+            buffer = new StringBuffer();
+        }
+        return buffer
+            .append(object.getClass().getName())
+            .append('@')
+            .append(Integer.toHexString(System.identityHashCode(object)));
+    }
+
+    // ToString
+    //-----------------------------------------------------------------------
+    /**
+     * <p>Gets the <code>toString</code> of an <code>Object</code> returning
+     * an empty string ("") if <code>null</code> input.</p>
+     * 
+     * <pre>
+     * ObjectUtils.toString(null)         = ""
+     * ObjectUtils.toString("")           = ""
+     * ObjectUtils.toString("bat")        = "bat"
+     * ObjectUtils.toString(Boolean.TRUE) = "true"
+     * </pre>
+     * 
+     * @see StringUtils#defaultString(String)
+     * @see String#valueOf(Object)
+     * @param obj  the Object to <code>toString</code>, may be null
+     * @return the passed in Object's toString, or nullStr if <code>null</code> input
+     * @since 2.0
+     */
+    public static String toString(Object obj) {
+        return obj == null ? "" : obj.toString();
+    }
+
+    /**
+     * <p>Gets the <code>toString</code> of an <code>Object</code> returning
+     * a specified text if <code>null</code> input.</p>
+     * 
+     * <pre>
+     * ObjectUtils.toString(null, null)           = null
+     * ObjectUtils.toString(null, "null")         = "null"
+     * ObjectUtils.toString("", "null")           = ""
+     * ObjectUtils.toString("bat", "null")        = "bat"
+     * ObjectUtils.toString(Boolean.TRUE, "null") = "true"
+     * </pre>
+     * 
+     * @see StringUtils#defaultString(String,String)
+     * @see String#valueOf(Object)
+     * @param obj  the Object to <code>toString</code>, may be null
+     * @param nullStr  the String to return if <code>null</code> input, may be null
+     * @return the passed in Object's toString, or nullStr if <code>null</code> input
+     * @since 2.0
+     */
+    public static String toString(Object obj, String nullStr) {
+        return obj == null ? nullStr : obj.toString();
+    }
+
+    // Null
+    //-----------------------------------------------------------------------
+    /**
+     * <p>Class used as a null placeholder where <code>null</code>
+     * has another meaning.</p>
+     *
+     * <p>For example, in a <code>HashMap</code> the
+     * {@link java.util.HashMap#get(java.lang.Object)} method returns
+     * <code>null</code> if the <code>Map</code> contains
+     * <code>null</code> or if there is no matching key. The
+     * <code>Null</code> placeholder can be used to distinguish between
+     * these two cases.</p>
+     *
+     * <p>Another example is <code>Hashtable</code>, where <code>null</code>
+     * cannot be stored.</p>
+     */
+    public static class Null implements Serializable {
+        // declare serialization compatibility with Commons Lang 1.0
+        private static final long serialVersionUID = 7092611880189329093L;
+        
+        /**
+         * Restricted constructor - singleton.
+         */
+        Null() {
+        }
+        
+        /**
+         * <p>Ensure singleton.</p>
+         * 
+         * @return the singleton value
+         */
+        private Object readResolve() {
+            return ObjectUtils.NULL;
+        }
+    }
+    
+}

Propchange: directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/ObjectUtils.java
------------------------------------------------------------------------------
    svn:keywords = HeadURL Id LastChangedBy LastChangedDate LastChangedRevision

Added: directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/RandomStringUtils.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/RandomStringUtils.java?rev=370807&view=auto
==============================================================================
--- directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/RandomStringUtils.java (added)
+++ directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/RandomStringUtils.java Fri Jan 20 05:47:50 2006
@@ -0,0 +1,294 @@
+/*
+ * Copyright 2002-2005 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.mina.common.support.lang;
+
+import java.util.Random;
+/**
+ * <p>Operations for random <code>String</code>s.</p>
+ *
+ * @author GenerationJava Core library
+ * @author <a href="mailto:bayard@generationjava.com">Henri Yandell</a>
+ * @author <a href="mailto:steven@caswell.name">Steven Caswell</a>
+ * @author Stephen Colebourne
+ * @author Gary Gregory
+ * @author Phil Steitz
+ * @since 1.0
+ * @version $Id$
+ */
+public class RandomStringUtils {
+
+    /**
+     * <p>Random object used by random method. This has to be not local
+     * to the random method so as to not return the same value in the 
+     * same millisecond.</p>
+     */
+    private static final Random RANDOM = new Random();
+
+    /**
+     * <p><code>RandomStringUtils</code> instances should NOT be constructed in
+     * standard programming. Instead, the class should be used as
+     * <code>RandomStringUtils.random(5);</code>.</p>
+     *
+     * <p>This constructor is public to permit tools that require a JavaBean instance
+     * to operate.</p>
+     */
+    public RandomStringUtils() {
+    }
+
+    // Random
+    //-----------------------------------------------------------------------
+    /**
+     * <p>Creates a random string whose length is the number of characters
+     * specified.</p>
+     *
+     * <p>Characters will be chosen from the set of all characters.</p>
+     *
+     * @param count  the length of random string to create
+     * @return the random string
+     */
+    public static String random(int count) {
+        return random(count, false, false);
+    }
+
+    /**
+     * <p>Creates a random string whose length is the number of characters
+     * specified.</p>
+     *
+     * <p>Characters will be chosen from the set of characters whose
+     * ASCII value is between <code>32</code> and <code>126</code> (inclusive).</p>
+     *
+     * @param count  the length of random string to create
+     * @return the random string
+     */
+    public static String randomAscii(int count) {
+        return random(count, 32, 127, false, false);
+    }
+    
+    /**
+     * <p>Creates a random string whose length is the number of characters
+     * specified.</p>
+     *
+     * <p>Characters will be chosen from the set of alphabetic
+     * characters.</p>
+     *
+     * @param count  the length of random string to create
+     * @return the random string
+     */
+    public static String randomAlphabetic(int count) {
+        return random(count, true, false);
+    }
+    
+    /**
+     * <p>Creates a random string whose length is the number of characters
+     * specified.</p>
+     *
+     * <p>Characters will be chosen from the set of alpha-numeric
+     * characters.</p>
+     *
+     * @param count  the length of random string to create
+     * @return the random string
+     */
+    public static String randomAlphanumeric(int count) {
+        return random(count, true, true);
+    }
+    
+    /**
+     * <p>Creates a random string whose length is the number of characters
+     * specified.</p>
+     *
+     * <p>Characters will be chosen from the set of numeric
+     * characters.</p>
+     *
+     * @param count  the length of random string to create
+     * @return the random string
+     */
+    public static String randomNumeric(int count) {
+        return random(count, false, true);
+    }
+
+    /**
+     * <p>Creates a random string whose length is the number of characters
+     * specified.</p>
+     *
+     * <p>Characters will be chosen from the set of alpha-numeric
+     * characters as indicated by the arguments.</p>
+     *
+     * @param count  the length of random string to create
+     * @param letters  if <code>true</code>, generated string will include
+     *  alphabetic characters
+     * @param numbers  if <code>true</code>, generated string will include
+     *  numeric characters
+     * @return the random string
+     */
+    public static String random(int count, boolean letters, boolean numbers) {
+        return random(count, 0, 0, letters, numbers);
+    }
+    
+    /**
+     * <p>Creates a random string whose length is the number of characters
+     * specified.</p>
+     *
+     * <p>Characters will be chosen from the set of alpha-numeric
+     * characters as indicated by the arguments.</p>
+     *
+     * @param count  the length of random string to create
+     * @param start  the position in set of chars to start at
+     * @param end  the position in set of chars to end before
+     * @param letters  if <code>true</code>, generated string will include
+     *  alphabetic characters
+     * @param numbers  if <code>true</code>, generated string will include
+     *  numeric characters
+     * @return the random string
+     */
+    public static String random(int count, int start, int end, boolean letters, boolean numbers) {
+        return random(count, start, end, letters, numbers, null, RANDOM);
+    }
+
+    /**
+     * <p>Creates a random string based on a variety of options, using
+     * default source of randomness.</p>
+     *
+     * <p>This method has exactly the same semantics as
+     * {@link #random(int,int,int,boolean,boolean,char[],Random)}, but
+     * instead of using an externally supplied source of randomness, it uses
+     * the internal static {@link Random} instance.</p>
+     *
+     * @param count  the length of random string to create
+     * @param start  the position in set of chars to start at
+     * @param end  the position in set of chars to end before
+     * @param letters  only allow letters?
+     * @param numbers  only allow numbers?
+     * @param chars  the set of chars to choose randoms from.
+     *  If <code>null</code>, then it will use the set of all chars.
+     * @return the random string
+     * @throws ArrayIndexOutOfBoundsException if there are not
+     *  <code>(end - start) + 1</code> characters in the set array.
+     */
+    public static String random(int count, int start, int end, boolean letters, boolean numbers, char[] chars) {
+        return random(count, start, end, letters, numbers, chars, RANDOM);
+    }
+
+    /**
+     * <p>Creates a random string based on a variety of options, using
+     * supplied source of randomness.</p>
+     *
+     * <p>If start and end are both <code>0</code>, start and end are set
+     * to <code>' '</code> and <code>'z'</code>, the ASCII printable
+     * characters, will be used, unless letters and numbers are both
+     * <code>false</code>, in which case, start and end are set to
+     * <code>0</code> and <code>Integer.MAX_VALUE</code>.
+     *
+     * <p>If set is not <code>null</code>, characters between start and
+     * end are chosen.</p>
+     *
+     * <p>This method accepts a user-supplied {@link Random}
+     * instance to use as a source of randomness. By seeding a single 
+     * {@link Random} instance with a fixed seed and using it for each call,
+     * the same random sequence of strings can be generated repeatedly
+     * and predictably.</p>
+     *
+     * @param count  the length of random string to create
+     * @param start  the position in set of chars to start at
+     * @param end  the position in set of chars to end before
+     * @param letters  only allow letters?
+     * @param numbers  only allow numbers?
+     * @param chars  the set of chars to choose randoms from.
+     *  If <code>null</code>, then it will use the set of all chars.
+     * @param random  a source of randomness.
+     * @return the random string
+     * @throws ArrayIndexOutOfBoundsException if there are not
+     *  <code>(end - start) + 1</code> characters in the set array.
+     * @throws IllegalArgumentException if <code>count</code> &lt; 0.
+     * @since 2.0
+     */
+    public static String random(int count, int start, int end, boolean letters, boolean numbers,
+                                char[] chars, Random random) {
+        if (count == 0) {
+            return "";
+        } else if (count < 0) {
+            throw new IllegalArgumentException("Requested random string length " + count + " is less than 0.");
+        }
+        if ((start == 0) && (end == 0)) {
+            end = 'z' + 1;
+            start = ' ';
+            if (!letters && !numbers) {
+                start = 0;
+                end = Integer.MAX_VALUE;
+            }
+        }
+
+        StringBuffer buffer = new StringBuffer();
+        int gap = end - start;
+
+        while (count-- != 0) {
+            char ch;
+            if (chars == null) {
+                ch = (char) (random.nextInt(gap) + start);
+            } else {
+                ch = chars[random.nextInt(gap) + start];
+            }
+            if ((letters && numbers && Character.isLetterOrDigit(ch))
+                || (letters && Character.isLetter(ch))
+                || (numbers && Character.isDigit(ch))
+                || (!letters && !numbers)) {
+                buffer.append(ch);
+            } else {
+                count++;
+            }
+        }
+        return buffer.toString();
+    }
+
+    /**
+     * <p>Creates a random string whose length is the number of characters
+     * specified.</p>
+     *
+     * <p>Characters will be chosen from the set of characters
+     * specified.</p>
+     *
+     * @param count  the length of random string to create
+     * @param chars  the String containing the set of characters to use,
+     *  may be null
+     * @return the random string
+     * @throws IllegalArgumentException if <code>count</code> &lt; 0.
+     */
+    public static String random(int count, String chars) {
+        if (chars == null) {
+            return random(count, 0, 0, false, false, null, RANDOM);
+        }
+        return random(count, chars.toCharArray());
+    }
+
+    /**
+     * <p>Creates a random string whose length is the number of characters
+     * specified.</p>
+     *
+     * <p>Characters will be chosen from the set of characters specified.</p>
+     *
+     * @param count  the length of random string to create
+     * @param chars  the character array containing the set of characters to use,
+     *  may be null
+     * @return the random string
+     * @throws IllegalArgumentException if <code>count</code> &lt; 0.
+     */
+    public static String random(int count, char[] chars) {
+        if (chars == null) {
+            return random(count, 0, 0, false, false, null, RANDOM);
+        }
+        return random(count, 0, chars.length, false, false, chars, RANDOM);
+    }
+    
+}

Propchange: directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/RandomStringUtils.java
------------------------------------------------------------------------------
    svn:keywords = HeadURL Id LastChangedBy LastChangedDate LastChangedRevision

Added: directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/SerializationException.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/SerializationException.java?rev=370807&view=auto
==============================================================================
--- directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/SerializationException.java (added)
+++ directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/SerializationException.java Fri Jan 20 05:47:50 2006
@@ -0,0 +1,72 @@
+/*
+ * Copyright 2002-2005 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.mina.common.support.lang;
+
+import org.apache.mina.common.support.lang.exception.NestableRuntimeException;
+
+/**
+ * <p>Exception thrown when the Serialization process fails.</p>
+ *
+ * <p>The original error is wrapped within this one.</p>
+ *
+ * @author Stephen Colebourne
+ * @since 1.0
+ * @version $Id$
+ */
+public class SerializationException extends NestableRuntimeException {
+
+    /**
+     * <p>Constructs a new <code>SerializationException</code> without specified
+     * detail message.</p>
+     */
+    public SerializationException() {
+        super();
+    }
+
+    /**
+     * <p>Constructs a new <code>SerializationException</code> with specified
+     * detail message.</p>
+     *
+     * @param msg  The error message.
+     */
+    public SerializationException(String msg) {
+        super(msg);
+    }
+
+    /**
+     * <p>Constructs a new <code>SerializationException</code> with specified
+     * nested <code>Throwable</code>.</p>
+     *
+     * @param cause  The <code>Exception</code> or <code>Error</code>
+     *  that caused this exception to be thrown.
+     */
+    public SerializationException(Throwable cause) {
+        super(cause);
+    }
+
+    /**
+     * <p>Constructs a new <code>SerializationException</code> with specified
+     * detail message and nested <code>Throwable</code>.</p>
+     *
+     * @param msg    The error message.
+     * @param cause  The <code>Exception</code> or <code>Error</code>
+     *  that caused this exception to be thrown.
+     */
+    public SerializationException(String msg, Throwable cause) {
+        super(msg, cause);
+    }
+
+}

Propchange: directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/SerializationException.java
------------------------------------------------------------------------------
    svn:keywords = HeadURL Id LastChangedBy LastChangedDate LastChangedRevision