at.molindo.utils.collections.IteratorChain Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of molindo-utils Show documentation
Show all versions of molindo-utils Show documentation
Simply utility methods used across other Molindo projects
/**
* Copyright 2010 Molindo GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package at.molindo.utils.collections;
import static at.molindo.utils.collections.IteratorUtils.iterators;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
public class IteratorChain implements Iterator {
private final Iterator> _iterators;
private Iterator _next;
private Iterator _last;
public static Builder builder() {
return new Builder();
}
public static Builder builder(Class cls) {
return builder();
}
public static Builder builder(Iterator iter) {
return new Builder().add(iter);
}
public static Builder builder(Iterable iter) {
return new Builder().add(iter);
}
public static Builder builder(T o) {
return new Builder().add(o);
}
public static Builder builder(T... o) {
return new Builder().add(o);
}
public static IteratorChain chainIterables(Iterable extends Iterable> iterables) {
return new IteratorChain(iterators(iterables));
}
public static IteratorChain chainIterables(Iterator extends Iterable> iterables) {
return new IteratorChain(iterators(iterables));
}
public IteratorChain(Iterable> iterators) {
this(iterators.iterator());
}
public IteratorChain(Iterator> iterator) {
// make a copy
_iterators = IteratorUtils.list(iterator).iterator();
// init
if (_iterators.hasNext()) {
_next = _iterators.next();
}
}
@Override
public boolean hasNext() {
if (_next == null) {
return false;
} else if (_next.hasNext()) {
return true;
} else if (_iterators.hasNext()) {
_next = _iterators.next();
return hasNext();
} else {
_next = null;
return false;
}
}
@Override
public T next() {
if (_next == null) {
throw new NoSuchElementException();
}
T o = _next.next();
_last = _next;
return o;
}
@Override
public void remove() {
if (_last == null) {
throw new IllegalStateException("next() has not yet been called");
}
_last.remove();
}
public static class Builder implements Iterable {
private final List> _iterators = new ArrayList>();
private Builder() {
}
public Builder add(Iterator iter) {
_iterators.add(iter);
return this;
}
public Builder add(Iterable iter) {
return add(iter.iterator());
}
public Builder add(T o) {
return add(Collections.singleton(o).iterator());
}
public Builder add(T... o) {
return add(Arrays.asList(o).iterator());
}
public IteratorChain build() {
return new IteratorChain(_iterators);
}
@Override
public Iterator iterator() {
return build();
}
}
}