[jboss-svn-commits] JBL Code SVN: r11328 - in labs/jbossrules/trunk/drools-core/src/main/java/org: drools/rule and 1 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Wed Apr 25 13:13:42 EDT 2007


Author: mark.proctor at jboss.com
Date: 2007-04-25 13:13:42 -0400 (Wed, 25 Apr 2007)
New Revision: 11328

Added:
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/StringUtils.java
Modified:
   labs/jbossrules/trunk/drools-core/src/main/java/org/codehaus/jfdi/interpreter/ClassTypeResolver.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/Package.java
Log:
JBRULES-713 Make Dialects Pluggeable
-BuildUtils is now gone, moved into JavaDialect

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/codehaus/jfdi/interpreter/ClassTypeResolver.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/codehaus/jfdi/interpreter/ClassTypeResolver.java	2007-04-25 17:11:02 UTC (rev 11327)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/codehaus/jfdi/interpreter/ClassTypeResolver.java	2007-04-25 17:13:42 UTC (rev 11328)
@@ -28,13 +28,13 @@
 public class ClassTypeResolver
     implements
     TypeResolver {
-    private List              imports;
+    private List             imports;
 
-    private final ClassLoader classLoader;
+    private ClassLoader      classLoader;
 
-    private Map               cachedImports    = new HashMap();
+    private Map              cachedImports    = new HashMap();
 
-    private static final Map        internalNamesMap = new HashMap();
+    private static final Map internalNamesMap = new HashMap();
     static {
         internalNamesMap.put( "int",
                               "I" );
@@ -78,6 +78,10 @@
         this.classLoader = classLoader;
     }
 
+    public void setClassLoader(ClassLoader classLoader) {
+        this.classLoader = classLoader;
+    }
+    
     /*
      * (non-Javadoc)
      * 
@@ -135,7 +139,7 @@
             isArray = true;
             int bracketIndex = className.indexOf( '[' );
             final String componentName = className.substring( 0,
-                                                        bracketIndex );
+                                                              bracketIndex );
             arrayClassName.append( '[' );
             while ( (bracketIndex = className.indexOf( '[',
                                                        bracketIndex + 1 )) > 0 ) {

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/Package.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/Package.java	2007-04-25 17:11:02 UTC (rev 11327)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/Package.java	2007-04-25 17:13:42 UTC (rev 11328)
@@ -33,6 +33,7 @@
 
 import org.drools.common.ObjectInputStreamWithLoader;
 import org.drools.facttemplates.FactTemplate;
+import org.drools.util.StringUtils;
 
 /**
  * Collection of related <code>Rule</code>s.
@@ -243,7 +244,7 @@
 
     public void removeFunction(final String functionName) {
         this.functions.remove( functionName );
-        this.packageCompilationData.remove( this.name + "." + ucFirst( functionName ) );
+        this.packageCompilationData.remove( this.name + "." + StringUtils.ucFirst( functionName ) );
     }
 
     public FactTemplate getFactTemplate(final String name) {
@@ -393,10 +394,6 @@
         return this.name.hashCode();
     }
 
-    private String ucFirst(final String name) {
-        return name.toUpperCase().charAt( 0 ) + name.substring( 1 );
-    }
-
     public void clear() {
         this.rules.clear();
         this.packageCompilationData.clear();

Added: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/StringUtils.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/StringUtils.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/StringUtils.java	2007-04-25 17:13:42 UTC (rev 11328)
@@ -0,0 +1,617 @@
+package org.drools.util;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Ripped form commons StringUtil:
+ * 
+ * <p>Operations on {@link java.lang.String} that are
+ * <code>null</code> safe.</p>
+ *
+ * <ul>
+ *  <li><b>IsEmpty/IsBlank</b>
+ *      - checks if a String contains text</li>
+ *  <li><b>Trim/Strip</b>
+ *      - removes leading and trailing whitespace</li>
+ *  <li><b>Equals</b>
+ *      - compares two strings null-safe</li>
+ *  <li><b>IndexOf/LastIndexOf/Contains</b>
+ *      - null-safe index-of checks
+ *  <li><b>IndexOfAny/LastIndexOfAny/IndexOfAnyBut/LastIndexOfAnyBut</b>
+ *      - index-of any of a set of Strings</li>
+ *  <li><b>ContainsOnly/ContainsNone</b>
+ *      - does String contains only/none of these characters</li>
+ *  <li><b>Substring/Left/Right/Mid</b>
+ *      - null-safe substring extractions</li>
+ *  <li><b>SubstringBefore/SubstringAfter/SubstringBetween</b>
+ *      - substring extraction relative to other strings</li>
+ *  <li><b>Split/Join</b>
+ *      - splits a String into an array of substrings and vice versa</li>
+ *  <li><b>Remove/Delete</b>
+ *      - removes part of a String</li>
+ *  <li><b>Replace/Overlay</b>
+ *      - Searches a String and replaces one String with another</li>
+ *  <li><b>Chomp/Chop</b>
+ *      - removes the last part of a String</li>
+ *  <li><b>LeftPad/RightPad/Center/Repeat</b>
+ *      - pads a String</li>
+ *  <li><b>UpperCase/LowerCase/SwapCase/Capitalize/Uncapitalize</b>
+ *      - changes the case of a String</li>
+ *  <li><b>CountMatches</b>
+ *      - counts the number of occurrences of one String in another</li>
+ *  <li><b>IsAlpha/IsNumeric/IsWhitespace/IsAsciiPrintable</b>
+ *      - checks the characters in a String</li>
+ *  <li><b>DefaultString</b>
+ *      - protects against a null input String</li>
+ *  <li><b>Reverse/ReverseDelimited</b>
+ *      - reverses a String</li>
+ *  <li><b>Abbreviate</b>
+ *      - abbreviates a string using ellipsis</li>
+ *  <li><b>Difference</b>
+ *      - compares two Strings and reports on their differences</li>
+ *  <li><b>LevensteinDistance</b>
+ *      - the number of changes needed to change one String into another</li>
+ * </ul>
+ *
+ * <p>The <code>StringUtils</code> class defines certain words related to
+ * String handling.</p>
+ *
+ * <ul>
+ *  <li>null - <code>null</code></li>
+ *  <li>empty - a zero-length string (<code>""</code>)</li>
+ *  <li>space - the space character (<code>' '</code>, char 32)</li>
+ *  <li>whitespace - the characters defined by {@link Character#isWhitespace(char)}</li>
+ *  <li>trim - the characters &lt;= 32 as in {@link String#trim()}</li>
+ * </ul>
+ *
+ * <p><code>StringUtils</code> handles <code>null</code> input Strings quietly.
+ * That is to say that a <code>null</code> input will return <code>null</code>.
+ * Where a <code>boolean</code> or <code>int</code> is being returned
+ * details vary by method.</p>
+ *
+ * <p>A side effect of the <code>null</code> handling is that a
+ * <code>NullPointerException</code> should be considered a bug in
+ * <code>StringUtils</code> (except for deprecated methods).</p>
+ *
+ * <p>Methods in this class give sample code to explain their operation.
+ * The symbol <code>*</code> is used to indicate any input including <code>null</code>.</p>
+ *
+ * @see java.lang.String
+ * @author <a href="http://jakarta.apache.org/turbine/">Apache Jakarta Turbine</a>
+ * @author <a href="mailto:jon at latchkey.com">Jon S. Stevens</a>
+ * @author <a href="mailto:dlr at finemaltcoding.com">Daniel Rall</a>
+ * @author <a href="mailto:gcoladonato at yahoo.com">Greg Coladonato</a>
+ * @author <a href="mailto:ed at apache.org">Ed Korthof</a>
+ * @author <a href="mailto:rand_mcneely at yahoo.com">Rand McNeely</a>
+ * @author Stephen Colebourne
+ * @author <a href="mailto:fredrik at westermarck.com">Fredrik Westermarck</a>
+ * @author Holger Krauth
+ * @author <a href="mailto:alex at purpletech.com">Alexander Day Chaffee</a>
+ * @author <a href="mailto:hps at intermeta.de">Henning P. Schmiedehausen</a>
+ * @author Arun Mammen Thomas
+ * @author Gary Gregory
+ * @author Phil Steitz
+ * @author Al Chou
+ * @author Michael Davey
+ * @author Reuben Sivan
+ * @author Chris Hyzer
+ * @since 1.0
+ * @version $Id$
+ */
+public class StringUtils {
+
+    /**
+     * An empty immutable <code>String</code> array.
+     */
+    public static final String[] EMPTY_STRING_ARRAY = new String[0];
+
+    // Performance testing notes (JDK 1.4, Jul03, scolebourne)
+    // Whitespace:
+    // Character.isWhitespace() is faster than WHITESPACE.indexOf()
+    // where WHITESPACE is a string of all whitespace characters
+    //
+    // Character access:
+    // String.charAt(n) versus toCharArray(), then array[n]
+    // String.charAt(n) is about 15% worse for a 10K string
+    // They are about equal for a length 50 string
+    // String.charAt(n) is about 4 times better for a length 3 string
+    // String.charAt(n) is best bet overall
+    //
+    // Append:
+    // String.concat about twice as fast as StringBuffer.append
+    // (not sure who tested this)
+
+    /**
+     * The empty String <code>""</code>.
+     * @since 2.0
+     */
+    public static final String   EMPTY              = "";
+
+    /**
+     * Represents a failed index search.
+     * @since 2.1
+     */
+    public static final int      INDEX_NOT_FOUND    = -1;
+
+    /**
+     * <p>The maximum size to which the padding constant(s) can expand.</p>
+     */
+    private static final int     PAD_LIMIT          = 8192;
+
+    /**
+     * <p><code>StringUtils</code> instances should NOT be constructed in
+     * standard programming. Instead, the class should be used as
+     * <code>StringUtils.trim(" foo ");</code>.</p>
+     *
+     * <p>This constructor is public to permit tools that require a JavaBean
+     * instance to operate.</p>
+     */
+    public StringUtils() {
+        super();
+    }
+    
+    public static String ucFirst(final String name) {
+        return name.toUpperCase().charAt( 0 ) + name.substring( 1 );
+    }
+    
+
+    // Empty checks
+    //-----------------------------------------------------------------------
+    /**
+     * <p>Checks if a String is empty ("") or null.</p>
+     *
+     * <pre>
+     * StringUtils.isEmpty(null)      = true
+     * StringUtils.isEmpty("")        = true
+     * StringUtils.isEmpty(" ")       = false
+     * StringUtils.isEmpty("bob")     = false
+     * StringUtils.isEmpty("  bob  ") = false
+     * </pre>
+     *
+     * <p>NOTE: This method changed in Lang version 2.0.
+     * It no longer trims the String.
+     * That functionality is available in isBlank().</p>
+     *
+     * @param str  the String to check, may be null
+     * @return <code>true</code> if the String is empty or null
+     */
+    public static boolean isEmpty(final String str) {
+        return str == null || str.length() == 0;
+    }
+
+    // Padding
+    //-----------------------------------------------------------------------
+    /**
+     * <p>Repeat a String <code>repeat</code> times to form a
+     * new String.</p>
+     *
+     * <pre>
+     * StringUtils.repeat(null, 2) = null
+     * StringUtils.repeat("", 0)   = ""
+     * StringUtils.repeat("", 2)   = ""
+     * StringUtils.repeat("a", 3)  = "aaa"
+     * StringUtils.repeat("ab", 2) = "abab"
+     * StringUtils.repeat("a", -2) = ""
+     * </pre>
+     *
+     * @param str  the String to repeat, may be null
+     * @param repeat  number of times to repeat str, negative treated as zero
+     * @return a new String consisting of the original String repeated,
+     *  <code>null</code> if null String input
+     */
+    public static String repeat(final String str,
+                                final int repeat) {
+        // Performance tuned for 2.0 (JDK1.4)
+
+        if ( str == null ) {
+            return null;
+        }
+        if ( repeat <= 0 ) {
+            return EMPTY;
+        }
+        final int inputLength = str.length();
+        if ( repeat == 1 || inputLength == 0 ) {
+            return str;
+        }
+        if ( inputLength == 1 && repeat <= PAD_LIMIT ) {
+            return padding( repeat,
+                            str.charAt( 0 ) );
+        }
+
+        final int outputLength = inputLength * repeat;
+        switch ( inputLength ) {
+            case 1 :
+                final char ch = str.charAt( 0 );
+                final char[] output1 = new char[outputLength];
+                for ( int i = repeat - 1; i >= 0; i-- ) {
+                    output1[i] = ch;
+                }
+                return new String( output1 );
+            case 2 :
+                final char ch0 = str.charAt( 0 );
+                final char ch1 = str.charAt( 1 );
+                final char[] output2 = new char[outputLength];
+                for ( int i = repeat * 2 - 2; i >= 0; i--, i-- ) {
+                    output2[i] = ch0;
+                    output2[i + 1] = ch1;
+                }
+                return new String( output2 );
+            default :
+                final StringBuffer buf = new StringBuffer( outputLength );
+                for ( int i = 0; i < repeat; i++ ) {
+                    buf.append( str );
+                }
+                return buf.toString();
+        }
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * <p>Splits the provided text into an array, using whitespace as the
+     * separator, preserving all tokens, including empty tokens created by 
+     * adjacent separators. This is an alternative to using StringTokenizer.
+     * Whitespace is defined by {@link Character#isWhitespace(char)}.</p>
+     *
+     * <p>The separator is not included in the returned String array.
+     * Adjacent separators are treated as separators for empty tokens.
+     * For more control over the split use the StrTokenizer class.</p>
+     *
+     * <p>A <code>null</code> input String returns <code>null</code>.</p>
+     *
+     * <pre>
+     * StringUtils.splitPreserveAllTokens(null)       = null
+     * StringUtils.splitPreserveAllTokens("")         = []
+     * StringUtils.splitPreserveAllTokens("abc def")  = ["abc", "def"]
+     * StringUtils.splitPreserveAllTokens("abc  def") = ["abc", "", "def"]
+     * StringUtils.splitPreserveAllTokens(" abc ")    = ["", "abc", ""]
+     * </pre>
+     *
+     * @param str  the String to parse, may be <code>null</code>
+     * @return an array of parsed Strings, <code>null</code> if null String input
+     * @since 2.1
+     */
+    public static String[] splitPreserveAllTokens(final String str) {
+        return splitWorker( str,
+                            null,
+                            -1,
+                            true );
+    }
+
+    /**
+     * <p>Splits the provided text into an array, separator specified,
+     * preserving all tokens, including empty tokens created by adjacent
+     * separators. This is an alternative to using StringTokenizer.</p>
+     *
+     * <p>The separator is not included in the returned String array.
+     * Adjacent separators are treated as separators for empty tokens.
+     * For more control over the split use the StrTokenizer class.</p>
+     *
+     * <p>A <code>null</code> input String returns <code>null</code>.</p>
+     *
+     * <pre>
+     * StringUtils.splitPreserveAllTokens(null, *)         = null
+     * StringUtils.splitPreserveAllTokens("", *)           = []
+     * StringUtils.splitPreserveAllTokens("a.b.c", '.')    = ["a", "b", "c"]
+     * StringUtils.splitPreserveAllTokens("a..b.c", '.')   = ["a", "", "b", "c"]
+     * StringUtils.splitPreserveAllTokens("a:b:c", '.')    = ["a:b:c"]
+     * StringUtils.splitPreserveAllTokens("a\tb\nc", null) = ["a", "b", "c"]
+     * StringUtils.splitPreserveAllTokens("a b c", ' ')    = ["a", "b", "c"]
+     * StringUtils.splitPreserveAllTokens("a b c ", ' ')   = ["a", "b", "c", ""]
+     * StringUtils.splitPreserveAllTokens("a b c  ", ' ')   = ["a", "b", "c", "", ""]
+     * StringUtils.splitPreserveAllTokens(" a b c", ' ')   = ["", a", "b", "c"]
+     * StringUtils.splitPreserveAllTokens("  a b c", ' ')  = ["", "", a", "b", "c"]
+     * StringUtils.splitPreserveAllTokens(" a b c ", ' ')  = ["", a", "b", "c", ""]
+     * </pre>
+     *
+     * @param str  the String to parse, may be <code>null</code>
+     * @param separatorChar  the character used as the delimiter,
+     *  <code>null</code> splits on whitespace
+     * @return an array of parsed Strings, <code>null</code> if null String input
+     * @since 2.1
+     */
+    public static String[] splitPreserveAllTokens(final String str,
+                                                  final char separatorChar) {
+        return splitWorker( str,
+                            separatorChar,
+                            true );
+    }
+
+    /**
+     * Performs the logic for the <code>split</code> and 
+     * <code>splitPreserveAllTokens</code> methods that do not return a
+     * maximum array length.
+     *
+     * @param str  the String to parse, may be <code>null</code>
+     * @param separatorChar the separate character
+     * @param preserveAllTokens if <code>true</code>, adjacent separators are
+     * treated as empty token separators; if <code>false</code>, adjacent
+     * separators are treated as one separator.
+     * @return an array of parsed Strings, <code>null</code> if null String input
+     */
+    private static String[] splitWorker(final String str,
+                                        final char separatorChar,
+                                        final boolean preserveAllTokens) {
+        // Performance tuned for 2.0 (JDK1.4)
+
+        if ( str == null ) {
+            return null;
+        }
+        final int len = str.length();
+        if ( len == 0 ) {
+            return EMPTY_STRING_ARRAY;
+        }
+        final List list = new ArrayList();
+        int i = 0, start = 0;
+        boolean match = false;
+        boolean lastMatch = false;
+        while ( i < len ) {
+            if ( str.charAt( i ) == separatorChar ) {
+                if ( match || preserveAllTokens ) {
+                    list.add( str.substring( start,
+                                             i ) );
+                    match = false;
+                    lastMatch = true;
+                }
+                start = ++i;
+                continue;
+            } else {
+                lastMatch = false;
+            }
+            match = true;
+            i++;
+        }
+        if ( match || (preserveAllTokens && lastMatch) ) {
+            list.add( str.substring( start,
+                                     i ) );
+        }
+        return (String[]) list.toArray( new String[list.size()] );
+    }
+
+    /**
+     * <p>Splits the provided text into an array, separators specified, 
+     * preserving all tokens, including empty tokens created by adjacent
+     * separators. This is an alternative to using StringTokenizer.</p>
+     *
+     * <p>The separator is not included in the returned String array.
+     * Adjacent separators are treated as separators for empty tokens.
+     * For more control over the split use the StrTokenizer class.</p>
+     *
+     * <p>A <code>null</code> input String returns <code>null</code>.
+     * A <code>null</code> separatorChars splits on whitespace.</p>
+     *
+     * <pre>
+     * StringUtils.splitPreserveAllTokens(null, *)           = null
+     * StringUtils.splitPreserveAllTokens("", *)             = []
+     * StringUtils.splitPreserveAllTokens("abc def", null)   = ["abc", "def"]
+     * StringUtils.splitPreserveAllTokens("abc def", " ")    = ["abc", "def"]
+     * StringUtils.splitPreserveAllTokens("abc  def", " ")   = ["abc", "", def"]
+     * StringUtils.splitPreserveAllTokens("ab:cd:ef", ":")   = ["ab", "cd", "ef"]
+     * StringUtils.splitPreserveAllTokens("ab:cd:ef:", ":")  = ["ab", "cd", "ef", ""]
+     * StringUtils.splitPreserveAllTokens("ab:cd:ef::", ":") = ["ab", "cd", "ef", "", ""]
+     * StringUtils.splitPreserveAllTokens("ab::cd:ef", ":")  = ["ab", "", cd", "ef"]
+     * StringUtils.splitPreserveAllTokens(":cd:ef", ":")     = ["", cd", "ef"]
+     * StringUtils.splitPreserveAllTokens("::cd:ef", ":")    = ["", "", cd", "ef"]
+     * StringUtils.splitPreserveAllTokens(":cd:ef:", ":")    = ["", cd", "ef", ""]
+     * </pre>
+     *
+     * @param str  the String to parse, may be <code>null</code>
+     * @param separatorChars  the characters used as the delimiters,
+     *  <code>null</code> splits on whitespace
+     * @return an array of parsed Strings, <code>null</code> if null String input
+     * @since 2.1
+     */
+    public static String[] splitPreserveAllTokens(final String str,
+                                                  final String separatorChars) {
+        return splitWorker( str,
+                            separatorChars,
+                            -1,
+                            true );
+    }
+
+    /**
+     * <p>Splits the provided text into an array with a maximum length,
+     * separators specified, preserving all tokens, including empty tokens 
+     * created by adjacent separators.</p>
+     *
+     * <p>The separator is not included in the returned String array.
+     * Adjacent separators are treated as separators for empty tokens.
+     * Adjacent separators are treated as one separator.</p>
+     *
+     * <p>A <code>null</code> input String returns <code>null</code>.
+     * A <code>null</code> separatorChars splits on whitespace.</p>
+     *
+     * <p>If more than <code>max</code> delimited substrings are found, the last
+     * returned string includes all characters after the first <code>max - 1</code>
+     * returned strings (including separator characters).</p>
+     *
+     * <pre>
+     * StringUtils.splitPreserveAllTokens(null, *, *)            = null
+     * StringUtils.splitPreserveAllTokens("", *, *)              = []
+     * StringUtils.splitPreserveAllTokens("ab de fg", null, 0)   = ["ab", "cd", "ef"]
+     * StringUtils.splitPreserveAllTokens("ab   de fg", null, 0) = ["ab", "cd", "ef"]
+     * StringUtils.splitPreserveAllTokens("ab:cd:ef", ":", 0)    = ["ab", "cd", "ef"]
+     * StringUtils.splitPreserveAllTokens("ab:cd:ef", ":", 2)    = ["ab", "cd:ef"]
+     * StringUtils.splitPreserveAllTokens("ab   de fg", null, 2) = ["ab", "  de fg"]
+     * StringUtils.splitPreserveAllTokens("ab   de fg", null, 3) = ["ab", "", " de fg"]
+     * StringUtils.splitPreserveAllTokens("ab   de fg", null, 4) = ["ab", "", "", "de fg"]
+     * </pre>
+     *
+     * @param str  the String to parse, may be <code>null</code>
+     * @param separatorChars  the characters used as the delimiters,
+     *  <code>null</code> splits on whitespace
+     * @param max  the maximum number of elements to include in the
+     *  array. A zero or negative value implies no limit
+     * @return an array of parsed Strings, <code>null</code> if null String input
+     * @since 2.1
+     */
+    public static String[] splitPreserveAllTokens(final String str,
+                                                  final String separatorChars,
+                                                  final int max) {
+        return splitWorker( str,
+                            separatorChars,
+                            max,
+                            true );
+    }
+
+    /**
+     * Performs the logic for the <code>split</code> and 
+     * <code>splitPreserveAllTokens</code> methods that return a maximum array 
+     * length.
+     *
+     * @param str  the String to parse, may be <code>null</code>
+     * @param separatorChars the separate character
+     * @param max  the maximum number of elements to include in the
+     *  array. A zero or negative value implies no limit.
+     * @param preserveAllTokens if <code>true</code>, adjacent separators are
+     * treated as empty token separators; if <code>false</code>, adjacent
+     * separators are treated as one separator.
+     * @return an array of parsed Strings, <code>null</code> if null String input
+     */
+    private static String[] splitWorker(final String str,
+                                        final String separatorChars,
+                                        final int max,
+                                        final boolean preserveAllTokens) {
+        // Performance tuned for 2.0 (JDK1.4)
+        // Direct code is quicker than StringTokenizer.
+        // Also, StringTokenizer uses isSpace() not isWhitespace()
+
+        if ( str == null ) {
+            return null;
+        }
+        final int len = str.length();
+        if ( len == 0 ) {
+            return EMPTY_STRING_ARRAY;
+        }
+        final List list = new ArrayList();
+        int sizePlus1 = 1;
+        int i = 0, start = 0;
+        boolean match = false;
+        boolean lastMatch = false;
+        if ( separatorChars == null ) {
+            // Null separator means use whitespace
+            while ( i < len ) {
+                if ( Character.isWhitespace( str.charAt( i ) ) ) {
+                    if ( match || preserveAllTokens ) {
+                        lastMatch = true;
+                        if ( sizePlus1++ == max ) {
+                            i = len;
+                            lastMatch = false;
+                        }
+                        list.add( str.substring( start,
+                                                 i ) );
+                        match = false;
+                    }
+                    start = ++i;
+                    continue;
+                } else {
+                    lastMatch = false;
+                }
+                match = true;
+                i++;
+            }
+        } else if ( separatorChars.length() == 1 ) {
+            // Optimise 1 character case
+            final char sep = separatorChars.charAt( 0 );
+            while ( i < len ) {
+                if ( str.charAt( i ) == sep ) {
+                    if ( match || preserveAllTokens ) {
+                        lastMatch = true;
+                        if ( sizePlus1++ == max ) {
+                            i = len;
+                            lastMatch = false;
+                        }
+                        list.add( str.substring( start,
+                                                 i ) );
+                        match = false;
+                    }
+                    start = ++i;
+                    continue;
+                } else {
+                    lastMatch = false;
+                }
+                match = true;
+                i++;
+            }
+        } else {
+            // standard case
+            while ( i < len ) {
+                if ( separatorChars.indexOf( str.charAt( i ) ) >= 0 ) {
+                    if ( match || preserveAllTokens ) {
+                        lastMatch = true;
+                        if ( sizePlus1++ == max ) {
+                            i = len;
+                            lastMatch = false;
+                        }
+                        list.add( str.substring( start,
+                                                 i ) );
+                        match = false;
+                    }
+                    start = ++i;
+                    continue;
+                } else {
+                    lastMatch = false;
+                }
+                match = true;
+                i++;
+            }
+        }
+        if ( match || (preserveAllTokens && lastMatch) ) {
+            list.add( str.substring( start,
+                                     i ) );
+        }
+        return (String[]) list.toArray( new String[list.size()] );
+    }
+
+    /**
+     * <p>Returns padding using the specified delimiter repeated
+     * to a given length.</p>
+     *
+     * <pre>
+     * StringUtils.padding(0, 'e')  = ""
+     * StringUtils.padding(3, 'e')  = "eee"
+     * StringUtils.padding(-2, 'e') = IndexOutOfBoundsException
+     * </pre>
+     *
+     * <p>Note: this method doesn't not support padding with
+     * <a href="http://www.unicode.org/glossary/#supplementary_character">Unicode Supplementary Characters</a>
+     * as they require a pair of <code>char</code>s to be represented.
+     * If you are needing to support full I18N of your applications
+     * consider using {@link #repeat(String, int)} instead. 
+     * </p>
+     *
+     * @param repeat  number of times to repeat delim
+     * @param padChar  character to repeat
+     * @return String with repeated character
+     * @throws IndexOutOfBoundsException if <code>repeat &lt; 0</code>
+     * @see #repeat(String, int)
+     */
+    private static String padding(final int repeat,
+                                  final char padChar) throws IndexOutOfBoundsException {
+        if ( repeat < 0 ) {
+            throw new IndexOutOfBoundsException( "Cannot pad a negative amount: " + repeat );
+        }
+        final char[] buf = new char[repeat];
+        for ( int i = 0; i < buf.length; i++ ) {
+            buf[i] = padChar;
+        }
+        return new String( buf );
+    }
+}
\ No newline at end of file




More information about the jboss-svn-commits mailing list