001package biweekly.util;
002
003import java.util.Collection;
004import java.util.Map;
005
006/*
007 Copyright (c) 2013-2015, 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 * Helper class for dealing with strings.
033 * @author Michael Angstadt
034 */
035public 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         * Trims the whitespace off the left side of a string.
043         * @param string the string to trim
044         * @return the trimmed string
045         */
046        public static String ltrim(String string) {
047                if (string == null) {
048                        return null;
049                }
050
051                int i;
052                for (i = 0; i < string.length() && Character.isWhitespace(string.charAt(i)); i++) {
053                        //do nothing
054                }
055                return (i == string.length()) ? "" : string.substring(i);
056        }
057
058        /**
059         * Trims the whitespace off the right side of a string.
060         * @param string the string to trim
061         * @return the trimmed string
062         */
063        public static String rtrim(String string) {
064                if (string == null) {
065                        return null;
066                }
067
068                int i;
069                for (i = string.length() - 1; i >= 0 && Character.isWhitespace(string.charAt(i)); i--) {
070                        //do nothing
071                }
072                return (i == 0) ? "" : string.substring(0, i + 1);
073        }
074
075        /**
076         * Creates a string consisting of "count" occurrences of char "c".
077         * @param c the character to repeat
078         * @param count the number of times to repeat the character
079         * @return the resulting string
080         */
081        public static String repeat(char c, int count) {
082                if (count <= 0) {
083                        return "";
084                }
085
086                StringBuilder sb = new StringBuilder(count);
087                for (int i = 0; i < count; i++) {
088                        sb.append(c);
089                }
090                return sb.toString();
091        }
092
093        /**
094         * Creates a string consisting of "count" occurrences of string "str".
095         * @param str the string to repeat
096         * @param count the number of times to repeat the string
097         * @return the resulting string
098         */
099        public static String repeat(String str, int count) {
100                if (count <= 0) {
101                        return "";
102                }
103
104                StringBuilder sb = new StringBuilder(count * str.length());
105                for (int i = 0; i < count; i++) {
106                        sb.append(str);
107                }
108                return sb.toString();
109        }
110
111        /**
112         * Joins a collection of values into a delimited list.
113         * @param collection the collection of values
114         * @param delimiter the delimiter (e.g. ",")
115         * @return the final string
116         */
117        public static <T> String join(Collection<T> collection, String delimiter) {
118                StringBuilder sb = new StringBuilder();
119                join(collection, delimiter, sb);
120                return sb.toString();
121        }
122
123        /**
124         * Joins a collection of values into a delimited list.
125         * @param collection the collection of values
126         * @param delimiter the delimiter (e.g. ",")
127         * @param sb the string builder to append onto
128         */
129        public static <T> void join(Collection<T> collection, String delimiter, StringBuilder sb) {
130                join(collection, delimiter, sb, new JoinCallback<T>() {
131                        public void handle(StringBuilder sb, T value) {
132                                sb.append(value);
133                        }
134                });
135        }
136
137        /**
138         * Joins a collection of values into a delimited list.
139         * @param collection the collection of values
140         * @param delimiter the delimiter (e.g. ",")
141         * @param join callback function to call on every element in the collection
142         * @return the final string
143         */
144        public static <T> String join(Collection<T> collection, String delimiter, JoinCallback<T> join) {
145                StringBuilder sb = new StringBuilder();
146                join(collection, delimiter, sb, join);
147                return sb.toString();
148        }
149
150        /**
151         * Joins a collection of values into a delimited list.
152         * @param collection the collection of values
153         * @param delimiter the delimiter (e.g. ",")
154         * @param sb the string builder to append onto
155         * @param join callback function to call on every element in the collection
156         */
157        public static <T> void join(Collection<T> collection, String delimiter, StringBuilder sb, JoinCallback<T> join) {
158                boolean first = true;
159                for (T element : collection) {
160                        if (first) {
161                                first = false;
162                        } else {
163                                sb.append(delimiter);
164                        }
165                        join.handle(sb, element);
166                }
167        }
168
169        /**
170         * Joins a map into a delimited list.
171         * @param map the map
172         * @param delimiter the delimiter (e.g. ",")
173         * @param join callback function to call on every element in the collection
174         * @return the final string
175         */
176        public static <K, V> String join(Map<K, V> map, String delimiter, final JoinMapCallback<K, V> join) {
177                return join(map.entrySet(), delimiter, new JoinCallback<Map.Entry<K, V>>() {
178                        public void handle(StringBuilder sb, Map.Entry<K, V> entry) {
179                                join.handle(sb, entry.getKey(), entry.getValue());
180                        }
181                });
182        }
183
184        /**
185         * Callback interface used with various {@code StringUtils.join()} methods.
186         * @author Michael Angstadt
187         * @param <T> the value type
188         */
189        public static interface JoinCallback<T> {
190                void handle(StringBuilder sb, T value);
191        }
192
193        /**
194         * Callback interface used with the
195         * {@link #join(Map, String, JoinMapCallback)} method.
196         * @author Michael Angstadt
197         * @param <K> the key class
198         * @param <V> the value class
199         */
200        public static interface JoinMapCallback<K, V> {
201                void handle(StringBuilder sb, K key, V value);
202        }
203
204        private StringUtils() {
205                //hide
206        }
207}