Clover coverage report - Code Coverage for tapestry release 4.0-beta-9
Coverage timestamp: Sat Oct 1 2005 08:36:20 EDT
file stats: LOC: 123   Methods: 3
NCLOC: 61   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
StringSplitter.java 80% 89.5% 66.7% 86.3%
coverage coverage
 1    // Copyright 2004, 2005 The Apache Software Foundation
 2    //
 3    // Licensed under the Apache License, Version 2.0 (the "License");
 4    // you may not use this file except in compliance with the License.
 5    // You may obtain a copy of the License at
 6    //
 7    // http://www.apache.org/licenses/LICENSE-2.0
 8    //
 9    // Unless required by applicable law or agreed to in writing, software
 10    // distributed under the License is distributed on an "AS IS" BASIS,
 11    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12    // See the License for the specific language governing permissions and
 13    // limitations under the License.
 14   
 15    package org.apache.tapestry.util;
 16   
 17    /**
 18    * Used to split a string into substrings based on a single character
 19    * delimiter. A fast, simple version of
 20    * {@link java.util.StringTokenizer}.
 21    *
 22    * @author Howard Lewis Ship
 23    *
 24    **/
 25   
 26    public class StringSplitter
 27    {
 28    private char delimiter;
 29   
 30  57 public StringSplitter(char delimiter)
 31    {
 32  57 this.delimiter = delimiter;
 33    }
 34   
 35  0 public char getDelimiter()
 36    {
 37  0 return delimiter;
 38    }
 39   
 40    /**
 41    * Splits a string on the delimter into an array of String
 42    * tokens. The delimiters are not included in the tokens. Null
 43    * tokens (caused by two consecutive delimiter) are reduced to an
 44    * empty string. Leading delimiters are ignored.
 45    *
 46    **/
 47   
 48  57 public String[] splitToArray(String value)
 49    {
 50  57 char[] buffer;
 51  57 int i;
 52  57 String[] result;
 53  57 int resultCount = 0;
 54  57 int start;
 55  57 int length;
 56  57 String token;
 57  57 String[] newResult;
 58  57 boolean first = true;
 59   
 60  57 buffer = value.toCharArray();
 61   
 62  57 result = new String[3];
 63   
 64  57 start = 0;
 65  57 length = 0;
 66   
 67  57 for (i = 0; i < buffer.length; i++)
 68    {
 69  390 if (buffer[i] != delimiter)
 70    {
 71  389 length++;
 72  389 continue;
 73    }
 74   
 75    // This is used to ignore leading delimiter(s).
 76   
 77  1 if (length > 0 || !first)
 78    {
 79  1 token = new String(buffer, start, length);
 80   
 81  1 if (resultCount == result.length)
 82    {
 83  0 newResult = new String[result.length * 2];
 84   
 85  0 System.arraycopy(result, 0, newResult, 0, result.length);
 86   
 87  0 result = newResult;
 88    }
 89   
 90  1 result[resultCount++] = token;
 91   
 92  1 first = false;
 93    }
 94   
 95  1 start = i + 1;
 96  1 length = 0;
 97    }
 98   
 99    // Special case: if the string contains no delimiters
 100    // then it isn't really split. Wrap the input string
 101    // in an array and return. This is a little optimization
 102    // to prevent a new String instance from being
 103    // created unnecessarily.
 104   
 105  57 if (start == 0 && length == buffer.length)
 106    {
 107  56 result = new String[1];
 108  56 result[0] = value;
 109  56 return result;
 110    }
 111   
 112    // If the string is all delimiters, then this
 113    // will result in a single empty token.
 114   
 115  1 token = new String(buffer, start, length);
 116   
 117  1 newResult = new String[resultCount + 1];
 118  1 System.arraycopy(result, 0, newResult, 0, resultCount);
 119  1 newResult[resultCount] = token;
 120   
 121  1 return newResult;
 122    }
 123    }