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

io.vlingo.reactivestreams.source.IterableSource 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.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);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy