io.vlingo.reactivestreams.source.LongRangeSource 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 io.vlingo.common.Completes;
import io.vlingo.reactivestreams.Elements;
import io.vlingo.reactivestreams.Source;
public class LongRangeSource implements Source {
private long current;
public final long endExclusive;
public final long startInclusive;
public LongRangeSource(final long startInclusive, final long endExclusive) {
assert(startInclusive <= endExclusive);
assert(startInclusive >= 0 && startInclusive <= Long.MAX_VALUE);
this.startInclusive = startInclusive;
assert(endExclusive >= 0 && endExclusive <= Long.MAX_VALUE);
this.endExclusive = endExclusive;
this.current = startInclusive;
}
@Override
public Completes> next() {
if (current < endExclusive) {
final Long[] element = new Long[1];
element[0] = current++;
return Completes.withSuccess(new Elements<>(element, false));
}
return Completes.withSuccess(new Elements<>(new Long[0], true));
}
@Override
public Completes> next(final int maximumElements) {
return next();
}
@Override
public Completes> next(long index) {
return next();
}
@Override
public Completes> next(final long index, final int maximumElements) {
return next();
}
@Override
public Completes isSlow() {
return Completes.withSuccess(false);
}
@Override
public String toString() {
return "LongRangeSource [startInclusive=" + startInclusive +
" endExclusive=" + endExclusive + " current=" + current + "]";
}
}