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

com.softicar.platform.common.container.iterable.IteratorIterable Maven / Gradle / Ivy

Go to download

The SoftiCAR Platform is a lightweight, Java-based library to create interactive business web applications.

There is a newer version: 50.0.0
Show newest version
package com.softicar.platform.common.container.iterable;

import com.softicar.platform.common.container.iterator.EnumerationIterator;
import com.softicar.platform.common.core.exceptions.SofticarDeveloperException;
import java.util.Enumeration;
import java.util.Iterator;

/**
 * Adapter class that converts an {@link Iterator} or {@link Enumeration} into
 * an {@link Iterable}.
 * 

* Instances of this class may only be traversed once. * * @author Oliver Richers */ public class IteratorIterable implements Iterable { private final Iterator iterator; private boolean used; public IteratorIterable(Iterator iterator) { this.iterator = iterator; this.used = false; } /** * Creates a new instance of this class, based on the specified * {@link Iterator} . * * @param iterator * the {@link Iterator} to convert into an {@link Iterable} * @return new instance of {@link IteratorIterable} */ public static IteratorIterable create(Iterator iterator) { return new IteratorIterable<>(iterator); } /** * Creates a new instance of this class, based on the specified * {@link Enumeration} . * * @param enumeration * the {@link Enumeration} to convert into an {@link Iterable} * @return new instance of {@link IteratorIterable} */ public static IteratorIterable create(Enumeration enumeration) { return new IteratorIterable<>(new EnumerationIterator<>(enumeration)); } /** * Returns the iterator. *

* This method may only be called once. * * @throws SofticarDeveloperException * if this method is called twice */ @Override public Iterator iterator() { if (used) { throw new SofticarDeveloperException("This iterable may only be used once."); } used = true; return iterator; } }