io.vlingo.reactivestreams.source.SupplierSource 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.function.Supplier;
import io.vlingo.common.Completes;
import io.vlingo.reactivestreams.Elements;
import io.vlingo.reactivestreams.Source;
/**
* A Source that uses a {@code Supplier} to provide next {@code Element} instances.
*
* @param the T type of Element being supplied
*/
public class SupplierSource implements Source {
private final boolean slowSupplier;
private final Supplier supplier;
public SupplierSource(final Supplier supplier, final boolean slowSupplier) {
this.supplier = supplier;
this.slowSupplier = slowSupplier;
}
@Override
@SuppressWarnings("unchecked")
public Completes> next() {
final T any = supplier.get();
if (any != null) {
final T[] elements = (T[]) new Object[1];
elements[0] = any;
return Completes.withSuccess(new Elements<>(elements, 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(slowSupplier);
}
}