|
| 1 | +package com.jgcomptech.tools; |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | +import java.util.function.Predicate; |
| 5 | + |
| 6 | +public final class CollectionUtils { |
| 7 | + /** |
| 8 | + * Converts a map into a delimited string value. |
| 9 | + * A '=' seperates the keys and values and a '&' seperates the key pairs. |
| 10 | + * @param stringMap map to convert |
| 11 | + * @return string representation of map |
| 12 | + */ |
| 13 | + public static String convertMapToString(Map<String, String> stringMap) { |
| 14 | + final StringBuilder sb = new StringBuilder(); |
| 15 | + final char KeySeparator = '='; |
| 16 | + final char PairSeparator = '&'; |
| 17 | + for(final Map.Entry<String, String> pair:stringMap.entrySet()) |
| 18 | + { |
| 19 | + sb.append(pair.getKey()); |
| 20 | + sb.append(KeySeparator); |
| 21 | + sb.append(pair.getValue()); |
| 22 | + sb.append(PairSeparator); |
| 23 | + } |
| 24 | + return sb.toString().substring(0, sb.length() - 1); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Converts a delimited string value into a map. |
| 29 | + * Expects that '=' seperates the keys and values and a '&' seperates the key pairs. |
| 30 | + * @param value map to convert |
| 31 | + * @return string representation of map |
| 32 | + */ |
| 33 | + public static Map<String, String> convertStringToMap(String value) { |
| 34 | + final Map<String, String> myMap = new HashMap<>(); |
| 35 | + final String KeySeparator = "="; |
| 36 | + final String PairSeparator = "&"; |
| 37 | + final String[] pairs = value.split(PairSeparator); |
| 38 | + for(final String pair : pairs) { |
| 39 | + final String[] keyValue = pair.split(KeySeparator); |
| 40 | + myMap.put(keyValue[0], keyValue[1]); |
| 41 | + } |
| 42 | + return myMap; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Checks if an item exists in a HashSet that matches the specified Predicate. |
| 47 | + * @param set the HashSet to check against |
| 48 | + * @param condition the Predicate to check |
| 49 | + * @param <T> the type of objects in the HashSet |
| 50 | + * @return true if condition is true |
| 51 | + * @throws IllegalArgumentException if HashSet or Predicate is null |
| 52 | + */ |
| 53 | + public static <T> boolean doesItemExistInHashSet(HashSet<T> set, Predicate<T> condition) { |
| 54 | + if(set == null) throw new IllegalArgumentException("HashSet cannot be null!"); |
| 55 | + if(condition == null) throw new IllegalArgumentException("Predicate cannot be null!"); |
| 56 | + for(final T object : set) if (condition.test(object)) return true; |
| 57 | + return false; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Checks if a value exists in a HashMap that matches the specified Predicate. |
| 62 | + * @param map the HashMap to check against |
| 63 | + * @param condition the Predicate to check |
| 64 | + * @param <K> the type of the Key in the HashMap |
| 65 | + * @param <V> the type of the Value in the HashMap |
| 66 | + * @return true if condition is true |
| 67 | + * @throws IllegalArgumentException if HashMap or Predicate is null |
| 68 | + */ |
| 69 | + public static <K, V> boolean doesItemExistInHashMap(HashMap<K, V> map, Predicate<V> condition) { |
| 70 | + if(map == null) throw new IllegalArgumentException("HashMap cannot be null!"); |
| 71 | + if(condition == null) throw new IllegalArgumentException("Predicate cannot be null!"); |
| 72 | + for(final Map.Entry<K, V> entry : map.entrySet()) if (condition.test(entry.getValue())) return true; |
| 73 | + return false; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Returns item in HashSet that matches the specified Predicate |
| 78 | + * @param set the HashSet to check against |
| 79 | + * @param condition the Predicate to check |
| 80 | + * @param <T> the type of objects in the HashSet |
| 81 | + * @return an Optional containing the matching object, Empty if not found |
| 82 | + * @throws IllegalArgumentException if HashSet or Predicate is null |
| 83 | + */ |
| 84 | + public static <T> Optional<T> getItemInHashSet(HashSet<T> set, Predicate<T> condition) { |
| 85 | + if(set == null) throw new IllegalArgumentException("HashSet cannot be null!"); |
| 86 | + if(condition == null) throw new IllegalArgumentException("Predicate cannot be null!"); |
| 87 | + for(final T object : set) if (condition.test(object)) return Optional.of(object); |
| 88 | + return Optional.empty(); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Returns a HashSet of values in a HashMap that match the specified Predicate |
| 93 | + * @param map the HashMap to check against |
| 94 | + * @param condition the Predicate to check |
| 95 | + * @param <K> the type of the Key in the HashMap |
| 96 | + * @param <V> the type of the Value in the HashMap |
| 97 | + * @return a HashSet of values matching the predicate, empty HashSet if no results found |
| 98 | + * @throws IllegalArgumentException if HashMap or Predicate is null |
| 99 | + */ |
| 100 | + public static <K, V> HashSet<V> getValuesInHashMap(HashMap<K, V> map, Predicate<V> condition) { |
| 101 | + final HashSet<V> matchingValues = new HashSet<>(); |
| 102 | + |
| 103 | + if(map == null) throw new IllegalArgumentException("HashMap cannot be null!"); |
| 104 | + if(condition == null) throw new IllegalArgumentException("Predicate cannot be null!"); |
| 105 | + |
| 106 | + map.forEach((key, value) -> { if(condition.test(value)) matchingValues.add(value); }); |
| 107 | + |
| 108 | + return matchingValues; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Returns the first item found in a HashMap that matches the specified Predicate |
| 113 | + * @param map the HashMap to check against |
| 114 | + * @param condition the Predicate to check |
| 115 | + * @param <K> the type of the Key in the HashMap |
| 116 | + * @param <V> the type of the Value in the HashMap |
| 117 | + * @return an Optional containing the matching value, Empty if no results found |
| 118 | + * @throws IllegalArgumentException if HashMap or Predicate is null |
| 119 | + */ |
| 120 | + public static <K, V> Optional<V> getValueInHashMap(HashMap<K, V> map, Predicate<V> condition) { |
| 121 | + V matchingValue = null; |
| 122 | + |
| 123 | + if(map == null) throw new IllegalArgumentException("HashMap cannot be null!"); |
| 124 | + if(condition == null) throw new IllegalArgumentException("Predicate cannot be null!"); |
| 125 | + |
| 126 | + for(final Map.Entry<K, V> entry : map.entrySet()) { |
| 127 | + if(condition.test(entry.getValue())) { |
| 128 | + matchingValue = entry.getValue(); |
| 129 | + break; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + return matchingValue == null ? Optional.empty() : Optional.of(matchingValue); |
| 134 | + } |
| 135 | +} |
0 commit comments