001 // Copyright 2004, 2005 The Apache Software Foundation
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014
015 package org.apache.hivemind.util;
016
017 import java.util.ArrayList;
018 import java.util.List;
019
020 /**
021 * A subset of the utilities available in commons-lang StringUtils. It's all
022 * about reducing dependencies, baby!
023 *
024 * @author Howard Lewis Ship
025 */
026 public class StringUtils
027 {
028
029 /**
030 * Splits an input string into a an array of strings, seperating
031 * at commas.
032 *
033 * @param input the string to split, possibly null or empty
034 * @return an array of the strings split at commas
035 */
036 public static String[] split(String input)
037 {
038 if (input == null)
039 return new String[0];
040
041 List strings = new ArrayList();
042
043 int startx = 0;
044 int cursor = 0;
045 int length = input.length();
046
047 while (cursor < length)
048 {
049 if (input.charAt(cursor) == ',')
050 {
051 String item = input.substring(startx, cursor);
052 strings.add(item);
053 startx = cursor + 1;
054 }
055
056 cursor++;
057 }
058
059 if (startx < length)
060 strings.add(input.substring(startx));
061
062 return (String[]) strings.toArray(new String[strings.size()]);
063 }
064
065 /**
066 * Converts a string such that the first character is upper case.
067 *
068 * @param input the input string (possibly empty)
069 * @return the string with the first character converted from lowercase to upper case (may
070 * return the string unchanged if already capitalized)
071 */
072
073 public static String capitalize(String input)
074 {
075 if (input.length() == 0)
076 return input;
077
078 char ch = input.charAt(0);
079
080 if (Character.isUpperCase(ch))
081 return input;
082
083 return String.valueOf(Character.toUpperCase(ch)) + input.substring(1);
084 }
085
086 public static String join(String[] input, char separator)
087 {
088 if (input == null || input.length == 0)
089 return null;
090
091 StringBuffer buffer = new StringBuffer();
092
093 for (int i = 0; i < input.length; i++)
094 {
095 if (i > 0)
096 buffer.append(separator);
097
098 buffer.append(input[i]);
099 }
100
101 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 public static String replace(String string, String pattern, String replacement)
109 {
110 StringBuffer sbuf = new StringBuffer();
111 int index = string.indexOf(pattern);
112 int pos = 0;
113 int patternLength = pattern.length();
114 for(; index >= 0; index = string.indexOf(pattern, pos))
115 {
116 sbuf.append(string.substring(pos, index));
117 sbuf.append(replacement);
118 pos = index + patternLength;
119 }
120 sbuf.append(string.substring(pos));
121
122 return sbuf.toString();
123 }
124
125 }