org.jhotdraw8.collection.spliterator.SpliteratorIterable Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of org.jhotdraw8.collection Show documentation
Show all versions of org.jhotdraw8.collection Show documentation
JHotDraw8 Utility classes for Collections
The newest version!
/*
* @(#)SpliteratorIterable.java
* Copyright © 2023 The authors and contributors of JHotDraw. MIT License.
*/
package org.jhotdraw8.collection.spliterator;
import java.util.Iterator;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.Consumer;
import java.util.function.Supplier;
/**
* An Iterable which takes a Spliterator supplier to implement the
* Iterable methods.
*
* @param the element type
* @author Werner Randelshofer
*/
public class SpliteratorIterable implements Iterable {
private final Supplier> factory;
public SpliteratorIterable(Supplier> factory) {
this.factory = factory;
}
@Override
public void forEach(Consumer super T> action) {
factory.get().forEachRemaining(action);
}
@Override
public Iterator iterator() {
return Spliterators.iterator(factory.get());
}
@Override
public Spliterator spliterator() {
return factory.get();
}
}