| 1 | package ezvcard.util; |
| 2 | |
| 3 | import java.util.Collection; |
| 4 | import java.util.HashMap; |
| 5 | import java.util.Map; |
| 6 | |
| 7 | /* |
| 8 | Copyright (c) 2012-2023, Michael Angstadt |
| 9 | All rights reserved. |
| 10 | |
| 11 | Redistribution and use in source and binary forms, with or without |
| 12 | modification, are permitted provided that the following conditions are met: |
| 13 | |
| 14 | 1. Redistributions of source code must retain the above copyright notice, this |
| 15 | list of conditions and the following disclaimer. |
| 16 | 2. Redistributions in binary form must reproduce the above copyright notice, |
| 17 | this list of conditions and the following disclaimer in the documentation |
| 18 | and/or other materials provided with the distribution. |
| 19 | |
| 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 21 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 22 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 24 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 25 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 26 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 27 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 29 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | |
| 31 | The views and conclusions contained in the software and documentation are those |
| 32 | of the authors and should not be interpreted as representing official policies, |
| 33 | either expressed or implied, of the FreeBSD Project. |
| 34 | */ |
| 35 | |
| 36 | /** |
| 37 | * Helper class for dealing with strings. |
| 38 | * @author Michael Angstadt |
| 39 | */ |
| 40 | public final class StringUtils { |
| 41 | /** |
| 42 | * The local computer's newline character sequence. |
| 43 | */ |
| 44 | public static final String NEWLINE = System.getProperty("line.separator"); |
| 45 | |
| 46 | /** |
| 47 | * Joins a collection of values into a delimited list. |
| 48 | * @param collection the collection of values |
| 49 | * @param delimiter the delimiter (e.g. ",") |
| 50 | * @return the delimited string |
| 51 | */ |
| 52 | public static String join(Collection<?> collection, String delimiter) { |
| 53 | StringBuilder sb = new StringBuilder(); |
| 54 | |
| 55 | boolean first = true; |
| 56 | for (Object value : collection) { |
| 57 | if (!first) { |
| 58 | sb.append(delimiter); |
| 59 | } |
| 60 | |
| 61 | sb.append(value); |
| 62 | first = false; |
| 63 | } |
| 64 | |
| 65 | return sb.toString(); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Creates a copy of the given map, converting its keys and values to |
| 70 | * lowercase. |
| 71 | * @param map the map |
| 72 | * @return the copy with lowercase keys and values |
| 73 | */ |
| 74 | public static Map<String, String> toLowerCase(Map<String, String> map) { |
| 75 | Map<String, String> lowerCaseMap = new HashMap<>(map.size()); |
| 76 | for (Map.Entry<String, String> entry : map.entrySet()) { |
| 77 | String key = entry.getKey(); |
| 78 | key = (key == null) ? null : key.toLowerCase(); |
| 79 | |
| 80 | String value = entry.getValue(); |
| 81 | value = (value == null) ? null : value.toLowerCase(); |
| 82 | |
| 83 | lowerCaseMap.put(key, value); |
| 84 | } |
| 85 | return lowerCaseMap; |
| 86 | } |
| 87 | |
| 88 | private StringUtils() { |
| 89 | //hide |
| 90 | } |
| 91 | } |