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

io.vlingo.reactivestreams.source.SupplierSource Maven / Gradle / Ivy

There is a newer version: 1.7.5
Show newest version
// 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);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy