View Javadoc

1   /*--------------------------------------------------------------------------
2    *  Copyright 2007 utgenome.org
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of 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,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *--------------------------------------------------------------------------*/
16  //--------------------------------------
17  // GenomeBrowser Project
18  //
19  // StringUtil.java
20  // Since: Jun 26, 2007
21  //
22  // $URL$ 
23  // $Author$
24  //--------------------------------------
25  package org.utgenome.gwt.utgb.client.util;
26  
27  import java.util.ArrayList;
28  import java.util.List;
29  
30  /**
31   * Utiltiles for manipulating strings in GWT client codes
32   * 
33   * @author leo
34   * 
35   */
36  public class StringUtil {
37  
38  	/**
39  	 * Non constractable
40  	 */
41  	private StringUtil() {
42  	}
43  
44  	/**
45  	 * Join the given element list with the specified separator
46  	 * 
47  	 * @param elementList
48  	 *            a list of strings to join
49  	 * @param separator
50  	 *            e.g., ",", " ", etc.
51  	 * @return the concatination of the strings in the elementList, separated by the separator
52  	 */
53  	public static String join(String[] elementList, String separator) {
54  		StringBuffer b = new StringBuffer();
55  		for (int i = 0; i < elementList.length - 1; i++) {
56  			b.append(elementList[i]);
57  			b.append(separator); // white space
58  		}
59  		b.append(elementList[elementList.length - 1]);
60  		return b.toString();
61  	}
62  
63  	public static String join(List<String> elementList, String separator) {
64  		if (elementList.isEmpty())
65  			return "";
66  		StringBuffer b = new StringBuffer();
67  		for (int i = 0; i < elementList.size() - 1; i++) {
68  			b.append(elementList.get(i));
69  			b.append(separator); // white space
70  		}
71  		b.append(elementList.get(elementList.size() - 1));
72  		return b.toString();
73  	}
74  
75  	public static String joinIterable(Iterable<String> element, String separator) {
76  		List<String> list = new ArrayList<String>();
77  		for (String each : element) {
78  			list.add(each);
79  		}
80  		return join(list, separator);
81  	}
82  
83  	/**
84  	 * 
85  	 * @param elementList
86  	 * @return
87  	 */
88  	public static String joinWithWS(String[] elementList) {
89  		return join(elementList, " ");
90  	}
91  
92  	public static String unquote(String s) {
93  		if (s.startsWith("\"") && s.endsWith("\""))
94  			return s.substring(1, s.length() - 1);
95  		else
96  			return s;
97  	}
98  
99  	/**
100 	 * Convert a string representation of an integer with comma into an int value
101 	 * 
102 	 * @param sInt
103 	 * @return
104 	 */
105 	public static int toInt(String sInt) {
106 		if (sInt == null)
107 			throw new NullPointerException();
108 		String intWithoutComma = sInt.replaceAll("[ ,]", "");
109 		return Integer.parseInt(intWithoutComma);
110 	}
111 
112 	/**
113 	 * insert commas to the given number for the readability
114 	 * 
115 	 * @param number
116 	 * @return
117 	 */
118 	public static String formatNumber(int number) {
119 		StringBuilder s = new StringBuilder();
120 		String intValue = Integer.toString(number);
121 
122 		final int len = intValue.length();
123 		for (int i = 0; i < len; ++i) {
124 			s.append(intValue.charAt(i));
125 
126 			int digit = len - i - 1;
127 			if (digit != 0 && (digit % 3 == 0)) {
128 				s.append(',');
129 			}
130 		}
131 		return s.toString();
132 	}
133 
134 }