View Javadoc

1   /*
2    * Copyright 2004-2005 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * the License at
7    * 
8    * http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package net.sf.composite.util;
17  
18  import java.lang.reflect.Array;
19  import java.util.Collection;
20  
21  /***
22   * @author Matt Sgarlata
23   * @since Mar 11, 2005
24   */
25  public class StringUtils {
26  	
27  	public static String capitalize(String string) {
28  		return 
29  			string.substring(0, 1).toUpperCase() +
30  			string.substring(1, string.length());
31  	}
32  	
33  	public static String join(Object values, String separator) {
34  		if (values == null) {
35  			return null;
36  		}
37  		else if (values instanceof Collection) {
38  			Collection valuesCollection = (Collection) values;
39  			return join(
40  				valuesCollection.toArray(new Object[valuesCollection.size()]),
41  				separator);
42  		}
43  		else if (values.getClass().isArray()) {
44  			StringBuffer buffer = new StringBuffer();
45  			int length = Array.getLength(values);
46  			for (int i = 0; i < length; i++) {
47  				buffer.append(Array.get(values, i));
48  				if (i < length - 1) {
49  					buffer.append(separator);
50  				}
51  			}
52  			return buffer.toString();
53  		}
54  		else {
55  			throw new IllegalArgumentException("Cannot join objects of class '" + values.getClass().getName() + "'");
56  		}
57  	}
58  
59  }