1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.myfaces.trinidad.util;
20
21 import java.util.Iterator;
22
23 /**
24 * This class contains String utilities. The code was taken from
25 * Apache Commons, LANG project.
26 *
27 * Reason is to provide an alternate method to java.lang.String.replace().
28 * That method uses java.util.regex.Pattern.matcher(), which has a PERF
29 * issue.
30 *
31 */
32 public final class StringUtils
33 {
34 private StringUtils()
35 {
36 // no-op
37 }
38
39 public static String replace(String text, String searchString, String replacement)
40 {
41 return replace(text, searchString, replacement, -1);
42 }
43
44 // making this public, doesn't hurt... but we don't need it, now
45 private static boolean isEmpty(String str)
46 {
47 return str == null || str.length() == 0;
48 }
49
50 // making this public, doesn't hurt... but we don't need it, now
51 private static String replace(String text, String searchString, String replacement, int max)
52 {
53 if (isEmpty(text) || isEmpty(searchString) || replacement == null || max == 0)
54 {
55 return text;
56 }
57 int start = 0;
58 int end = text.indexOf(searchString, start);
59
60 if (end == -1)
61 {
62 return text;
63 }
64 int replLength = searchString.length();
65 int increase = replacement.length() - replLength;
66 increase = (increase < 0 ? 0 : increase);
67 increase *= (max < 0 ? 16 : (max > 64 ? 64 : max));
68 StringBuffer buf = new StringBuffer(text.length() + increase);
69 while (end != -1)
70 {
71 buf.append(text.substring(start, end)).append(replacement);
72 start = end + replLength;
73
74 if (--max == 0)
75 {
76 break;
77 }
78 end = text.indexOf(searchString, start);
79 }
80 buf.append(text.substring(start));
81 return buf.toString();
82 }
83
84 /**
85 * <p>Joins the elements of the provided array into a single String
86 * containing the provided list of elements.</p>
87 *
88 * <p>No delimiter is added before or after the list.
89 * Null objects or empty strings within the array are represented by
90 * empty strings.</p>
91 *
92 * <pre>
93 * StringUtils.join(null, *) = null
94 * StringUtils.join([], *) = ""
95 * StringUtils.join([null], *) = ""
96 * StringUtils.join(["a", "b", "c"], ';') = "a;b;c"
97 * StringUtils.join(["a", "b", "c"], null) = "abc"
98 * StringUtils.join([null, "", "a"], ';') = ";;a"
99 * </pre>
100 *
101 * @param array the array of values to join together, may be null
102 * @param separator the separator character to use
103 * @return the joined String, <code>null</code> if null array input
104 */
105
106 public static String join(Object[] array, char separator)
107 {
108 if (array == null)
109 {
110 return null;
111 }
112 int arraySize = array.length;
113 int bufSize = (arraySize == 0? 0: ((array[0] == null? 16: array[0].toString().length()) + 1) * arraySize);
114 StringBuilder buf = new StringBuilder(bufSize);
115
116 for (int i = 0; i < arraySize; i++)
117 {
118 if (i > 0)
119 {
120 buf.append(separator);
121 }
122 if (array[i] != null)
123 {
124 buf.append(array[i]);
125 }
126 }
127 return buf.toString();
128 }
129
130 /**
131 * <p>Joins the elements of the provided <code>Iterator</code> into
132 * a single String containing the provided elements.</p>
133 *
134 * <p>No delimiter is added before or after the list. Null objects or empty
135 * strings within the iteration are represented by empty strings.</p>
136 *
137 * <p>See the examples here: {@link #join(Object[],char)}. </p>
138 *
139 * @param iterator the <code>Iterator</code> of values to join together, may be null
140 * @param separator the separator character to use
141 * @return the joined String, <code>null</code> if null iterator input
142 */
143 public static String join(Iterator iterator, char separator)
144 {
145 if (iterator == null)
146 {
147 return null;
148 }
149
150 StringBuilder buf = new StringBuilder(256); // Java default is 16, probably too small
151
152 while (iterator.hasNext())
153 {
154 Object obj = iterator.next();
155 if (obj != null)
156 {
157 buf.append(obj);
158 }
159 if (iterator.hasNext())
160 {
161 buf.append(separator);
162 }
163 }
164 return buf.toString();
165 }
166 }