@Test public void testImmutableSetDoesNotChangeOnSourceChange() throws Exception { Set<String> input = new HashSet<>(Collections.singleton("key")); Set<String> output = CollectionUtils.immutableSetOrNull(input); input.add("new_key"); assertThat(output, not(sameInstance(input))); assertThat(output, not(contains("new_key"))); }
public static <T> Set<T> immutableSetOrNull(Set<T> input) { return Optional.ofNullable(input).map(HashSet::new).map(Collections::unmodifiableSet).orElse(null); }
CollectionUtils { public static <T> Set<T> immutableSetOrNull(Set<T> input) { return Optional.ofNullable(input).map(HashSet::new).map(Collections::unmodifiableSet).orElse(null); } }
CollectionUtils { public static <T> Set<T> immutableSetOrNull(Set<T> input) { return Optional.ofNullable(input).map(HashSet::new).map(Collections::unmodifiableSet).orElse(null); } private  CollectionUtils(); }
CollectionUtils { public static <T> Set<T> immutableSetOrNull(Set<T> input) { return Optional.ofNullable(input).map(HashSet::new).map(Collections::unmodifiableSet).orElse(null); } private  CollectionUtils(); static T[] nullableArrayCopy(T[] input); static double[] nullableArrayCopy(double... input); static long[] nullableArrayCopy(long... input); static byte[] nullableArrayCopy(byte... input); static void nullOutByteArray(byte... input); static List<T> immutableOrEmptyList(List<T> input); static List<T> immutableListOrNull(List<T> input); static List<T> mutableListCopyOrNull(List<T> input); static Map<K, V> immutableOrEmptyMap(Map<K, V> input); static Map<K, V> immutableMapOrNull(Map<K, V> input); static SortedMap<K, V> immutableSortedOrEmptyMap(SortedMap<K, V> input); static SortedMap<K, V> immutableSortedMapOrNull(SortedMap<K, V> input); static Set<T> immutableOrEmptySet(Set<T> input); static Set<T> immutableSetOrNull(Set<T> input); }
CollectionUtils { public static <T> Set<T> immutableSetOrNull(Set<T> input) { return Optional.ofNullable(input).map(HashSet::new).map(Collections::unmodifiableSet).orElse(null); } private  CollectionUtils(); static T[] nullableArrayCopy(T[] input); static double[] nullableArrayCopy(double... input); static long[] nullableArrayCopy(long... input); static byte[] nullableArrayCopy(byte... input); static void nullOutByteArray(byte... input); static List<T> immutableOrEmptyList(List<T> input); static List<T> immutableListOrNull(List<T> input); static List<T> mutableListCopyOrNull(List<T> input); static Map<K, V> immutableOrEmptyMap(Map<K, V> input); static Map<K, V> immutableMapOrNull(Map<K, V> input); static SortedMap<K, V> immutableSortedOrEmptyMap(SortedMap<K, V> input); static SortedMap<K, V> immutableSortedMapOrNull(SortedMap<K, V> input); static Set<T> immutableOrEmptySet(Set<T> input); static Set<T> immutableSetOrNull(Set<T> input);  }