2009/04/15 - Apache HiveMind has been retired.

For more information, please explore the Attic.

Clover coverage report - Code Coverage for hivemind release 1.2.1
Coverage timestamp: Fri Feb 10 2006 16:33:43 PST
file stats: LOC: 125   Methods: 4
NCLOC: 65   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
StringUtils.java 90% 74.4% 75% 79.4%
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.hivemind.util;
 16   
 17    import java.util.ArrayList;
 18    import java.util.List;
 19   
 20    /**
 21    * A subset of the utilities available in commons-lang StringUtils. It's all
 22    * about reducing dependencies, baby!
 23    *
 24    * @author Howard Lewis Ship
 25    */
 26    public class StringUtils
 27    {
 28   
 29    /**
 30    * Splits an input string into a an array of strings, seperating
 31    * at commas.
 32    *
 33    * @param input the string to split, possibly null or empty
 34    * @return an array of the strings split at commas
 35    */
 36  168 public static String[] split(String input)
 37    {
 38  168 if (input == null)
 39  122 return new String[0];
 40   
 41  46 List strings = new ArrayList();
 42   
 43  46 int startx = 0;
 44  46 int cursor = 0;
 45  46 int length = input.length();
 46   
 47  46 while (cursor < length)
 48    {
 49  552 if (input.charAt(cursor) == ',')
 50    {
 51  12 String item = input.substring(startx, cursor);
 52  12 strings.add(item);
 53  12 startx = cursor + 1;
 54    }
 55   
 56  552 cursor++;
 57    }
 58   
 59  46 if (startx < length)
 60  45 strings.add(input.substring(startx));
 61   
 62  46 return (String[]) strings.toArray(new String[strings.size()]);
 63    }
 64   
 65    /**
 66    * Converts a string such that the first character is upper case.
 67    *
 68    * @param input the input string (possibly empty)
 69    * @return the string with the first character converted from lowercase to upper case (may
 70    * return the string unchanged if already capitalized)
 71    */
 72   
 73  7 public static String capitalize(String input)
 74    {
 75  7 if (input.length() == 0)
 76  1 return input;
 77   
 78  5 char ch = input.charAt(0);
 79   
 80  5 if (Character.isUpperCase(ch))
 81  1 return input;
 82   
 83  4 return String.valueOf(Character.toUpperCase(ch)) + input.substring(1);
 84    }
 85   
 86  7 public static String join(String[] input, char separator)
 87    {
 88  7 if (input == null || input.length == 0)
 89  2 return null;
 90   
 91  5 StringBuffer buffer = new StringBuffer();
 92   
 93  5 for (int i = 0; i < input.length; i++)
 94    {
 95  9 if (i > 0)
 96  4 buffer.append(separator);
 97   
 98  9 buffer.append(input[i]);
 99    }
 100   
 101  5 return buffer.toString();
 102    }
 103   
 104    /**
 105    * Replaces all occurrences of <code>pattern</code> in
 106    * <code>string</code> with <code>replacement</code>
 107    */
 108  0 public static String replace(String string, String pattern, String replacement)
 109    {
 110  0 StringBuffer sbuf = new StringBuffer();
 111  0 int index = string.indexOf(pattern);
 112  0 int pos = 0;
 113  0 int patternLength = pattern.length();
 114  0 for(; index >= 0; index = string.indexOf(pattern, pos))
 115    {
 116  0 sbuf.append(string.substring(pos, index));
 117  0 sbuf.append(replacement);
 118  0 pos = index + patternLength;
 119    }
 120  0 sbuf.append(string.substring(pos));
 121   
 122  0 return sbuf.toString();
 123    }
 124   
 125    }