org.jhotdraw8.collection.iterator.Iterators 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!
/*
* @(#)Iterators.java
* Copyright © 2023 The authors and contributors of JHotDraw. MIT License.
*/
package org.jhotdraw8.collection.iterator;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* Provides static utility methods for iterators.
*
* @author Werner Randelshofer
*/
public class Iterators {
/**
* Don't let anyone instantiate this class.
*/
private Iterators() {
}
/**
* Creates a list from an {@code Iterable}.
* If the {@code Iterable} is a list, it is returned.
*
* @param the value type
* @param iterable the iterable
* @return the list
*/
public static List toList(Iterable iterable) {
if (iterable instanceof List>) {
return (List) iterable;
}
ArrayList list = new ArrayList<>();
iterable.forEach(list::add);
return list;
}
public static Iterator unmodifiableIterator(Iterator iterator) {
return new Iterator<>() {
@Override
public boolean hasNext() {
return iterator.hasNext();
}
@Override
public E next() {
return iterator.next();
}
};
}
}