@Test public void testTransformingListViewModifyAdd() { List<String> strings = new ArrayList<>(); List<Integer> ints = Views.listView(strings, Integer::parseInt); strings.add("1"); strings.add("2"); strings.add("3"); try { ints.add(4); } catch (UnsupportedOperationException e) { return; } fail("UnsupportedOperationException expected."); }
public static <T, R> List<T> listView(List<R> originalList, Function<R, T> transformer) { return new AbstractSequentialList<T>() { @Override public ListIterator<T> listIterator(int index) { return new ListIterator<T>() { final ListIterator<R> iterator = originalList.listIterator(index); @Override public boolean hasNext() { return iterator.hasNext(); } @Override public T next() { return transformer.apply(iterator.next()); } @Override public boolean hasPrevious() { return iterator.hasPrevious(); } @Override public T previous() { return transformer.apply(iterator.previous()); } @Override public int nextIndex() { return iterator.nextIndex(); } @Override public int previousIndex() { return iterator.previousIndex(); } @Override public void remove() { iterator.remove(); } @Override public void set(T t) { throw new UnsupportedOperationException("Not supported."); } @Override public void add(T t) { throw new UnsupportedOperationException("Not supported."); } }; } @Override public int size() { return originalList.size(); } }; }
Views { public static <T, R> List<T> listView(List<R> originalList, Function<R, T> transformer) { return new AbstractSequentialList<T>() { @Override public ListIterator<T> listIterator(int index) { return new ListIterator<T>() { final ListIterator<R> iterator = originalList.listIterator(index); @Override public boolean hasNext() { return iterator.hasNext(); } @Override public T next() { return transformer.apply(iterator.next()); } @Override public boolean hasPrevious() { return iterator.hasPrevious(); } @Override public T previous() { return transformer.apply(iterator.previous()); } @Override public int nextIndex() { return iterator.nextIndex(); } @Override public int previousIndex() { return iterator.previousIndex(); } @Override public void remove() { iterator.remove(); } @Override public void set(T t) { throw new UnsupportedOperationException("Not supported."); } @Override public void add(T t) { throw new UnsupportedOperationException("Not supported."); } }; } @Override public int size() { return originalList.size(); } }; } }
Views { public static <T, R> List<T> listView(List<R> originalList, Function<R, T> transformer) { return new AbstractSequentialList<T>() { @Override public ListIterator<T> listIterator(int index) { return new ListIterator<T>() { final ListIterator<R> iterator = originalList.listIterator(index); @Override public boolean hasNext() { return iterator.hasNext(); } @Override public T next() { return transformer.apply(iterator.next()); } @Override public boolean hasPrevious() { return iterator.hasPrevious(); } @Override public T previous() { return transformer.apply(iterator.previous()); } @Override public int nextIndex() { return iterator.nextIndex(); } @Override public int previousIndex() { return iterator.previousIndex(); } @Override public void remove() { iterator.remove(); } @Override public void set(T t) { throw new UnsupportedOperationException("Not supported."); } @Override public void add(T t) { throw new UnsupportedOperationException("Not supported."); } }; } @Override public int size() { return originalList.size(); } }; } private  Views(); }
Views { public static <T, R> List<T> listView(List<R> originalList, Function<R, T> transformer) { return new AbstractSequentialList<T>() { @Override public ListIterator<T> listIterator(int index) { return new ListIterator<T>() { final ListIterator<R> iterator = originalList.listIterator(index); @Override public boolean hasNext() { return iterator.hasNext(); } @Override public T next() { return transformer.apply(iterator.next()); } @Override public boolean hasPrevious() { return iterator.hasPrevious(); } @Override public T previous() { return transformer.apply(iterator.previous()); } @Override public int nextIndex() { return iterator.nextIndex(); } @Override public int previousIndex() { return iterator.previousIndex(); } @Override public void remove() { iterator.remove(); } @Override public void set(T t) { throw new UnsupportedOperationException("Not supported."); } @Override public void add(T t) { throw new UnsupportedOperationException("Not supported."); } }; } @Override public int size() { return originalList.size(); } }; } 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 <T, R> List<T> listView(List<R> originalList, Function<R, T> transformer) { return new AbstractSequentialList<T>() { @Override public ListIterator<T> listIterator(int index) { return new ListIterator<T>() { final ListIterator<R> iterator = originalList.listIterator(index); @Override public boolean hasNext() { return iterator.hasNext(); } @Override public T next() { return transformer.apply(iterator.next()); } @Override public boolean hasPrevious() { return iterator.hasPrevious(); } @Override public T previous() { return transformer.apply(iterator.previous()); } @Override public int nextIndex() { return iterator.nextIndex(); } @Override public int previousIndex() { return iterator.previousIndex(); } @Override public void remove() { iterator.remove(); } @Override public void set(T t) { throw new UnsupportedOperationException("Not supported."); } @Override public void add(T t) { throw new UnsupportedOperationException("Not supported."); } }; } @Override public int size() { return originalList.size(); } }; } 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);  }