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 * Contains miscellaneous string utilities.
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         * Joins a collection of values into a delimited list.
077         * @param collection the collection of values
078         * @param delimiter the delimiter (e.g. ",")
079         * @return the final string
080         */
081        public static <T> String join(Collection<T> collection, String delimiter) {
082                StringBuilder sb = new StringBuilder();
083                join(collection, delimiter, sb);
084                return sb.toString();
085        }
086
087        /**
088         * Joins a collection of values into a delimited list.
089         * @param collection the collection of values
090         * @param delimiter the delimiter (e.g. ",")
091         * @param sb the string builder to append onto
092         */
093        public static <T> void join(Collection<T> collection, String delimiter, StringBuilder sb) {
094                join(collection, delimiter, sb, new JoinCallback<T>() {
095                        public void handle(StringBuilder sb, T value) {
096                                sb.append(value);
097                        }
098                });
099        }
100
101        /**
102         * Joins a collection of values into a delimited list.
103         * @param collection the collection of values
104         * @param delimiter the delimiter (e.g. ",")
105         * @param join callback function to call on every element in the collection
106         * @return the final string
107         */
108        public static <T> String join(Collection<T> collection, String delimiter, JoinCallback<T> join) {
109                StringBuilder sb = new StringBuilder();
110                join(collection, delimiter, sb, join);
111                return sb.toString();
112        }
113
114        /**
115         * Joins a collection of values into a delimited list.
116         * @param collection the collection of values
117         * @param delimiter the delimiter (e.g. ",")
118         * @param sb the string builder to append onto
119         * @param join callback function to call on every element in the collection
120         */
121        public static <T> void join(Collection<T> collection, String delimiter, StringBuilder sb, JoinCallback<T> join) {
122                boolean first = true;
123                for (T element : collection) {
124                        if (first) {
125                                first = false;
126                        } else {
127                                sb.append(delimiter);
128                        }
129                        join.handle(sb, element);
130                }
131        }
132
133        /**
134         * Joins a map into a delimited list.
135         * @param map the map
136         * @param delimiter the delimiter (e.g. ",")
137         * @param join callback function to call on every element in the collection
138         * @return the final string
139         */
140        public static <K, V> String join(Map<K, V> map, String delimiter, final JoinMapCallback<K, V> join) {
141                return join(map.entrySet(), delimiter, new JoinCallback<Map.Entry<K, V>>() {
142                        public void handle(StringBuilder sb, Map.Entry<K, V> entry) {
143                                join.handle(sb, entry.getKey(), entry.getValue());
144                        }
145                });
146        }
147
148        /**
149         * Callback interface used with the
150         * {@link StringUtils#join(Collection, String, JoinCallback)} method.
151         * @author Michael Angstadt
152         * @param <T> the value type
153         */
154        public static interface JoinCallback<T> {
155                void handle(StringBuilder sb, T value);
156        }
157
158        /**
159         * Callback interface used with the
160         * {@link StringUtils#join(Map, String, JoinMapCallback)} method.
161         * @author Michael Angstadt
162         * @param <K> the key class
163         * @param <V> the value class
164         */
165        public static interface JoinMapCallback<K, V> {
166                void handle(StringBuilder sb, K key, V value);
167        }
168
169        private StringUtils() {
170                //hide
171        }
172}