All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jhotdraw8.collection.iterator.Iterators Maven / Gradle / Ivy

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();
            }
        };
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy