io.vlingo.reactivestreams.source.IterableSource Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of vlingo-streams Show documentation
Show all versions of vlingo-streams Show documentation
Reactive Streams for the VLINGO XOOM Platform.
// Copyright © 2012-2020 VLINGO LABS. All rights reserved.
//
// This Source Code Form is subject to the terms of the
// Mozilla Public License, v. 2.0. If a copy of the MPL
// was not distributed with this file, You can obtain
// one at https://mozilla.org/MPL/2.0/.
package io.vlingo.reactivestreams.source;
import java.util.Iterator;
import io.vlingo.common.Completes;
import io.vlingo.reactivestreams.Elements;
import io.vlingo.reactivestreams.Source;
public class IterableSource implements Source {
private final Iterator iterator;
private final boolean slowIterable;
public IterableSource(final Iterable iterable, final boolean slowIterable) {
this.iterator = iterable.iterator();
this.slowIterable = slowIterable;
}
@Override
@SuppressWarnings("unchecked")
public Completes> next() {
if (iterator.hasNext()) {
final T[] element = (T[]) new Object[1];
element[0] = iterator.next();
return Completes.withSuccess(new Elements<>(element, false));
}
return Completes.withSuccess(new Elements<>((T[]) new Object[0], true));
}
@Override
public Completes> next(final int maximumElements) {
return next();
}
@Override
public Completes> next(final long index) {
return next();
}
@Override
public Completes> next(final long index, final int maximumElements) {
return next();
}
@Override
public Completes isSlow() {
return Completes.withSuccess(slowIterable);
}
}