001    package biweekly.util;
002    
003    import java.util.Collection;
004    import java.util.Map;
005    
006    /*
007     Copyright (c) 2013, Michael Angstadt
008     All rights reserved.
009    
010     Redistribution and use in source and binary forms, with or without
011     modification, are permitted provided that the following conditions are met: 
012    
013     1. Redistributions of source code must retain the above copyright notice, this
014     list of conditions and the following disclaimer. 
015     2. Redistributions in binary form must reproduce the above copyright notice,
016     this list of conditions and the following disclaimer in the documentation
017     and/or other materials provided with the distribution. 
018    
019     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020     ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023     ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
026     ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028     SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029     */
030    
031    /**
032     * Contains miscellaneous string utilities.
033     * @author Michael Angstadt
034     */
035    public class StringUtils {
036            /**
037             * The local computer's newline character sequence.
038             */
039            public static final String NEWLINE = System.getProperty("line.separator");
040    
041            /**
042             * Joins a collection of values into a delimited list.
043             * @param collection the collection of values
044             * @param delimiter the delimiter (e.g. ",")
045             * @return the final string
046             */
047            public static <T> String join(Collection<T> collection, String delimiter) {
048                    StringBuilder sb = new StringBuilder();
049                    join(collection, delimiter, sb);
050                    return sb.toString();
051            }
052    
053            /**
054             * Joins a collection of values into a delimited list.
055             * @param collection the collection of values
056             * @param delimiter the delimiter (e.g. ",")
057             * @param sb the string builder to append onto
058             */
059            public static <T> void join(Collection<T> collection, String delimiter, StringBuilder sb) {
060                    join(collection, delimiter, sb, new JoinCallback<T>() {
061                            public void handle(StringBuilder sb, T value) {
062                                    sb.append(value);
063                            }
064                    });
065            }
066    
067            /**
068             * Joins a collection of values into a delimited list.
069             * @param collection the collection of values
070             * @param delimiter the delimiter (e.g. ",")
071             * @param join callback function to call on every element in the collection
072             * @return the final string
073             */
074            public static <T> String join(Collection<T> collection, String delimiter, JoinCallback<T> join) {
075                    StringBuilder sb = new StringBuilder();
076                    join(collection, delimiter, sb, join);
077                    return sb.toString();
078            }
079    
080            /**
081             * Joins a collection of values into a delimited list.
082             * @param collection the collection of values
083             * @param delimiter the delimiter (e.g. ",")
084             * @param sb the string builder to append onto
085             * @param join callback function to call on every element in the collection
086             */
087            public static <T> void join(Collection<T> collection, String delimiter, StringBuilder sb, JoinCallback<T> join) {
088                    boolean first = true;
089                    for (T element : collection) {
090                            if (first) {
091                                    first = false;
092                            } else {
093                                    sb.append(delimiter);
094                            }
095                            join.handle(sb, element);
096                    }
097            }
098    
099            /**
100             * Joins a map into a delimited list.
101             * @param map the map
102             * @param delimiter the delimiter (e.g. ",")
103             * @param join callback function to call on every element in the collection
104             * @return the final string
105             */
106            public static <K, V> String join(Map<K, V> map, String delimiter, final JoinMapCallback<K, V> join) {
107                    return join(map.entrySet(), delimiter, new JoinCallback<Map.Entry<K, V>>() {
108                            public void handle(StringBuilder sb, Map.Entry<K, V> entry) {
109                                    join.handle(sb, entry.getKey(), entry.getValue());
110                            }
111                    });
112            }
113    
114            /**
115             * Callback interface used with the
116             * {@link StringUtils#join(Collection, String, JoinCallback)} method.
117             * @author Michael Angstadt
118             * @param <T> the value type
119             */
120            public static interface JoinCallback<T> {
121                    void handle(StringBuilder sb, T value);
122            }
123    
124            /**
125             * Callback interface used with the
126             * {@link StringUtils#join(Map, String, JoinMapCallback)} method.
127             * @author Michael Angstadt
128             * @param <K> the key class
129             * @param <V> the value class
130             */
131            public static interface JoinMapCallback<K, V> {
132                    void handle(StringBuilder sb, K key, V value);
133            }
134    
135            private StringUtils() {
136                    //hide
137            }
138    }