directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From trus...@apache.org
Subject svn commit: r370807 [6/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...
Date Fri, 20 Jan 2006 13:48:55 GMT
Propchange: directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/ArrayUtils.java
------------------------------------------------------------------------------
    svn:keywords = HeadURL Id LastChangedBy LastChangedDate LastChangedRevision

Added: directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/BitField.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/BitField.java?rev=370807&view=auto
==============================================================================
--- directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/BitField.java (added)
+++ directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/BitField.java Fri Jan 20 05:47:50 2006
@@ -0,0 +1,289 @@
+/*
+ * 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>Operations on bit-mapped fields.</p>
+ *
+ * @author Apache Jakarta POI
+ * @author Scott Sanders (sanders at apache dot org)
+ * @author Marc Johnson (mjohnson at apache dot org)
+ * @author Andrew C. Oliver (acoliver at apache dot org)
+ * @author Stephen Colebourne
+ * @author Pete Gieser
+ * @author Gary Gregory
+ * @since 2.0
+ * @version $Id$
+ */
+public class BitField {
+    
+    private final int _mask;
+    private final int _shift_count;
+
+    /**
+     * <p>Creates a BitField instance.</p>
+     *
+     * @param mask the mask specifying which bits apply to this
+     *  BitField. Bits that are set in this mask are the bits
+     *  that this BitField operates on
+     */
+    public BitField(int mask) {
+        _mask = mask;
+        int count = 0;
+        int bit_pattern = mask;
+
+        if (bit_pattern != 0) {
+            while ((bit_pattern & 1) == 0) {
+                count++;
+                bit_pattern >>= 1;
+            }
+        }
+        _shift_count = count;
+    }
+
+    /**
+     * <p>Obtains the value for the specified BitField, appropriately
+     * shifted right.</p>
+     *
+     * <p>Many users of a BitField will want to treat the specified
+     * bits as an int value, and will not want to be aware that the
+     * value is stored as a BitField (and so shifted left so many
+     * bits).</p>
+     *
+     * @see #setValue(int,int)
+     * @param holder the int data containing the bits we're interested
+     *  in
+     * @return the selected bits, shifted right appropriately
+     */
+    public int getValue(int holder) {
+        return getRawValue(holder) >> _shift_count;
+    }
+
+    /**
+     * <p>Obtains the value for the specified BitField, appropriately
+     * shifted right, as a short.</p>
+     *
+     * <p>Many users of a BitField will want to treat the specified
+     * bits as an int value, and will not want to be aware that the
+     * value is stored as a BitField (and so shifted left so many
+     * bits).</p>
+     *
+     * @see #setShortValue(short,short)
+     * @param holder the short data containing the bits we're
+     *  interested in
+     * @return the selected bits, shifted right appropriately
+     */
+    public short getShortValue(short holder) {
+        return (short) getValue(holder);
+    }
+
+    /**
+     * <p>Obtains the value for the specified BitField, unshifted.</p>
+     *
+     * @param holder the int data containing the bits we're
+     *  interested in
+     * @return the selected bits
+     */
+    public int getRawValue(int holder) {
+        return holder & _mask;
+    }
+
+    /**
+     * <p>Obtains the value for the specified BitField, unshifted.</p>
+     *
+     * @param holder the short data containing the bits we're
+     *  interested in
+     * @return the selected bits
+     */
+    public short getShortRawValue(short holder) {
+        return (short) getRawValue(holder);
+    }
+
+    /**
+     * <p>Returns whether the field is set or not.</p>
+     *
+     * <p>This is most commonly used for a single-bit field, which is
+     * often used to represent a boolean value; the results of using
+     * it for a multi-bit field is to determine whether *any* of its
+     * bits are set.</p>
+     *
+     * @param holder the int data containing the bits we're interested
+     *  in
+     * @return <code>true</code> if any of the bits are set,
+     *  else <code>false</code>
+     */
+    public boolean isSet(int holder) {
+        return (holder & _mask) != 0;
+    }
+
+    /**
+     * <p>Returns whether all of the bits are set or not.</p>
+     *
+     * <p>This is a stricter test than {@link #isSet(int)},
+     * in that all of the bits in a multi-bit set must be set
+     * for this method to return <code>true</code>.</p>
+     *
+     * @param holder the int data containing the bits we're
+     *  interested in
+     * @return <code>true</code> if all of the bits are set,
+     *  else <code>false</code>
+     */
+    public boolean isAllSet(int holder) {
+        return (holder & _mask) == _mask;
+    }
+
+    /**
+     * <p>Replaces the bits with new values.</p>
+     *
+     * @see #getValue(int)
+     * @param holder the int data containing the bits we're
+     *  interested in
+     * @param value the new value for the specified bits
+     * @return the value of holder with the bits from the value
+     *  parameter replacing the old bits
+     */
+    public int setValue(int holder, int value) {
+        return (holder & ~_mask) | ((value << _shift_count) & _mask);
+    }
+
+    /**
+     * <p>Replaces the bits with new values.</p>
+     *
+     * @see #getShortValue(short)
+     * @param holder the short data containing the bits we're
+     *  interested in
+     * @param value the new value for the specified bits
+     * @return the value of holder with the bits from the value
+     *  parameter replacing the old bits
+     */
+    public short setShortValue(short holder, short value) {
+        return (short) setValue(holder, value);
+    }
+
+    /**
+     * <p>Clears the bits.</p>
+     *
+     * @param holder the int data containing the bits we're
+     *  interested in
+     * @return the value of holder with the specified bits cleared
+     *  (set to <code>0</code>)
+     */
+    public int clear(int holder) {
+        return holder & ~_mask;
+    }
+
+    /**
+     * <p>Clears the bits.</p>
+     *
+     * @param holder the short data containing the bits we're
+     *  interested in
+     * @return the value of holder with the specified bits cleared
+     *  (set to <code>0</code>)
+     */
+    public short clearShort(short holder) {
+        return (short) clear(holder);
+    }
+
+    /**
+     * <p>Clears the bits.</p>
+     *
+     * @param holder the byte data containing the bits we're
+     *  interested in
+     *
+     * @return the value of holder with the specified bits cleared
+     *  (set to <code>0</code>)
+     */
+    public byte clearByte(byte holder) {
+        return (byte) clear(holder);
+    }
+
+    /**
+     * <p>Sets the bits.</p>
+     *
+     * @param holder the int data containing the bits we're
+     *  interested in
+     * @return the value of holder with the specified bits set
+     *  to <code>1</code>
+     */
+    public int set(int holder) {
+        return holder | _mask;
+    }
+
+    /**
+     * <p>Sets the bits.</p>
+     *
+     * @param holder the short data containing the bits we're
+     *  interested in
+     * @return the value of holder with the specified bits set
+     *  to <code>1</code>
+     */
+    public short setShort(short holder) {
+        return (short) set(holder);
+    }
+
+    /**
+     * <p>Sets the bits.</p>
+     *
+     * @param holder the byte data containing the bits we're
+     *  interested in
+     *
+     * @return the value of holder with the specified bits set
+     *  to <code>1</code>
+     */
+    public byte setByte(byte holder) {
+        return (byte) set(holder);
+    }
+
+    /**
+     * <p>Sets a boolean BitField.</p>
+     *
+     * @param holder the int data containing the bits we're
+     *  interested in
+     * @param flag indicating whether to set or clear the bits
+     * @return the value of holder with the specified bits set or
+     *         cleared
+     */
+    public int setBoolean(int holder, boolean flag) {
+        return flag ? set(holder) : clear(holder);
+    }
+
+    /**
+     * <p>Sets a boolean BitField.</p>
+     *
+     * @param holder the short data containing the bits we're
+     *  interested in
+     * @param flag indicating whether to set or clear the bits
+     * @return the value of holder with the specified bits set or
+     *  cleared
+     */
+    public short setShortBoolean(short holder, boolean flag) {
+        return flag ? setShort(holder) : clearShort(holder);
+    }
+
+    /**
+     * <p>Sets a boolean BitField.</p>
+     *
+     * @param holder the byte data containing the bits we're
+     *  interested in
+     * @param flag indicating whether to set or clear the bits
+     * @return the value of holder with the specified bits set or
+     *  cleared
+     */
+    public byte setByteBoolean(byte holder, boolean flag) {
+        return flag ? setByte(holder) : clearByte(holder);
+    }
+
+}

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

Added: directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/BooleanUtils.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/BooleanUtils.java?rev=370807&view=auto
==============================================================================
--- directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/BooleanUtils.java (added)
+++ directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/BooleanUtils.java Fri Jan 20 05:47:50 2006
@@ -0,0 +1,928 @@
+/*
+ * 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.math.NumberUtils;
+
+/**
+ * <p>Operations on boolean primitives and Boolean objects.</p>
+ *
+ * <p>This class tries to handle <code>null</code> input gracefully.
+ * An exception will not be thrown for a <code>null</code> input.
+ * Each method documents its behaviour in more detail.</p>
+ * 
+ * @author Stephen Colebourne
+ * @author Matthew Hawthorne
+ * @author Gary Gregory
+ * @since 2.0
+ * @version $Id$
+ */
+public class BooleanUtils {
+
+    /**
+     * <p><code>BooleanUtils</code> instances should NOT be constructed in standard programming.
+     * Instead, the class should be used as <code>BooleanUtils.toBooleanObject(true);</code>.</p>
+     *
+     * <p>This constructor is public to permit tools that require a JavaBean instance
+     * to operate.</p>
+     */
+    public BooleanUtils() {
+    }
+
+    // Boolean utilities
+    //--------------------------------------------------------------------------
+    /**
+     * <p>Negates the specified boolean.</p>
+     * 
+     * <p>If <code>null</code> is passed in, <code>null</code> will be returned.</p>
+     *
+     * <pre>
+     *   BooleanUtils.negate(Boolean.TRUE)  = Boolean.FALSE;
+     *   BooleanUtils.negate(Boolean.FALSE) = Boolean.TRUE;
+     *   BooleanUtils.negate(null)          = null;
+     * </pre>
+     * 
+     * @param bool  the Boolean to negate, may be null
+     * @return the negated Boolean, or <code>null</code> if <code>null</code> input
+     */
+    public static Boolean negate(Boolean bool) {
+        if (bool == null) {
+            return null;
+        }
+        return (bool.booleanValue() ? Boolean.FALSE : Boolean.TRUE);
+    }
+    
+    // boolean Boolean methods
+    //-----------------------------------------------------------------------
+    /**
+     * <p>Is a Boolean value <code>true</code>, handling <code>null</code>.</p>
+     *
+     * <pre>
+     *   BooleanUtils.isTrue(Boolean.TRUE)  = true
+     *   BooleanUtils.isTrue(Boolean.FALSE) = false
+     *   BooleanUtils.isTrue(null)          = false
+     * </pre>
+     *
+     * @param bool  the boolean to convert
+     * @return <code>true</code> only if the input is non-null and true
+     * @since 2.1
+     */
+    public static boolean isTrue(Boolean bool) {
+        if (bool == null) {
+            return false;
+        }
+        return bool.booleanValue() ? true : false;
+    }
+
+    /**
+     * <p>Is a Boolean value <code>false</code>, handling <code>null</code>.</p>
+     *
+     * <pre>
+     *   BooleanUtils.isFalse(Boolean.TRUE)  = false
+     *   BooleanUtils.isFalse(Boolean.FALSE) = true
+     *   BooleanUtils.isFalse(null)          = false
+     * </pre>
+     *
+     * @param bool  the boolean to convert
+     * @return <code>true</code> only if the input is non-null and false
+     * @since 2.1
+     */
+    public static boolean isFalse(Boolean bool) {
+        if (bool == null) {
+            return false;
+        }
+        return bool.booleanValue() ? false : true;
+    }
+
+    /**
+     * <p>Boolean factory that avoids creating new Boolean objecs all the time.</p>
+     * 
+     * <p>This method was added to JDK1.4 but is available here for earlier JDKs.</p>
+     *
+     * <pre>
+     *   BooleanUtils.toBooleanObject(false) = Boolean.FALSE
+     *   BooleanUtils.toBooleanObject(true)  = Boolean.TRUE
+     * </pre>
+     *
+     * @param bool  the boolean to convert
+     * @return Boolean.TRUE or Boolean.FALSE as appropriate
+     */
+    public static Boolean toBooleanObject(boolean bool) {
+        return bool ? Boolean.TRUE : Boolean.FALSE;
+    }
+    
+    /**
+     * <p>Converts a Boolean to a boolean handling <code>null</code>
+     * by returning <code>false</code>.</p>
+     *
+     * <pre>
+     *   BooleanUtils.toBoolean(Boolean.TRUE)  = true
+     *   BooleanUtils.toBoolean(Boolean.FALSE) = false
+     *   BooleanUtils.toBoolean(null)          = false
+     * </pre>
+     *
+     * @param bool  the boolean to convert
+     * @return <code>true</code> or <code>false</code>, 
+     *  <code>null</code> returns <code>false</code>
+     */
+    public static boolean toBoolean(Boolean bool) {
+        if (bool == null) {
+            return false;
+        }
+        return bool.booleanValue() ? true : false;
+    }
+    
+    /**
+     * <p>Converts a Boolean to a boolean handling <code>null</code>.</p>
+     * 
+     * <pre>
+     *   BooleanUtils.toBooleanDefaultIfNull(Boolean.TRUE, false) = true
+     *   BooleanUtils.toBooleanDefaultIfNull(Boolean.FALSE, true) = false
+     *   BooleanUtils.toBooleanDefaultIfNull(null, true)          = true
+     * </pre>
+     *
+     * @param bool  the boolean to convert
+     * @param valueIfNull  the boolean value to return if <code>null</code>
+     * @return <code>true</code> or <code>false</code>
+     */
+    public static boolean toBooleanDefaultIfNull(Boolean bool, boolean valueIfNull) {
+        if (bool == null) {
+            return valueIfNull;
+        }
+        return bool.booleanValue() ? true : false;
+    }
+    
+    // Integer to Boolean methods
+    //-----------------------------------------------------------------------
+    /**
+     * <p>Converts an int to a boolean using the convention that <code>zero</code>
+     * is <code>false</code>.</p>
+     * 
+     * <pre>
+     *   BooleanUtils.toBoolean(0) = false
+     *   BooleanUtils.toBoolean(1) = true
+     *   BooleanUtils.toBoolean(2) = true
+     * </pre>
+     *
+     * @param value  the int to convert
+     * @return <code>true</code> if non-zero, <code>false</code>
+     *  if zero
+     */
+    public static boolean toBoolean(int value) {
+        return value == 0 ? false : true;
+    }
+    
+    /**
+     * <p>Converts an int to a Boolean using the convention that <code>zero</code>
+     * is <code>false</code>.</p>
+     * 
+     * <pre>
+     *   BooleanUtils.toBoolean(0) = Boolean.FALSE
+     *   BooleanUtils.toBoolean(1) = Boolean.TRUE
+     *   BooleanUtils.toBoolean(2) = Boolean.TRUE
+     * </pre>
+     *
+     * @param value  the int to convert
+     * @return Boolean.TRUE if non-zero, Boolean.FALSE if zero,
+     *  <code>null</code> if <code>null</code>
+     */
+    public static Boolean toBooleanObject(int value) {
+        return value == 0 ? Boolean.FALSE : Boolean.TRUE;
+    }
+    
+    /**
+     * <p>Converts an Integer to a Boolean using the convention that <code>zero</code>
+     * is <code>false</code>.</p>
+     * 
+     * <p><code>null</code> will be converted to <code>null</code>.</p>
+     *
+     * <pre>
+     *   BooleanUtils.toBoolean(new Integer(0))    = Boolean.FALSE
+     *   BooleanUtils.toBoolean(new Integer(1))    = Boolean.TRUE
+     *   BooleanUtils.toBoolean(new Integer(null)) = null
+     * </pre>
+     *
+     * @param value  the Integer to convert
+     * @return Boolean.TRUE if non-zero, Boolean.FALSE if zero,
+     *  <code>null</code> if <code>null</code> input
+     */
+    public static Boolean toBooleanObject(Integer value) {
+        if (value == null) {
+            return null;
+        }
+        return value.intValue() == 0 ? Boolean.FALSE : Boolean.TRUE;
+    }
+    
+    /**
+     * <p>Converts an int to a boolean specifying the conversion values.</p>
+     * 
+     * <pre>
+     *   BooleanUtils.toBoolean(0, 1, 0) = false
+     *   BooleanUtils.toBoolean(1, 1, 0) = true
+     *   BooleanUtils.toBoolean(2, 1, 2) = false
+     *   BooleanUtils.toBoolean(2, 2, 0) = true
+     * </pre>
+     *
+     * @param value  the Integer to convert
+     * @param trueValue  the value to match for <code>true</code>
+     * @param falseValue  the value to match for <code>false</code>
+     * @return <code>true</code> or <code>false</code>
+     * @throws IllegalArgumentException if no match
+     */
+    public static boolean toBoolean(int value, int trueValue, int falseValue) {
+        if (value == trueValue) {
+            return true;
+        } else if (value == falseValue) {
+            return false;
+        }
+        // no match
+        throw new IllegalArgumentException("The Integer did not match either specified value");
+    }
+    
+    /**
+     * <p>Converts an Integer to a boolean specifying the conversion values.</p>
+     * 
+     * <pre>
+     *   BooleanUtils.toBoolean(new Integer(0), new Integer(1), new Integer(0)) = false
+     *   BooleanUtils.toBoolean(new Integer(1), new Integer(1), new Integer(0)) = true
+     *   BooleanUtils.toBoolean(new Integer(2), new Integer(1), new Integer(2)) = false
+     *   BooleanUtils.toBoolean(new Integer(2), new Integer(2), new Integer(0)) = true
+     *   BooleanUtils.toBoolean(null, null, new Integer(0))                     = true
+     * </pre>
+     *
+     * @param value  the Integer to convert
+     * @param trueValue  the value to match for <code>true</code>,
+     *  may be <code>null</code>
+     * @param falseValue  the value to match for <code>false</code>,
+     *  may be <code>null</code>
+     * @return <code>true</code> or <code>false</code>
+     * @throws IllegalArgumentException if no match
+     */
+    public static boolean toBoolean(Integer value, Integer trueValue, Integer falseValue) {
+        if (value == null) {
+            if (trueValue == null) {
+                return true;
+            } else if (falseValue == null) {
+                return false;
+            }
+        } else if (value.equals(trueValue)) {
+            return true;
+        } else if (value.equals(falseValue)) {
+            return false;
+        }
+        // no match
+        throw new IllegalArgumentException("The Integer did not match either specified value");
+    }
+    
+    /**
+     * <p>Converts an int to a Boolean specifying the conversion values.</p>
+     * 
+     * <pre>
+     *   BooleanUtils.toBooleanObject(0, 0, 2, 3) = Boolean.TRUE
+     *   BooleanUtils.toBooleanObject(2, 1, 2, 3) = Boolean.FALSE
+     *   BooleanUtils.toBooleanObject(3, 1, 2, 3) = null
+     * </pre>
+     *
+     * @param value  the Integer to convert
+     * @param trueValue  the value to match for <code>true</code>
+     * @param falseValue  the value to match for <code>false</code>
+     * @param nullValue  the value to to match for <code>null</code>
+     * @return Boolean.TRUE, Boolean.FALSE, or <code>null</code>
+     * @throws IllegalArgumentException if no match
+     */
+    public static Boolean toBooleanObject(int value, int trueValue, int falseValue, int nullValue) {
+        if (value == trueValue) {
+            return Boolean.TRUE;
+        } else if (value == falseValue) {
+            return Boolean.FALSE;
+        } else if (value == nullValue) {
+            return null;
+        }
+        // no match
+        throw new IllegalArgumentException("The Integer did not match any specified value");
+    }
+    
+    /**
+     * <p>Converts an Integer to a Boolean specifying the conversion values.</p>
+     * 
+     * <pre>
+     *   BooleanUtils.toBooleanObject(new Integer(0), new Integer(0), new Integer(2), new Integer(3)) = Boolean.TRUE
+     *   BooleanUtils.toBooleanObject(new Integer(2), new Integer(1), new Integer(2), new Integer(3)) = Boolean.FALSE
+     *   BooleanUtils.toBooleanObject(new Integer(3), new Integer(1), new Integer(2), new Integer(3)) = null
+     * </pre>
+     *
+     * @param value  the Integer to convert
+     * @param trueValue  the value to match for <code>true</code>,
+     *  may be <code>null</code>
+     * @param falseValue  the value to match for <code>false</code>,
+     *  may be <code>null</code>
+     * @param nullValue  the value to to match for <code>null</code>,
+     *  may be <code>null</code>
+     * @return Boolean.TRUE, Boolean.FALSE, or <code>null</code>
+     * @throws IllegalArgumentException if no match
+     */
+    public static Boolean toBooleanObject(Integer value, Integer trueValue, Integer falseValue, Integer nullValue) {
+        if (value == null) {
+            if (trueValue == null) {
+                return Boolean.TRUE;
+            } else if (falseValue == null) {
+                return Boolean.FALSE;
+            } else if (nullValue == null) {
+                return null;
+            }
+        } else if (value.equals(trueValue)) {
+            return Boolean.TRUE;
+        } else if (value.equals(falseValue)) {
+            return Boolean.FALSE;
+        } else if (value.equals(nullValue)) {
+            return null;
+        }
+        // no match
+        throw new IllegalArgumentException("The Integer did not match any specified value");
+    }
+    
+    // Boolean to Integer methods
+    //-----------------------------------------------------------------------
+    /**
+     * <p>Converts a boolean to an int using the convention that
+     * <code>zero</code> is <code>false</code>.</p>
+     *
+     * <pre>
+     *   BooleanUtils.toInteger(true)  = 1
+     *   BooleanUtils.toInteger(false) = 0
+     * </pre>
+     *
+     * @param bool  the boolean to convert
+     * @return one if <code>true</code>, zero if <code>false</code>
+     */
+    public static int toInteger(boolean bool) {
+        return bool ? 1 : 0;
+    }
+    
+    /**
+     * <p>Converts a boolean to an Integer using the convention that
+     * <code>zero</code> is <code>false</code>.</p>
+     * 
+     * <pre>
+     *   BooleanUtils.toIntegerObject(true)  = new Integer(1)
+     *   BooleanUtils.toIntegerObject(false) = new Integer(0)
+     * </pre>
+     *
+     * @param bool  the boolean to convert
+     * @return one if <code>true</code>, zero if <code>false</code>
+     */
+    public static Integer toIntegerObject(boolean bool) {
+        return bool ? NumberUtils.INTEGER_ONE : NumberUtils.INTEGER_ZERO;
+    }
+    
+    /**
+     * <p>Converts a Boolean to a Integer using the convention that
+     * <code>zero</code> is <code>false</code>.</p>
+     *
+     * <p><code>null</code> will be converted to <code>null</code>.</p>
+     *
+     * <pre>
+     *   BooleanUtils.toIntegerObject(Boolean.TRUE)  = new Integer(1)
+     *   BooleanUtils.toIntegerObject(Boolean.FALSE) = new Integer(0)
+     * </pre>
+     *
+     * @param bool  the Boolean to convert
+     * @return one if Boolean.TRUE, zero if Boolean.FALSE, <code>null</code> if <code>null</code>
+     */
+    public static Integer toIntegerObject(Boolean bool) {
+        if (bool == null) {
+            return null;
+        }
+        return bool.booleanValue() ? NumberUtils.INTEGER_ONE : NumberUtils.INTEGER_ZERO;
+    }
+    
+    /**
+     * <p>Converts a boolean to an int specifying the conversion values.</p>
+     * 
+     * <pre>
+     *   BooleanUtils.toInteger(true, 1, 0)  = 1
+     *   BooleanUtils.toInteger(false, 1, 0) = 0
+     * </pre>
+     *
+     * @param bool  the to convert
+     * @param trueValue  the value to return if <code>true</code>
+     * @param falseValue  the value to return if <code>false</code>
+     * @return the appropriate value
+     */
+    public static int toInteger(boolean bool, int trueValue, int falseValue) {
+        return bool ? trueValue : falseValue;
+    }
+    
+    /**
+     * <p>Converts a Boolean to an int specifying the conversion values.</p>
+     * 
+     * <pre>
+     *   BooleanUtils.toInteger(Boolean.TRUE, 1, 0, 2)  = 1
+     *   BooleanUtils.toInteger(Boolean.FALSE, 1, 0, 2) = 0
+     *   BooleanUtils.toInteger(null, 1, 0, 2)          = 2
+     * </pre>
+     *
+     * @param bool  the Boolean to convert
+     * @param trueValue  the value to return if <code>true</code>
+     * @param falseValue  the value to return if <code>false</code>
+     * @param nullValue  the value to return if <code>null</code>
+     * @return the appropriate value
+     */
+    public static int toInteger(Boolean bool, int trueValue, int falseValue, int nullValue) {
+        if (bool == null) {
+            return nullValue;
+        }
+        return bool.booleanValue() ? trueValue : falseValue;
+    }
+    
+    /**
+     * <p>Converts a boolean to an Integer specifying the conversion values.</p>
+     * 
+     * <pre>
+     *   BooleanUtils.toIntegerObject(true, new Integer(1), new Integer(0))  = new Integer(1)
+     *   BooleanUtils.toIntegerObject(false, new Integer(1), new Integer(0)) = new Integer(0)
+     * </pre>
+     *
+     * @param bool  the to convert
+     * @param trueValue  the value to return if <code>true</code>,
+     *  may be <code>null</code>
+     * @param falseValue  the value to return if <code>false</code>,
+     *  may be <code>null</code>
+     * @return the appropriate value
+     */
+    public static Integer toIntegerObject(boolean bool, Integer trueValue, Integer falseValue) {
+        return bool ? trueValue : falseValue;
+    }
+    
+    /**
+     * <p>Converts a Boolean to an Integer specifying the conversion values.</p>
+     * 
+     * <pre>
+     *   BooleanUtils.toIntegerObject(Boolean.TRUE, new Integer(1), new Integer(0), new Integer(2))  = new Integer(1)
+     *   BooleanUtils.toIntegerObject(Boolean.FALSE, new Integer(1), new Integer(0), new Integer(2)) = new Integer(0)
+     *   BooleanUtils.toIntegerObject(null, new Integer(1), new Integer(0), new Integer(2))          = new Integer(2)
+     * </pre>
+     *
+     * @param bool  the Boolean to convert
+     * @param trueValue  the value to return if <code>true</code>,
+     *  may be <code>null</code>
+     * @param falseValue  the value to return if <code>false</code>,
+     *  may be <code>null</code>
+     * @param nullValue  the value to return if <code>null</code>,
+     *  may be <code>null</code>
+     * @return the appropriate value
+     */
+    public static Integer toIntegerObject(Boolean bool, Integer trueValue, Integer falseValue, Integer nullValue) {
+        if (bool == null) {
+            return nullValue;
+        }
+        return bool.booleanValue() ? trueValue : falseValue;
+    }
+    
+    // String to Boolean methods
+    //-----------------------------------------------------------------------
+    /**
+     * <p>Converts a String to a Boolean.</p>
+     * 
+     * <p><code>'true'</code>, <code>'on'</code> or <code>'yes'</code>
+     * (case insensitive) will return <code>true</code>.
+     * <code>'false'</code>, <code>'off'</code> or <code>'no'</code>
+     * (case insensitive) will return <code>false</code>.
+     * Otherwise, <code>null</code> is returned.</p>
+     *
+     * <pre>
+     *   BooleanUtils.toBooleanObject(null)    = null
+     *   BooleanUtils.toBooleanObject("true")  = Boolean.TRUE
+     *   BooleanUtils.toBooleanObject("false") = Boolean.FALSE
+     *   BooleanUtils.toBooleanObject("on")    = Boolean.TRUE
+     *   BooleanUtils.toBooleanObject("ON")    = Boolean.TRUE
+     *   BooleanUtils.toBooleanObject("off")   = Boolean.FALSE
+     *   BooleanUtils.toBooleanObject("oFf")   = Boolean.FALSE
+     *   BooleanUtils.toBooleanObject("blue")  = null
+     * </pre>
+     *
+     * @param str  the String to check
+     * @return the Boolean value of the string,
+     *  <code>null</code> if no match or <code>null</code> input
+     */
+    public static Boolean toBooleanObject(String str) {
+        if ("true".equalsIgnoreCase(str)) {
+            return Boolean.TRUE;
+        } else if ("false".equalsIgnoreCase(str)) {
+            return Boolean.FALSE;
+        } else if ("on".equalsIgnoreCase(str)) {
+            return Boolean.TRUE;
+        } else if ("off".equalsIgnoreCase(str)) {
+            return Boolean.FALSE;
+        } else if ("yes".equalsIgnoreCase(str)) {
+            return Boolean.TRUE;
+        } else if ("no".equalsIgnoreCase(str)) {
+            return Boolean.FALSE;
+        }
+        // no match
+        return null;
+    }
+
+    /**
+     * <p>Converts a String to a Boolean throwing an exception if no match.</p>
+     *
+     * <pre>
+     *   BooleanUtils.toBooleanObject("true", "true", "false", "null")  = Boolean.TRUE
+     *   BooleanUtils.toBooleanObject("false", "true", "false", "null") = Boolean.FALSE
+     *   BooleanUtils.toBooleanObject("null", "true", "false", "null")  = null
+     * </pre>
+     *
+     * @param str  the String to check
+     * @param trueString  the String to match for <code>true</code>
+     *  (case sensitive), may be <code>null</code>
+     * @param falseString  the String to match for <code>false</code>
+     *  (case sensitive), may be <code>null</code>
+     * @param nullString  the String to match for <code>null</code>
+     *  (case sensitive), may be <code>null</code>
+     * @return the Boolean value of the string,
+     *  <code>null</code> if no match or <code>null</code> input
+     */
+    public static Boolean toBooleanObject(String str, String trueString, String falseString, String nullString) {
+        if (str == null) {
+            if (trueString == null) {
+                return Boolean.TRUE;
+            } else if (falseString == null) {
+                return Boolean.FALSE;
+            } else if (nullString == null) {
+                return null;
+            }
+        } else if (str.equals(trueString)) {
+            return Boolean.TRUE;
+        } else if (str.equals(falseString)) {
+            return Boolean.FALSE;
+        } else if (str.equals(nullString)) {
+            return null;
+        }
+        // no match
+        throw new IllegalArgumentException("The String did not match any specified value");
+    }
+
+    // String to boolean methods
+    //-----------------------------------------------------------------------
+    /**
+     * <p>Converts a String to a boolean (optimised for performance).</p>
+     * 
+     * <p><code>'true'</code>, <code>'on'</code> or <code>'yes'</code>
+     * (case insensitive) will return <code>true</code>. Otherwise,
+     * <code>false</code> is returned.</p>
+     * 
+     * <p>This method performs 4 times faster (JDK1.4) than
+     * <code>Boolean.valueOf(String)</code>. However, this method accepts
+     * 'on' and 'yes' as true values.
+     *
+     * <pre>
+     *   BooleanUtils.toBoolean(null)    = false
+     *   BooleanUtils.toBoolean("true")  = true
+     *   BooleanUtils.toBoolean("TRUE")  = true
+     *   BooleanUtils.toBoolean("tRUe")  = true
+     *   BooleanUtils.toBoolean("on")    = true
+     *   BooleanUtils.toBoolean("yes")   = true
+     *   BooleanUtils.toBoolean("false") = false
+     *   BooleanUtils.toBoolean("x gti") = false
+     * </pre>
+     *
+     * @param str  the String to check
+     * @return the boolean value of the string, <code>false</code> if no match
+     */
+    public static boolean toBoolean(String str) {
+        // Previously used equalsIgnoreCase, which was fast for interned 'true'.
+        // Non interned 'true' matched 15 times slower.
+        // 
+        // Optimisation provides same performance as before for interned 'true'.
+        // Similar performance for null, 'false', and other strings not length 2/3/4.
+        // 'true'/'TRUE' match 4 times slower, 'tRUE'/'True' 7 times slower.
+        if (str == "true") {
+            return true;
+        }
+        if (str == null) {
+            return false;
+        }
+        switch (str.length()) {
+            case 2: {
+                char ch0 = str.charAt(0);
+                char ch1 = str.charAt(1);
+                return 
+                    (ch0 == 'o' || ch0 == 'O') &&
+                    (ch1 == 'n' || ch1 == 'N');
+            }
+            case 3: {
+                char ch = str.charAt(0);
+                if (ch == 'y') {
+                    return 
+                        (str.charAt(1) == 'e' || str.charAt(1) == 'E') &&
+                        (str.charAt(2) == 's' || str.charAt(2) == 'S');
+                }
+                if (ch == 'Y') {
+                    return 
+                        (str.charAt(1) == 'E' || str.charAt(1) == 'e') &&
+                        (str.charAt(2) == 'S' || str.charAt(2) == 's');
+                }
+            }
+            case 4: {
+                char ch = str.charAt(0);
+                if (ch == 't') {
+                    return 
+                        (str.charAt(1) == 'r' || str.charAt(1) == 'R') &&
+                        (str.charAt(2) == 'u' || str.charAt(2) == 'U') &&
+                        (str.charAt(3) == 'e' || str.charAt(3) == 'E');
+                }
+                if (ch == 'T') {
+                    return 
+                        (str.charAt(1) == 'R' || str.charAt(1) == 'r') &&
+                        (str.charAt(2) == 'U' || str.charAt(2) == 'u') &&
+                        (str.charAt(3) == 'E' || str.charAt(3) == 'e');
+                }
+            }
+        }
+        return false;
+    }
+    
+//    public static void main(String[] args) {
+//        long start = System.currentTimeMillis();
+//        boolean flag = true;
+//        int count = 0;
+//        for (int i = 0; i < 100000000; i++) {
+//            flag = toBoolean("YES");
+//        }
+//        long end = System.currentTimeMillis();
+//        System.out.println((end - start) + " " + flag + " " + count);
+//    }
+    
+    /**
+     * <p>Converts a String to a Boolean throwing an exception if no match found.</p>
+     * 
+     * <p>null is returned if there is no match.</p>
+     *
+     * <pre>
+     *   BooleanUtils.toBoolean("true", "true", "false")  = true
+     *   BooleanUtils.toBoolean("false", "true", "false") = false
+     * </pre>
+     *
+     * @param str  the String to check
+     * @param trueString  the String to match for <code>true</code>
+     *  (case sensitive), may be <code>null</code>
+     * @param falseString  the String to match for <code>false</code>
+     *  (case sensitive), may be <code>null</code>
+     * @return the boolean value of the string
+     * @throws IllegalArgumentException if the String doesn't match
+     */
+    public static boolean toBoolean(String str, String trueString, String falseString) {
+        if (str == null) {
+            if (trueString == null) {
+                return true;
+            } else if (falseString == null) {
+                return false;
+            }
+        } else if (str.equals(trueString)) {
+            return true;
+        } else if (str.equals(falseString)) {
+            return false;
+        }
+        // no match
+        throw new IllegalArgumentException("The String did not match either specified value");
+    }
+
+    // Boolean to String methods
+    //-----------------------------------------------------------------------
+    /**
+     * <p>Converts a Boolean to a String returning <code>'true'</code>,
+     * <code>'false'</code>, or <code>null</code>.</p>
+     * 
+     * <pre>
+     *   BooleanUtils.toStringTrueFalse(Boolean.TRUE)  = "true"
+     *   BooleanUtils.toStringTrueFalse(Boolean.FALSE) = "false"
+     *   BooleanUtils.toStringTrueFalse(null)          = null;
+     * </pre>
+     *
+     * @param bool  the Boolean to check
+     * @return <code>'true'</code>, <code>'false'</code>,
+     *  or <code>null</code>
+     */
+    public static String toStringTrueFalse(Boolean bool) {
+        return toString(bool, "true", "false", null);
+    }
+    
+    /**
+     * <p>Converts a Boolean to a String returning <code>'on'</code>,
+     * <code>'off'</code>, or <code>null</code>.</p>
+     * 
+     * <pre>
+     *   BooleanUtils.toStringOnOff(Boolean.TRUE)  = "on"
+     *   BooleanUtils.toStringOnOff(Boolean.FALSE) = "off"
+     *   BooleanUtils.toStringOnOff(null)          = null;
+     * </pre>
+     *
+     * @param bool  the Boolean to check
+     * @return <code>'on'</code>, <code>'off'</code>,
+     *  or <code>null</code>
+     */
+    public static String toStringOnOff(Boolean bool) {
+        return toString(bool, "on", "off", null);
+    }
+    
+    /**
+     * <p>Converts a Boolean to a String returning <code>'yes'</code>,
+     * <code>'no'</code>, or <code>null</code>.</p>
+     * 
+     * <pre>
+     *   BooleanUtils.toStringYesNo(Boolean.TRUE)  = "yes"
+     *   BooleanUtils.toStringYesNo(Boolean.FALSE) = "no"
+     *   BooleanUtils.toStringYesNo(null)          = null;
+     * </pre>
+     *
+     * @param bool  the Boolean to check
+     * @return <code>'yes'</code>, <code>'no'</code>,
+     *  or <code>null</code>
+     */
+    public static String toStringYesNo(Boolean bool) {
+        return toString(bool, "yes", "no", null);
+    }
+    
+    /**
+     * <p>Converts a Boolean to a String returning one of the input Strings.</p>
+     * 
+     * <pre>
+     *   BooleanUtils.toString(Boolean.TRUE, "true", "false", null)   = "true"
+     *   BooleanUtils.toString(Boolean.FALSE, "true", "false", null)  = "false"
+     *   BooleanUtils.toString(null, "true", "false", null)           = null;
+     * </pre>
+     *
+     * @param bool  the Boolean to check
+     * @param trueString  the String to return if <code>true</code>,
+     *  may be <code>null</code>
+     * @param falseString  the String to return if <code>false</code>,
+     *  may be <code>null</code>
+     * @param nullString  the String to return if <code>null</code>,
+     *  may be <code>null</code>
+     * @return one of the three input Strings
+     */
+    public static String toString(Boolean bool, String trueString, String falseString, String nullString) {
+        if (bool == null) {
+            return nullString;
+        }
+        return bool.booleanValue() ? trueString : falseString;
+    }
+    
+    // boolean to String methods
+    //-----------------------------------------------------------------------
+    /**
+     * <p>Converts a boolean to a String returning <code>'true'</code>
+     * or <code>'false'</code>.</p>
+     * 
+     * <pre>
+     *   BooleanUtils.toStringTrueFalse(true)   = "true"
+     *   BooleanUtils.toStringTrueFalse(false)  = "false"
+     * </pre>
+     *
+     * @param bool  the Boolean to check
+     * @return <code>'true'</code>, <code>'false'</code>,
+     *  or <code>null</code>
+     */
+    public static String toStringTrueFalse(boolean bool) {
+        return toString(bool, "true", "false");
+    }
+    
+    /**
+     * <p>Converts a boolean to a String returning <code>'on'</code>
+     * or <code>'off'</code>.</p>
+     * 
+     * <pre>
+     *   BooleanUtils.toStringOnOff(true)   = "on"
+     *   BooleanUtils.toStringOnOff(false)  = "off"
+     * </pre>
+     *
+     * @param bool  the Boolean to check
+     * @return <code>'on'</code>, <code>'off'</code>,
+     *  or <code>null</code>
+     */
+    public static String toStringOnOff(boolean bool) {
+        return toString(bool, "on", "off");
+    }
+    
+    /**
+     * <p>Converts a boolean to a String returning <code>'yes'</code>
+     * or <code>'no'</code>.</p>
+     * 
+     * <pre>
+     *   BooleanUtils.toStringYesNo(true)   = "yes"
+     *   BooleanUtils.toStringYesNo(false)  = "no"
+     * </pre>
+     *
+     * @param bool  the Boolean to check
+     * @return <code>'yes'</code>, <code>'no'</code>,
+     *  or <code>null</code>
+     */
+    public static String toStringYesNo(boolean bool) {
+        return toString(bool, "yes", "no");
+    }
+    
+    /**
+     * <p>Converts a boolean to a String returning one of the input Strings.</p>
+     * 
+     * <pre>
+     *   BooleanUtils.toString(true, "true", "false")   = "true"
+     *   BooleanUtils.toString(false, "true", "false")  = "false"
+     * </pre>
+     *
+     * @param bool  the Boolean to check
+     * @param trueString  the String to return if <code>true</code>,
+     *  may be <code>null</code>
+     * @param falseString  the String to return if <code>false</code>,
+     *  may be <code>null</code>
+     * @return one of the two input Strings
+     */
+    public static String toString(boolean bool, String trueString, String falseString) {
+        return bool ? trueString : falseString;
+    }
+    
+    // xor methods
+    // ----------------------------------------------------------------------
+    /**
+     * <p>Performs an xor on a set of booleans.</p>
+     *
+     * <pre>
+     *   BooleanUtils.xor(new boolean[] { true, true })   = false
+     *   BooleanUtils.xor(new boolean[] { false, false }) = false
+     *   BooleanUtils.xor(new boolean[] { true, false })  = true
+     * </pre>
+     *
+     * @param array  an array of <code>boolean<code>s
+     * @return <code>true</code> if the xor is successful.
+     * @throws IllegalArgumentException if <code>array</code> is <code>null</code>
+     * @throws IllegalArgumentException if <code>array</code> is empty.
+     */
+    public static boolean xor(boolean[] array) {
+        // Validates input
+        if (array == null) {
+            throw new IllegalArgumentException("The Array must not be null");
+        } else if (array.length == 0) {
+            throw new IllegalArgumentException("Array is empty");
+        }
+
+        // Loops through array, comparing each item
+        int trueCount = 0;
+        for (int i = 0; i < array.length; i++) {
+            // If item is true, and trueCount is < 1, increments count
+            // Else, xor fails
+            if (array[i]) {
+                if (trueCount < 1) {
+                    trueCount++;
+                } else {
+                    return false;
+                }
+            }
+        }
+
+        // Returns true if there was exactly 1 true item
+        return trueCount == 1;
+    }
+
+    /**
+     * <p>Performs an xor on an array of Booleans.</p>
+     * 
+     * <pre>
+     *   BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.TRUE })   = Boolean.FALSE
+     *   BooleanUtils.xor(new Boolean[] { Boolean.FALSE, Boolean.FALSE }) = Boolean.FALSE
+     *   BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.FALSE })  = Boolean.TRUE
+     * </pre>
+     *
+     * @param array  an array of <code>Boolean<code>s
+     * @return <code>true</code> if the xor is successful.
+     * @throws IllegalArgumentException if <code>array</code> is <code>null</code>
+     * @throws IllegalArgumentException if <code>array</code> is empty.
+     * @throws IllegalArgumentException if <code>array</code> contains a <code>null</code>
+     */
+    public static Boolean xor(Boolean[] array) {
+        if (array == null) {
+            throw new IllegalArgumentException("The Array must not be null");
+        } else if (array.length == 0) {
+            throw new IllegalArgumentException("Array is empty");
+        }
+        boolean[] primitive = null;
+        try {
+            primitive = ArrayUtils.toPrimitive(array);
+        } catch (NullPointerException ex) {
+            throw new IllegalArgumentException("The array must not contain any null elements");
+        }
+        return xor(primitive) ? Boolean.TRUE : Boolean.FALSE;
+    }
+
+}

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

Added: directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/CharEncoding.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/CharEncoding.java?rev=370807&view=auto
==============================================================================
--- directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/CharEncoding.java (added)
+++ directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/CharEncoding.java Fri Jan 20 05:47:50 2006
@@ -0,0 +1,152 @@
+/*
+ * Copyright 2001-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.UnsupportedEncodingException;
+
+/**
+ * <p>
+ * Character encoding names required of every implementation of the Java platform.
+ * </p>
+ * 
+ * <p>
+ * According to <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character
+ * encoding names</a>:
+ * <p>
+ * <cite>Every implementation of the Java platform is required to support the following character encodings. Consult the
+ * release documentation for your implementation to see if any other encodings are supported.</cite>
+ * </p>
+ * </p>
+ * 
+ * @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character encoding
+ *      names</a>
+ * @author Apache Software Foundation
+ * @since 2.1
+ * @version $Id$
+ */
+public class CharEncoding {
+
+    /**
+     * <p>
+     * ISO Latin Alphabet #1, also known as ISO-LATIN-1.
+     * </p>
+     * <p>
+     * Every implementation of the Java platform is required to support this character encoding.
+     * </p>
+     * 
+     * @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character
+     *      encoding names</a>
+     */
+    public static final String ISO_8859_1 = "ISO-8859-1";
+
+    /**
+     * <p>
+     * Seven-bit ASCII, also known as ISO646-US, also known as the Basic Latin block of the Unicode character set.
+     * </p>
+     * <p>
+     * Every implementation of the Java platform is required to support this character encoding.
+     * </p>
+     * 
+     * @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character
+     *      encoding names</a>
+     */
+    public static final String US_ASCII = "US-ASCII";
+
+    /**
+     * <p>
+     * Sixteen-bit Unicode Transformation Format, byte order specified by a mandatory initial byte-order mark (either
+     * order accepted on input, big-endian used on output).
+     * </p>
+     * <p>
+     * Every implementation of the Java platform is required to support this character encoding.
+     * </p>
+     * 
+     * @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character
+     *      encoding names</a>
+     */
+    public static final String UTF_16 = "UTF-16";
+
+    /**
+     * <p>
+     * Sixteen-bit Unicode Transformation Format, big-endian byte order.
+     * </p>
+     * <p>
+     * Every implementation of the Java platform is required to support this character encoding.
+     * </p>
+     * 
+     * @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character
+     *      encoding names</a>
+     */
+    public static final String UTF_16BE = "UTF-16BE";
+
+    /**
+     * <p>
+     * Sixteen-bit Unicode Transformation Format, little-endian byte order.
+     * </p>
+     * <p>
+     * Every implementation of the Java platform is required to support this character encoding.
+     * </p>
+     * 
+     * @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character
+     *      encoding names</a>
+     */
+    public static final String UTF_16LE = "UTF-16LE";
+
+    /**
+     * <p>
+     * Eight-bit Unicode Transformation Format.
+     * </p>
+     * <p>
+     * Every implementation of the Java platform is required to support this character encoding.
+     * </p>
+     * 
+     * @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character
+     *      encoding names</a>
+     */
+    public static final String UTF_8 = "UTF-8";
+
+    /**
+     * <p>
+     * Returns whether the named charset is supported.
+     * </p>
+     * <p>
+     * This is similar to <a
+     * href="http://java.sun.com/j2se/1.4.2/docs/api/java/nio/charset/Charset.html#isSupported(java.lang.String)">
+     * java.nio.charset.Charset.isSupported(String)</a>
+     * </p>
+     * 
+     * @param name
+     *            the name of the requested charset; may be either a canonical name or an alias
+     * @return <code>true</code> if, and only if, support for the named charset is available in the current Java
+     *         virtual machine
+     * 
+     * @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character
+     *      encoding names</a>
+     */
+    public static boolean isSupported(String name) {
+        if (name == null) {
+            return false;
+        }
+        try {
+            new String(ArrayUtils.EMPTY_BYTE_ARRAY, name);
+        } catch (UnsupportedEncodingException e) {
+            return false;
+        }
+        return true;
+    }
+
+}

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

Added: directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/CharRange.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/CharRange.java?rev=370807&view=auto
==============================================================================
--- directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/CharRange.java (added)
+++ directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/CharRange.java Fri Jan 20 05:47:50 2006
@@ -0,0 +1,228 @@
+/*
+ * 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>A contiguous range of characters, optionally negated.</p>
+ * 
+ * <p>Instances are immutable.</p>
+ *
+ * @author Henri Yandell
+ * @author Stephen Colebourne
+ * @author Chris Feldhacker
+ * @author Gary Gregory
+ * @since 1.0
+ * @version $Id$
+ */
+public final class CharRange implements Serializable {
+
+    /** Serialization lock, Lang version 2.0. */
+    private static final long serialVersionUID = 8270183163158333422L;
+    
+    /** The first character, inclusive, in the range. */
+    private final char start;
+    /** The last character, inclusive, in the range. */
+    private final char end;
+    /** True if the range is everything except the characters specified. */
+    private final boolean negated;
+    
+    /** Cached toString. */
+    private transient String iToString;
+
+    //-----------------------------------------------------------------------
+    /**
+     * <p>Constructs a <code>CharRange</code> over a single character.</p>
+     *
+     * @param ch  only character in this range
+     */
+    public CharRange(char ch) {
+        this(ch, ch, false);
+    }
+
+    /**
+     * <p>Constructs a <code>CharRange</code> over a single character,
+     * optionally negating the range.</p>
+     *
+     * <p>A negated range includes everything except the specified char.</p>
+     *
+     * @param ch  only character in this range
+     * @param negated  true to express everything except the range
+     */
+    public CharRange(char ch, boolean negated) {
+        this(ch, ch, negated);
+    }
+
+    /**
+     * <p>Constructs a <code>CharRange</code> over a set of characters.</p>
+     *
+     * @param start  first character, inclusive, in this range
+     * @param end  last character, inclusive, in this range
+     */
+    public CharRange(char start, char end) {
+        this(start, end, false);
+    }
+
+    /**
+     * <p>Constructs a <code>CharRange</code> over a set of characters,
+     * optionally negating the range.</p>
+     *
+     * <p>A negated range includes everything except that defined by the
+     * start and end characters.</p>
+     * 
+     * <p>If start and end are in the wrong order, they are reversed.
+     * Thus <code>a-e</code> is the same as <code>e-a</code>.</p>
+     *
+     * @param start  first character, inclusive, in this range
+     * @param end  last character, inclusive, in this range
+     * @param negated  true to express everything except the range
+     */
+    public CharRange(char start, char end, boolean negated) {
+        super();
+        if (start > end) {
+            char temp = start;
+            start = end;
+            end = temp;
+        }
+        
+        this.start = start;
+        this.end = end;
+        this.negated = negated;
+    }
+
+    // Accessors
+    //-----------------------------------------------------------------------
+    /**
+     * <p>Gets the start character for this character range.</p>
+     * 
+     * @return the start char (inclusive)
+     */
+    public char getStart() {
+        return this.start;
+    }
+
+    /**
+     * <p>Gets the end character for this character range.</p>
+     * 
+     * @return the end char (inclusive)
+     */
+    public char getEnd() {
+        return this.end;
+    }
+
+    /**
+     * <p>Is this <code>CharRange</code> negated.</p>
+     * 
+     * <p>A negated range includes everything except that defined by the
+     * start and end characters.</p>
+     *
+     * @return <code>true</code> is negated
+     */
+    public boolean isNegated() {
+        return negated;
+    }
+
+    // Contains
+    //-----------------------------------------------------------------------
+    /**
+     * <p>Is the character specified contained in this range.</p>
+     *
+     * @param ch  the character to check
+     * @return <code>true</code> if this range contains the input character
+     */
+    public boolean contains(char ch) {
+        return (ch >= start && ch <= end) != negated;
+    }
+
+    /**
+     * <p>Are all the characters of the passed in range contained in
+     * this range.</p>
+     *
+     * @param range  the range to check against
+     * @return <code>true</code> if this range entirely contains the input range
+     * @throws IllegalArgumentException if <code>null</code> input
+     */
+    public boolean contains(CharRange range) {
+        if (range == null) {
+            throw new IllegalArgumentException("The Range must not be null");
+        }
+        if (negated) {
+            if (range.negated) {
+                return start >= range.start && end <= range.end;
+            } else {
+                return range.end < start || range.start > end;
+            }
+        } else {
+            if (range.negated) {
+                return start == 0 && end == Character.MAX_VALUE;
+            } else {
+                return start <= range.start && end >= range.end;
+            }
+        }
+    }
+
+    // Basics
+    //-----------------------------------------------------------------------
+    /**
+     * <p>Compares two CharRange objects, returning true if they represent
+     * exactly the same range of characters defined in the same way.</p>
+     * 
+     * @param obj  the object to compare to
+     * @return true if equal
+     */
+    public boolean equals(Object obj) {
+        if (obj == this) {
+            return true;
+        }
+        if (obj instanceof CharRange == false) {
+            return false;
+        }
+        CharRange other = (CharRange) obj;
+        return start == other.start && end == other.end && negated == other.negated;
+    }
+
+    /**
+     * <p>Gets a hashCode compatible with the equals method.</p>
+     * 
+     * @return a suitable hashCode
+     */
+    public int hashCode() {
+        return 83 + start + 7 * end + (negated ? 1 : 0);
+    }
+    
+    /**
+     * <p>Gets a string representation of the character range.</p>
+     * 
+     * @return string representation of this range
+     */
+    public String toString() {
+        if (iToString == null) {
+            StringBuffer buf = new StringBuffer(4);
+            if (isNegated()) {
+                buf.append('^');
+            }
+            buf.append(start);
+            if (start != end) {
+                buf.append('-');
+                buf.append(end);
+            }
+            iToString = buf.toString();
+        }
+        return iToString;
+    }
+    
+}

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

Added: directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/CharSet.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/CharSet.java?rev=370807&view=auto
==============================================================================
--- directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/CharSet.java (added)
+++ directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/CharSet.java Fri Jan 20 05:47:50 2006
@@ -0,0 +1,279 @@
+/*
+ * 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;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * <p>A set of characters.</p>
+ *
+ * <p>Instances are immutable, but instances of subclasses may not be.</p>
+ *
+ * @author Henri Yandell
+ * @author Stephen Colebourne
+ * @author Phil Steitz
+ * @author Pete Gieser
+ * @author Gary Gregory
+ * @since 1.0
+ * @version $Id$
+ */
+public class CharSet implements Serializable {
+
+    /** Serialization lock, Lang version 2.0. */
+    private static final long serialVersionUID = 5947847346149275958L;
+
+    /** 
+     * A CharSet defining no characters. 
+     * @since 2.0
+     */
+    public static final CharSet EMPTY = new CharSet((String) null);
+
+    /** 
+     * A CharSet defining ASCII alphabetic characters "a-zA-Z".
+     * @since 2.0
+     */
+    public static final CharSet ASCII_ALPHA = new CharSet("a-zA-Z");
+
+    /** 
+     * A CharSet defining ASCII alphabetic characters "a-z".
+     * @since 2.0
+     */
+    public static final CharSet ASCII_ALPHA_LOWER = new CharSet("a-z");
+
+    /** 
+     * A CharSet defining ASCII alphabetic characters "A-Z".
+     * @since 2.0
+     */
+    public static final CharSet ASCII_ALPHA_UPPER = new CharSet("A-Z");
+
+    /** 
+     * A CharSet defining ASCII alphabetic characters "0-9".
+     * @since 2.0
+     */
+    public static final CharSet ASCII_NUMERIC = new CharSet("0-9");
+
+    /**
+     * A Map of the common cases used in the factory.
+     * Subclasses can add more common patterns if desired.
+     * @since 2.0
+     */
+    protected static final Map COMMON = new HashMap();
+    
+    static {
+        COMMON.put(null, EMPTY);
+        COMMON.put("", EMPTY);
+        COMMON.put("a-zA-Z", ASCII_ALPHA);
+        COMMON.put("A-Za-z", ASCII_ALPHA);
+        COMMON.put("a-z", ASCII_ALPHA_LOWER);
+        COMMON.put("A-Z", ASCII_ALPHA_UPPER);
+        COMMON.put("0-9", ASCII_NUMERIC);
+    }
+
+    /** The set of CharRange objects. */
+    private Set set = new HashSet();
+
+    //-----------------------------------------------------------------------
+    /**
+     * <p>Factory method to create a new CharSet using a special syntax.</p>
+     *
+     * <ul>
+     *  <li><code>null</code> or empty string ("")
+     * - set containing no characters</li>
+     *  <li>Single character, such as "a"
+     *  - set containing just that character</li>
+     *  <li>Multi character, such as "a-e"
+     *  - set containing characters from one character to the other</li>
+     *  <li>Negated, such as "^a" or "^a-e"
+     *  - set containing all characters except those defined</li>
+     *  <li>Combinations, such as "abe-g"
+     *  - set containing all the characters from the individual sets</li>
+     * </ul>
+     *
+     * <p>The matching order is:</p>
+     * <ol>
+     *  <li>Negated multi character range, such as "^a-e"
+     *  <li>Ordinary multi character range, such as "a-e"
+     *  <li>Negated single character, such as "^a"
+     *  <li>Ordinary single character, such as "a"
+     * </ol>
+     * <p>Matching works left to right. Once a match is found the
+     * search starts again from the next character.</p>
+     *
+     * <p>If the same range is defined twice using the same syntax, only
+     * one range will be kept.
+     * Thus, "a-ca-c" creates only one range of "a-c".</p>
+     *
+     * <p>If the start and end of a range are in the wrong order,
+     * they are reversed. Thus "a-e" is the same as "e-a".
+     * As a result, "a-ee-a" would create only one range,
+     * as the "a-e" and "e-a" are the same.</p>
+     *
+     * <p>The set of characters represented is the union of the specified ranges.</p>
+     *
+     * <p>All CharSet objects returned by this method will be immutable.</p>
+     *
+     * @param setStr  the String describing the set, may be null
+     * @return a CharSet instance
+     * @since 2.0
+     */
+    public static CharSet getInstance(String setStr) {
+        Object set = COMMON.get(setStr);
+        if (set != null) {
+            return (CharSet) set;
+        }
+        return new CharSet(setStr);
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * <p>Constructs a new CharSet using the set syntax.</p>
+     *
+     * @param setStr  the String describing the set, may be null
+     * @since 2.0
+     */
+    protected CharSet(String setStr) {
+        super();
+        add(setStr);
+    }
+
+    /**
+     * <p>Constructs a new CharSet using the set syntax.
+     * Each string is merged in with the set.</p>
+     *
+     * @param set  Strings to merge into the initial set
+     * @throws NullPointerException if set is <code>null</code>
+     */
+    protected CharSet(String[] set) {
+        super();
+        int sz = set.length;
+        for (int i = 0; i < sz; i++) {
+            add(set[i]);
+        }
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * <p>Add a set definition string to the <code>CharSet</code>.</p>
+     *
+     * @param str  set definition string
+     */
+    protected void add(String str) {
+        if (str == null) {
+            return;
+        }
+
+        int len = str.length();
+        int pos = 0;
+        while (pos < len) {
+            int remainder = (len - pos);
+            if (remainder >= 4 && str.charAt(pos) == '^' && str.charAt(pos + 2) == '-') {
+                // negated range
+                set.add(new CharRange(str.charAt(pos + 1), str.charAt(pos + 3), true));
+                pos += 4;
+            } else if (remainder >= 3 && str.charAt(pos + 1) == '-') {
+                // range
+                set.add(new CharRange(str.charAt(pos), str.charAt(pos + 2)));
+                pos += 3;
+            } else if (remainder >= 2 && str.charAt(pos) == '^') {
+                // negated char
+                set.add(new CharRange(str.charAt(pos + 1), true));
+                pos += 2;
+            } else {
+                // char
+                set.add(new CharRange(str.charAt(pos)));
+                pos += 1;
+            }
+        }
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * <p>Gets the internal set as an array of CharRange objects.</p>
+     *
+     * @return an array of immutable CharRange objects
+     * @since 2.0
+     */
+    public CharRange[] getCharRanges() {
+        return (CharRange[]) set.toArray(new CharRange[set.size()]);
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * <p>Does the <code>CharSet</code> contain the specified
+     * character <code>ch</code>.</p>
+     *
+     * @param ch  the character to check for
+     * @return <code>true</code> if the set contains the characters
+     */
+    public boolean contains(char ch) {
+        for (Iterator it = set.iterator(); it.hasNext();) {
+            CharRange range = (CharRange) it.next();
+            if (range.contains(ch)) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    // Basics
+    //-----------------------------------------------------------------------
+    /**
+     * <p>Compares two CharSet objects, returning true if they represent
+     * exactly the same set of characters defined in the same way.</p>
+     *
+     * <p>The two sets <code>abc</code> and <code>a-c</code> are <i>not</i>
+     * equal according to this method.</p>
+     *
+     * @param obj  the object to compare to
+     * @return true if equal
+     * @since 2.0
+     */
+    public boolean equals(Object obj) {
+        if (obj == this) {
+            return true;
+        }
+        if (obj instanceof CharSet == false) {
+            return false;
+        }
+        CharSet other = (CharSet) obj;
+        return set.equals(other.set);
+    }
+
+    /**
+     * <p>Gets a hashCode compatible with the equals method.</p>
+     *
+     * @return a suitable hashCode
+     * @since 2.0
+     */
+    public int hashCode() {
+        return 89 + set.hashCode();
+    }
+
+    /**
+     * <p>Gets a string representation of the set.</p>
+     *
+     * @return string representation of the set
+     */
+    public String toString() {
+        return set.toString();
+    }
+
+}

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

Added: directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/CharSetUtils.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/CharSetUtils.java?rev=370807&view=auto
==============================================================================
--- directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/CharSetUtils.java (added)
+++ directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/CharSetUtils.java Fri Jan 20 05:47:50 2006
@@ -0,0 +1,389 @@
+/*
+ * 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>Operations on <code>CharSet</code>s.</p>
+ *
+ * <p>This class handles <code>null</code> input gracefully.
+ * An exception will not be thrown for a <code>null</code> input.
+ * Each method documents its behaviour in more detail.</p>
+ * 
+ * @see CharSet
+ * @author <a href="bayard@generationjava.com">Henri Yandell</a>
+ * @author Stephen Colebourne
+ * @author Phil Steitz
+ * @author Gary Gregory
+ * @since 1.0
+ * @version $Id$
+ */
+public class CharSetUtils {
+
+    /**
+     * <p>CharSetUtils instances should NOT be constructed in standard programming.
+     * Instead, the class should be used as <code>CharSetUtils.evaluateSet(null);</code>.</p>
+     *
+     * <p>This constructor is public to permit tools that require a JavaBean instance
+     * to operate.</p>
+     */
+    public CharSetUtils() {
+    }
+
+    // Factory
+    //-----------------------------------------------------------------------
+    /**
+     * <p>Creates a <code>CharSet</code> instance which allows a certain amount of
+     * set logic to be performed.</p>
+     * <p>The syntax is:</p>
+     * <ul>
+     *  <li>&quot;aeio&quot; which implies 'a','e',..</li>
+     *  <li>&quot;^e&quot; implies not e.</li>
+     *  <li>&quot;ej-m&quot; implies e,j-&gt;m. e,j,k,l,m.</li>
+     * </ul>
+     * 
+     * <pre>
+     * CharSetUtils.evaluateSet(null)    = null
+     * CharSetUtils.evaluateSet([])      = CharSet matching nothing
+     * CharSetUtils.evaluateSet(["a-e"]) = CharSet matching a,b,c,d,e
+     * </pre>
+     *
+     * @param set  the set, may be null
+     * @return a CharSet instance, <code>null</code> if null input
+     * @deprecated Use {@link CharSet#getInstance(String)}.
+     *             Method will be removed in Commons Lang 3.0.
+     */
+    public static CharSet evaluateSet(String[] set) {
+        if (set == null) {
+            return null;
+        }
+        return new CharSet(set); 
+    }
+
+    // Squeeze
+    //-----------------------------------------------------------------------
+    /**
+     * <p>Squeezes any repetitions of a character that is mentioned in the
+     * supplied set.</p>
+     *
+     * <pre>
+     * CharSetUtils.squeeze(null, *)        = null
+     * CharSetUtils.squeeze("", *)          = ""
+     * CharSetUtils.squeeze(*, null)        = *
+     * CharSetUtils.squeeze(*, "")          = *
+     * CharSetUtils.squeeze("hello", "k-p") = "helo"
+     * CharSetUtils.squeeze("hello", "a-e") = "hello"
+     * </pre>
+     *
+     * @see #evaluateSet(java.lang.String[]) for set-syntax.
+     * @param str  the string to squeeze, may be null
+     * @param set  the character set to use for manipulation, may be null
+     * @return modified String, <code>null</code> if null string input
+     */
+    public static String squeeze(String str, String set) {
+        if (StringUtils.isEmpty(str) || StringUtils.isEmpty(set)) {
+            return str;
+        }
+        String[] strs = new String[1];
+        strs[0] = set;
+        return squeeze(str, strs);
+    }
+
+    /**
+     * <p>Squeezes any repetitions of a character that is mentioned in the
+     * supplied set.</p>
+     *
+     * <p>An example is:</p>
+     * <ul>
+     *   <li>squeeze(&quot;hello&quot;, {&quot;el&quot;}) => &quot;helo&quot;</li>
+     * </ul>
+     * 
+     * @see #evaluateSet(java.lang.String[]) for set-syntax.
+     * @param str  the string to squeeze, may be null
+     * @param set  the character set to use for manipulation, may be null
+     * @return modified String, <code>null</code> if null string input
+     */
+    public static String squeeze(String str, String[] set) {
+        if (StringUtils.isEmpty(str) || ArrayUtils.isEmpty(set)) {
+            return str;
+        }
+        CharSet chars = evaluateSet(set);
+        StringBuffer buffer = new StringBuffer(str.length());
+        char[] chrs = str.toCharArray();
+        int sz = chrs.length;
+        char lastChar = ' ';
+        char ch = ' ';
+        for (int i = 0; i < sz; i++) {
+            ch = chrs[i];
+            if (chars.contains(ch)) {
+                if ((ch == lastChar) && (i != 0)) {
+                    continue;
+                }
+            }
+            buffer.append(ch);
+            lastChar = ch;
+        }
+        return buffer.toString();
+    }
+
+    // Count
+    //-----------------------------------------------------------------------
+    /**
+     * <p>Takes an argument in set-syntax, see evaluateSet,
+     * and returns the number of characters present in the specified string.</p>
+     *
+     * <pre>
+     * CharSetUtils.count(null, *)        = 0
+     * CharSetUtils.count("", *)          = 0
+     * CharSetUtils.count(*, null)        = 0
+     * CharSetUtils.count(*, "")          = 0
+     * CharSetUtils.count("hello", "k-p") = 3
+     * CharSetUtils.count("hello", "a-e") = 1
+     * </pre>
+     *
+     * @see #evaluateSet(java.lang.String[]) for set-syntax.
+     * @param str  String to count characters in, may be null
+     * @param set  String set of characters to count, may be null
+     * @return character count, zero if null string input
+     */
+    public static int count(String str, String set) {
+        if (StringUtils.isEmpty(str) || StringUtils.isEmpty(set)) {
+            return 0;
+        }
+        String[] strs = new String[1];
+        strs[0] = set;
+        return count(str, strs);
+    }
+    
+    /**
+     * <p>Takes an argument in set-syntax, see evaluateSet,
+     * and returns the number of characters present in the specified string.</p>
+     *
+     * <p>An example would be:</p>
+     * <ul>
+     *  <li>count(&quot;hello&quot;, {&quot;c-f&quot;, &quot;o&quot;}) returns 2.</li>
+     * </ul>
+     *
+     * @see #evaluateSet(java.lang.String[]) for set-syntax.
+     * @param str  String to count characters in, may be null
+     * @param set  String[] set of characters to count, may be null
+     * @return character count, zero if null string input
+     */
+    public static int count(String str, String[] set) {
+        if (StringUtils.isEmpty(str) || ArrayUtils.isEmpty(set)) {
+            return 0;
+        }
+        CharSet chars = evaluateSet(set);
+        int count = 0;
+        char[] chrs = str.toCharArray();
+        int sz = chrs.length;
+        for(int i=0; i<sz; i++) {
+            if(chars.contains(chrs[i])) {
+                count++;
+            }
+        }
+        return count;
+    }
+
+    // Keep
+    //-----------------------------------------------------------------------
+    /**
+     * <p>Takes an argument in set-syntax, see evaluateSet,
+     * and keeps any of characters present in the specified string.</p>
+     *
+     * <pre>
+     * CharSetUtils.keep(null, *)        = null
+     * CharSetUtils.keep("", *)          = ""
+     * CharSetUtils.keep(*, null)        = ""
+     * CharSetUtils.keep(*, "")          = ""
+     * CharSetUtils.keep("hello", "hl")  = "hll"
+     * CharSetUtils.keep("hello", "le")  = "ell"
+     * </pre>
+     *
+     * @see #evaluateSet(java.lang.String[]) for set-syntax.
+     * @param str  String to keep characters from, may be null
+     * @param set  String set of characters to keep, may be null
+     * @return modified String, <code>null</code> if null string input
+     * @since 2.0
+     */
+    public static String keep(String str, String set) {
+        if (str == null) {
+            return null;
+        }
+        if (str.length() == 0 || StringUtils.isEmpty(set)) {
+            return "";
+        }
+        String[] strs = new String[1];
+        strs[0] = set;
+        return keep(str, strs);
+    }
+    
+    /**
+     * <p>Takes an argument in set-syntax, see evaluateSet,
+     * and keeps any of characters present in the specified string.</p>
+     *
+     * <p>An example would be:</p>
+     * <ul>
+     *  <li>keep(&quot;hello&quot;, {&quot;c-f&quot;, &quot;o&quot;})
+     *   returns &quot;eo&quot;</li>
+     * </ul>
+     *
+     * @see #evaluateSet(java.lang.String[]) for set-syntax.
+     * @param str  String to keep characters from, may be null
+     * @param set  String[] set of characters to keep, may be null
+     * @return modified String, <code>null</code> if null string input
+     * @since 2.0
+     */
+    public static String keep(String str, String[] set) {
+        if (str == null) {
+            return null;
+        }
+        if (str.length() == 0 || ArrayUtils.isEmpty(set)) {
+            return "";
+        }
+        return modify(str, set, true);
+    }
+
+    // Delete
+    //-----------------------------------------------------------------------
+    /**
+     * <p>Takes an argument in set-syntax, see evaluateSet,
+     * and deletes any of characters present in the specified string.</p>
+     *
+     * <pre>
+     * CharSetUtils.delete(null, *)        = null
+     * CharSetUtils.delete("", *)          = ""
+     * CharSetUtils.delete(*, null)        = *
+     * CharSetUtils.delete(*, "")          = *
+     * CharSetUtils.delete("hello", "hl")  = "eo"
+     * CharSetUtils.delete("hello", "le")  = "ho"
+     * </pre>
+     *
+     * @see #evaluateSet(java.lang.String[]) for set-syntax.
+     * @param str  String to delete characters from, may be null
+     * @param set  String set of characters to delete, may be null
+     * @return modified String, <code>null</code> if null string input
+     */
+    public static String delete(String str, String set) {
+        if (StringUtils.isEmpty(str) || StringUtils.isEmpty(set)) {
+            return str;
+        }
+        String[] strs = new String[1];
+        strs[0] = set;
+        return delete(str, strs);
+    }
+    
+    /**
+     * <p>Takes an argument in set-syntax, see evaluateSet,
+     * and deletes any of characters present in the specified string.</p>
+     *
+     * <p>An example would be:</p>
+     * <ul>
+     *  <li>delete(&quot;hello&quot;, {&quot;c-f&quot;, &quot;o&quot;}) returns
+     *   &quot;hll&quot;</li>
+     * </ul>
+     *
+     * @see #evaluateSet(java.lang.String[]) for set-syntax.
+     * @param str  String to delete characters from, may be null
+     * @param set  String[] set of characters to delete, may be null
+     * @return modified String, <code>null</code> if null string input
+     */
+    public static String delete(String str, String[] set) {
+        if (StringUtils.isEmpty(str) || ArrayUtils.isEmpty(set)) {
+            return str;
+        }
+        return modify(str, set, false);
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * Implementation of delete and keep
+     *
+     * @param str String to modify characters within
+     * @param set String[] set of characters to modify
+     * @param expect whether to evaluate on match, or non-match
+     * @return modified String
+     */
+    private static String modify(String str, String[] set, boolean expect) {
+        CharSet chars = evaluateSet(set);
+        StringBuffer buffer = new StringBuffer(str.length());
+        char[] chrs = str.toCharArray();
+        int sz = chrs.length;
+        for(int i=0; i<sz; i++) {
+            if(chars.contains(chrs[i]) == expect) {
+                buffer.append(chrs[i]);
+            }
+        }
+        return buffer.toString();
+    }
+
+    // Translate
+    //-----------------------------------------------------------------------
+    /**
+     * <p>Translate characters in a String.
+     * This is a multi character search and replace routine.</p>
+     *
+     * <p>An example is:</p>
+     * <ul>
+     *   <li>translate(&quot;hello&quot;, &quot;ho&quot;, &quot;jy&quot;)
+     *    =&gt; jelly</li>
+     * </ul>
+     *
+     * <p>If the length of characters to search for is greater than the
+     * length of characters to replace, then the last character is 
+     * used.</p>
+     * 
+     * <pre>
+     * CharSetUtils.translate(null, *, *) = null
+     * CharSetUtils.translate("", *, *)   = ""
+     * </pre>
+     *
+     * @param str  String to replace characters in, may be null
+     * @param searchChars   a set of characters to search for, must not be null
+     * @param replaceChars  a set of characters to replace, must not be null or empty (&quot;&quot;)
+     * @return translated String, <code>null</code> if null string input
+     * @throws NullPointerException if <code>searchChars</code> or <code>replaceChars</code> 
+     *  is <code>null</code>
+     * @throws ArrayIndexOutOfBoundsException if <code>replaceChars</code> is empty (&quot;&quot;)
+     * @deprecated Use {@link StringUtils#replaceChars(String, String, String)}.
+     *             Method will be removed in Commons Lang 3.0.
+     *  NOTE: StringUtils#replaceChars behaves differently when 'searchChars' is longer
+     *  than 'replaceChars'. CharSetUtils#translate will use the last char of the replacement
+     *  string whereas StringUtils#replaceChars will delete
+     */
+    public static String translate(String str, String searchChars, String replaceChars) {
+        if (StringUtils.isEmpty(str)) {
+            return str;
+        }
+        StringBuffer buffer = new StringBuffer(str.length());
+        char[] chrs = str.toCharArray();
+        char[] withChrs = replaceChars.toCharArray();
+        int sz = chrs.length;
+        int withMax = replaceChars.length() - 1;
+        for(int i=0; i<sz; i++) {
+            int idx = searchChars.indexOf(chrs[i]);
+            if(idx != -1) {
+                if(idx > withMax) {
+                    idx = withMax;
+                }
+                buffer.append(withChrs[idx]);
+            } else {
+                buffer.append(chrs[i]);
+            }
+        }
+        return buffer.toString();
+    }
+
+}

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



Mime
View raw message