hg: lambda/lambda/jdk: Modify Iterables.uniqueElements to be fully lazy. (original) (raw)
Rémi Forax forax at univ-mlv.fr
Fri Apr 13 16:12:28 PDT 2012
- Previous message: hg: lambda/lambda/jdk: Modify Iterables.uniqueElements to be fully lazy.
- Next message: hg: lambda/lambda/hotspot: Summary: Fixes processing of non-class types as generic parameters
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On 04/14/2012 12:23 AM, stuart.marks at oracle.com wrote:
Changeset: 08c12c22393c Author: smarks Date: 2012-04-13 15:24 -0700 URL: http://hg.openjdk.java.net/lambda/lambda/jdk/rev/08c12c22393c
Modify Iterables.uniqueElements to be fully lazy. ! src/share/classes/java/util/Iterables.java ! src/share/classes/java/util/Iterators.java ! test-ng/tests/org/openjdk/tests/java/util/IterableTest.java ! test-ng/tests/org/openjdk/tests/java/util/IterablesTest.java
Hi Stuart, null should be an allowed value. It's really easy to implement it with a null object. Also I think it's better to let the user choose which Set he wants to use (the equals semantics is not always the best).
So the code should be something like this:
private static final Object NULL_OBJECT = new Object();
public static Iterator uniqueElements(final Iterator<? extends T> iterator, final Set<? super T> seen) { Objects.requireNonNull(iterator); Objects.requireNonNull(cache);
return new Iterator<T>() {
private T cache = NULL_OBJECT;
@Override
public boolean hasNext() {
if (cache != NULL_OBJECT) {
return true;
}
// cache is empty so attempt to populate it
while (iterator.hasNext()) {
T tmp = iterator.next();
if (seen.add(tmp)) {
cache = tmp;
return true;
}
}
return false;
}
@Override
public T next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
T r = cache;
cache = NULL_OBJECT;
return r;
}
};
}
Rémi
- Previous message: hg: lambda/lambda/jdk: Modify Iterables.uniqueElements to be fully lazy.
- Next message: hg: lambda/lambda/hotspot: Summary: Fixes processing of non-class types as generic parameters
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]