The following code is from: http://www.erik-rasmussen.com/blog/2008/01/18/the-filter-pattern-java-conditional-abstraction-with-iterables/
import java.util.Iterator;
import java.util.NoSuchElementException;
public abstract class Filter<T> {
public abstract boolean passes(T object);
public Iterator<T> filter(Iterator<T> iterator) {
return new FilterIterator(iterator);
}
public Iterable<T> filter(Iterable<T> iterable) {
return new Iterable<T>() {
public Iterator<T> iterator() {
return filter(iterable.iterator());
}
};
}
private class FilterIterator implements Iterator<T> {
private Iterator<T> iterator;
private T next;
private FilterIterator(Iterator<T> iterator) {
this.iterator = iterator;
toNext();
}
public boolean hasNext() {
return next != null;
}
public T next() {
if (next == null)
throw new NoSuchElementException();
T returnValue = next;
toNext();
return returnValue;
}
public void remove() {
throw new UnsupportedOperationException();
}
private void toNext() {
next = null;
while (iterator.hasNext()) {
T item = iterator.next();
if (item != null && passes(item)) {
next = item;
break;
}
}
}
}
}
This blog is supposed to keep track of all the small fixes, good tutorials and cheat-sheets I encounter in my binary life so I won't have to seek them on the web again.
Wednesday, December 1, 2010
The Filter Pattern - Selective Iterators
Subscribe to:
Post Comments (Atom)
1 comment:
How to add Chinese keyboard to windows 10? Windows 10 Offers us to add one or multiple keyboards. If you want to type in Chinese, you should have to follow the procedure. Also, you will also understand why you’ve configured with the wrong keyboard and the wrong language and how to get rid of this problem. Normally you don’t need to change your windows language settings. But if you want to type in Chinese languages and want to type any Chinese letter or word, you must have to change the language settings. That’s why I am going to let you know how to install chinese keyboard on windows 10.
Here you’ll find two methods for windows 10 Chinese handwriting input. Following the way step by step, you’ll find it much easier to add any language keyboard. Also, I’ll give you some tips to avoid and solve the common problem related to language and keyboard. Before wrapping up the article, I’ll answer some common questions on adding the Chinese keyboard.
Post a Comment