001/*
002 * HA-JDBC: High-Availability JDBC
003 * Copyright (C) 2012  Paul Ferraro
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Lesser General Public License as published by
007 * the Free Software Foundation, either version 3 of the License, or
008 * (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Lesser General Public License for more details.
014 *
015 * You should have received a copy of the GNU Lesser General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018package net.sf.hajdbc.util;
019
020import java.util.Arrays;
021import java.util.Collection;
022import java.util.Iterator;
023
024/**
025 * String utility methods.
026 * @author Paul Ferraro
027 */
028public final class Strings
029{
030        public static final String ANY = "%"; //$NON-NLS-1$
031        public static final String COMMA = ","; //$NON-NLS-1$
032        public static final String DASH = "-"; //$NON-NLS-1$
033        public static final String DOT = "."; //$NON-NLS-1$
034        public static final String EMPTY = ""; //$NON-NLS-1$
035        public static final String PADDED_COMMA = ", "; //$NON-NLS-1$
036        public static final String QUESTION = "?"; //$NON-NLS-1$
037        public static final String UNDERSCORE = "_"; //$NON-NLS-1$
038        public static final String TAB = "\t";
039        public static final String NEW_LINE = SystemProperties.getSystemProperty("line.separator");
040        public static final String FILE_SEPARATOR = SystemProperties.getSystemProperty("file.separator");
041        public static final String PATH_SEPARATOR = SystemProperties.getSystemProperty("path.separator");
042        public static final String USER_HOME = SystemProperties.getSystemProperty("user.home");
043        public static final String HA_JDBC_HOME = USER_HOME + FILE_SEPARATOR + ".ha-jdbc";
044        
045        /**
046         * Performs the reverse of a split operation, joining the elements of the specified collection using the specified delimiter.
047         * @param collection a collection of strings
048         * @param delimiter a string to insert between each collection element
049         * @return a new String
050         */
051        public static StringBuilder join(StringBuilder builder, Collection<String> collection, String delimiter)
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;
066        }
067        
068        /**
069         * Performs the reverse of a split operation, joining the elements of the specified collection using the specified delimiter.
070         * @param collection a collection of strings
071         * @param delimiter a string to insert between each collection element
072         * @return a new String
073         */
074        public static String join(Collection<String> collection, String delimiter)
075        {
076                return join(new StringBuilder(), collection, delimiter).toString();
077        }
078        
079        /**
080         * Performs the reverse of a split operation, joining the elements of the specified collection using the specified delimiter.
081         * @param strings an array of strings
082         * @param delimiter a string to insert between each array element
083         * @return a new String
084         */
085        public static String join(String[] strings, String delimiter)
086        {
087                return join(Arrays.asList(strings), delimiter);
088        }
089        
090        /**
091         * Performs the reverse of a split operation, joining the elements of the specified collection using the specified delimiter.
092         * @param strings an array of strings
093         * @param delimiter a string to insert between each array element
094         * @return a new String
095         */
096        public static StringBuilder join(StringBuilder builder, String[] strings, String delimiter)
097        {
098                return join(builder, Arrays.asList(strings), delimiter);
099        }
100        
101        private Strings()
102        {
103                // Hide constructor
104        }
105}