01: /*
02: * HA-JDBC: High-Availability JDBC
03: * Copyright (c) 2004-2007 Paul Ferraro
04: *
05: * This library is free software; you can redistribute it and/or modify it
06: * under the terms of the GNU Lesser General Public License as published by the
07: * Free Software Foundation; either version 2.1 of the License, or (at your
08: * option) any later version.
09: *
10: * This library is distributed in the hope that it will be useful, but WITHOUT
11: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13: * for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public License
16: * along with this library; if not, write to the Free Software Foundation,
17: * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18: *
19: * Contact: ferraro@users.sourceforge.net
20: */
21: package net.sf.hajdbc.util;
22:
23: import java.util.Arrays;
24: import java.util.Collection;
25: import java.util.Iterator;
26:
27: /**
28: * A set of String utilities.
29: * @author Paul Ferraro
30: * @since 2.0
31: */
32: public final class Strings {
33: public static final String ANY = "%"; //$NON-NLS-1$
34: public static final String COMMA = ","; //$NON-NLS-1$
35: public static final String DASH = "-"; //$NON-NLS-1$
36: public static final String DOT = "."; //$NON-NLS-1$
37: public static final String EMPTY = ""; //$NON-NLS-1$
38: public static final String PADDED_COMMA = ", "; //$NON-NLS-1$
39: public static final String QUESTION = "?"; //$NON-NLS-1$
40: public static final String UNDERSCORE = "_"; //$NON-NLS-1$
41:
42: /**
43: * Performs the reverse of a split operation, joining the elements of the specified collection using the specified delimiter.
44: * @param collection a collection of strings
45: * @param delimiter a string to insert between each collection element
46: * @return a new String
47: */
48: public static String join(Collection<String> collection,
49: String delimiter) {
50: StringBuilder builder = new StringBuilder();
51:
52: Iterator<String> elements = collection.iterator();
53:
54: while (elements.hasNext()) {
55: builder.append(elements.next());
56:
57: if (elements.hasNext()) {
58: builder.append(delimiter);
59: }
60: }
61:
62: return builder.toString();
63: }
64:
65: /**
66: * Performs the reverse of a split operation, joining the elements of the specified collection using the specified delimiter.
67: * @param strings an array of strings
68: * @param delimiter a string to insert between each array element
69: * @return a new String
70: */
71: public static String join(String[] strings, String delimiter) {
72: return join(Arrays.asList(strings), delimiter);
73: }
74:
75: private Strings() {
76: // Hide constructor
77: }
78: }
|