001/*
002 * HA-JDBC: High-Availability JDBC
003 * Copyright (c) 2004-2007 Paul Ferraro
004 * 
005 * This library is free software; you can redistribute it and/or modify it 
006 * under the terms of the GNU Lesser General Public License as published by the 
007 * Free Software Foundation; either version 2.1 of the License, or (at your 
008 * option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful, but WITHOUT
011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 
013 * for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public License
016 * along with this library; if not, write to the Free Software Foundation, 
017 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018 * 
019 * Contact: ferraro@users.sourceforge.net
020 */
021package net.sf.hajdbc.util;
022
023import java.util.Arrays;
024import java.util.Collection;
025import java.util.Iterator;
026
027/**
028 * A set of String utilities.
029 * @author Paul Ferraro
030 * @since 2.0
031 */
032public final class Strings
033{
034        public static final String ANY = "%"; //$NON-NLS-1$
035        public static final String COMMA = ","; //$NON-NLS-1$
036        public static final String DASH = "-"; //$NON-NLS-1$
037        public static final String DOT = "."; //$NON-NLS-1$
038        public static final String EMPTY = ""; //$NON-NLS-1$
039        public static final String PADDED_COMMA = ", "; //$NON-NLS-1$
040        public static final String QUESTION = "?"; //$NON-NLS-1$
041        public static final String UNDERSCORE = "_"; //$NON-NLS-1$
042        
043        /**
044         * Performs the reverse of a split operation, joining the elements of the specified collection using the specified delimiter.
045         * @param collection a collection of strings
046         * @param delimiter a string to insert between each collection element
047         * @return a new String
048         */
049        public static String join(Collection<String> collection, String delimiter)
050        {
051                StringBuilder builder = new StringBuilder();
052                
053                Iterator<String> elements = collection.iterator();
054                
055                while (elements.hasNext())
056                {
057                        builder.append(elements.next());
058                        
059                        if (elements.hasNext())
060                        {
061                                builder.append(delimiter);
062                        }
063                }
064                
065                return builder.toString();
066        }
067        
068        /**
069         * Performs the reverse of a split operation, joining the elements of the specified collection using the specified delimiter.
070         * @param strings an array of strings
071         * @param delimiter a string to insert between each array element
072         * @return a new String
073         */
074        public static String join(String[] strings, String delimiter)
075        {
076                return join(Arrays.asList(strings), delimiter);
077        }
078        
079        private Strings()
080        {
081                // Hide constructor
082        }
083}