public interface Filter<T> { public boolean accept(T value); } public class Functional { /** * Filter items from the view of the returned Iterable<T> */ public static <T> Iterable<T> filter(final Iterable<T> iterable, final Filter<T> filter) { if (iterable == null) throw new NullPointerException(); if (filter == null) throw new NullPointerException(); return new Iterable<T>() { @Override public Iterator<T> iterator() { final Iterator<T> iterator = iterable.iterator(); return new Iterator<T>() { @Override public boolean hasNext() { this.ensureReady(); return this.ready; } @Override public T next() { this.ensureReady(); if (!this.ready) throw new NoSuchElementException(); T result = this.current; this.ready = false; this.current = null; return result; } @Override public void remove() { iterator.remove(); } // private boolean ready = false; private T current; private void ensureReady() { while (!this.ready && iterator.hasNext()) { T item = iterator.next(); if (!filter.accept(item)) continue; this.ready = true; this.current = item; } } }; } }; } }
Example:
Filter<String> nonNull = new Filter<String>() { public boolean accept(String value) { return value != null; } }; Iterable<String> data = ...; for(String item: filter(data, nonNull)) { System.out.println("item: "+item); // guaranteed not to be null }
No comments:
Post a Comment