@Test public void testSetUnionViewNulls() { Set<Object> union = Views.setUnionView(Collections.emptySet(), Collections.emptySet()); assertNotNull(union); try { Views.setUnionView(Collections.emptySet(), null); fail(); } catch (NullPointerException e) { } try { Views.setUnionView(null, Collections.emptySet()); fail(); } catch (NullPointerException e) { } }
public static <E> Set<E> setUnionView(final Set<? extends E> set1, final Set<? extends E> set2) { checkNotNull(set1, "set1"); checkNotNull(set2, "set2"); return new AbstractSet<E>() { @Override public Iterator<E> iterator() { return getUnion(set1, set2).iterator(); } @Override public int size() { return getUnion(set1, set2).size(); } private Set<E> getUnion(Set<? extends E> set1, Set<? extends E> set2) { HashSet<E> hashSet = new HashSet<>(set1); hashSet.addAll(set2); return hashSet; } }; }
Views { public static <E> Set<E> setUnionView(final Set<? extends E> set1, final Set<? extends E> set2) { checkNotNull(set1, "set1"); checkNotNull(set2, "set2"); return new AbstractSet<E>() { @Override public Iterator<E> iterator() { return getUnion(set1, set2).iterator(); } @Override public int size() { return getUnion(set1, set2).size(); } private Set<E> getUnion(Set<? extends E> set1, Set<? extends E> set2) { HashSet<E> hashSet = new HashSet<>(set1); hashSet.addAll(set2); return hashSet; } }; } }
Views { public static <E> Set<E> setUnionView(final Set<? extends E> set1, final Set<? extends E> set2) { checkNotNull(set1, "set1"); checkNotNull(set2, "set2"); return new AbstractSet<E>() { @Override public Iterator<E> iterator() { return getUnion(set1, set2).iterator(); } @Override public int size() { return getUnion(set1, set2).size(); } private Set<E> getUnion(Set<? extends E> set1, Set<? extends E> set2) { HashSet<E> hashSet = new HashSet<>(set1); hashSet.addAll(set2); return hashSet; } }; } private  Views(); }
Views { public static <E> Set<E> setUnionView(final Set<? extends E> set1, final Set<? extends E> set2) { checkNotNull(set1, "set1"); checkNotNull(set2, "set2"); return new AbstractSet<E>() { @Override public Iterator<E> iterator() { return getUnion(set1, set2).iterator(); } @Override public int size() { return getUnion(set1, set2).size(); } private Set<E> getUnion(Set<? extends E> set1, Set<? extends E> set2) { HashSet<E> hashSet = new HashSet<>(set1); hashSet.addAll(set2); return hashSet; } }; } private  Views(); static List<T> listView(List<R> originalList, Function<R, T> transformer); static Map<K, V> mapView(Map<K, O> originalMap, Function<O, V> valuesTransformer); static Set<E> setUnionView(final Set<? extends E> set1, final Set<? extends E> set2); static Set<E> setDiffView(final Set<? extends E> set1, final Set<? extends E> set2); }
Views { public static <E> Set<E> setUnionView(final Set<? extends E> set1, final Set<? extends E> set2) { checkNotNull(set1, "set1"); checkNotNull(set2, "set2"); return new AbstractSet<E>() { @Override public Iterator<E> iterator() { return getUnion(set1, set2).iterator(); } @Override public int size() { return getUnion(set1, set2).size(); } private Set<E> getUnion(Set<? extends E> set1, Set<? extends E> set2) { HashSet<E> hashSet = new HashSet<>(set1); hashSet.addAll(set2); return hashSet; } }; } private  Views(); static List<T> listView(List<R> originalList, Function<R, T> transformer); static Map<K, V> mapView(Map<K, O> originalMap, Function<O, V> valuesTransformer); static Set<E> setUnionView(final Set<? extends E> set1, final Set<? extends E> set2); static Set<E> setDiffView(final Set<? extends E> set1, final Set<? extends E> set2);  }