001package ezvcard.util; 002 003import java.util.HashMap; 004import java.util.List; 005import java.util.Map; 006import java.util.Objects; 007import java.util.stream.Collectors; 008import java.util.stream.IntStream; 009 010/* 011 Copyright (c) 2012-2026, Michael Angstadt 012 All rights reserved. 013 014 Redistribution and use in source and binary forms, with or without 015 modification, are permitted provided that the following conditions are met: 016 017 1. Redistributions of source code must retain the above copyright notice, this 018 list of conditions and the following disclaimer. 019 2. Redistributions in binary form must reproduce the above copyright notice, 020 this list of conditions and the following disclaimer in the documentation 021 and/or other materials provided with the distribution. 022 023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 024 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 025 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 026 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 027 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 028 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 029 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 030 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 031 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 032 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 033 034 The views and conclusions contained in the software and documentation are those 035 of the authors and should not be interpreted as representing official policies, 036 either expressed or implied, of the FreeBSD Project. 037 */ 038 039/** 040 * Helper class for dealing with strings. 041 * @author Michael Angstadt 042 */ 043public final class StringUtils { 044 /** 045 * The local computer's newline character sequence. 046 */ 047 public static final String NEWLINE = System.lineSeparator(); 048 049 /** 050 * Creates a copy of the given map, converting its keys and values to 051 * lowercase. 052 * @param map the map 053 * @return the copy with lowercase keys and values 054 */ 055 static Map<String, String> mapToLowercase(Map<String, String> map) { 056 Map<String, String> lowerMap = new HashMap<>(map.size()); 057 map.forEach((key, value) -> lowerMap.put(toLowerCase(key), toLowerCase(value))); 058 return lowerMap; 059 } 060 061 private static String toLowerCase(String s) { 062 return (s == null) ? null : s.toLowerCase(); 063 } 064 065 /** 066 * Compares two strings using case-insensitive equality, also checking for 067 * null. 068 * @param a the first string (can be null) 069 * @param b the second string (can be null) 070 * @return true if they are equal, false if not 071 */ 072 public static boolean equalsIgnoreCase(String a, String b) { 073 return (a == null) ? b == null : a.equalsIgnoreCase(b); 074 } 075 076 /** 077 * Compares two string-based maps using case-insensitive equality, also 078 * checking for null. 079 * @param a the first map (can be null) 080 * @param b the second map (can be null) 081 * @return true if they are equal, false if not 082 */ 083 public static boolean equalsIgnoreCase(Map<String, String> a, Map<String, String> b) { 084 if (a == null) return b == null; 085 if (b == null) return a == null; 086 if (a.size() != b.size()) return false; 087 088 return mapToLowercase(a).equals(mapToLowercase(b)); 089 } 090 091 /** 092 * Compares two lists of strings using case-insensitive equality and 093 * ignoring order. Also checks for null. 094 * @param a the first list (can be null) 095 * @param b the second list (can be null) 096 * @return true if they are equal, false if not 097 */ 098 public static boolean equalsIgnoreCaseIgnoreOrder(List<String> a, List<String> b) { 099 if (a == null) return b == null; 100 if (b == null) return a == null; 101 if (a.size() != b.size()) return false; 102 103 return listToLowerCaseAndSort(a).equals(listToLowerCaseAndSort(b)); 104 } 105 106 private static List<String> listToLowerCaseAndSort(List<String> values) { 107 //@formatter:off 108 return values.stream() 109 .map(String::toLowerCase) 110 .sorted() 111 .collect(Collectors.toList()); 112 //@formatter:on 113 } 114 115 /** 116 * Calls {@link Objects#hash}, replacing all String objects with lowercase 117 * versions. 118 * @param objects the objects to hash 119 * @return the hash code 120 */ 121 public static int hashIgnoreCase(Object... objects) { 122 //@formatter:off 123 IntStream.range(0, objects.length) 124 .filter(i -> objects[i] instanceof String) 125 .forEach(i -> objects[i] = ((String) objects[i]).toLowerCase()); 126 //@formatter:on 127 128 return Objects.hash(objects); 129 } 130 131 /** 132 * Generates a case-insensitive hash code of a string-based map. 133 * @param map the map (can be null) 134 * @return the hash code or 0 if the map is null 135 */ 136 public static int hashIgnoreCase(Map<String, String> map) { 137 return (map == null) ? 0 : mapToLowercase(map).hashCode(); 138 } 139 140 private StringUtils() { 141 //hide 142 } 143}