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

org.bcos.web3j.utils.Observables Maven / Gradle / Ivy

There is a newer version: 2.6.6
Show newest version
package org.bcos.web3j.utils;

import java.math.BigInteger;

import rx.Observable;

/**
 * Observable utility functions.
 */
public class Observables {

    /**
     * Simple Observable implementation to emit a range of BigInteger values.
     *
     * @param startValue first value to emit in range
     * @param endValue final value to emit in range
     * @return Observable to omit this range of values
     */
    public static Observable range(BigInteger startValue, BigInteger endValue) {
        //if (startValue.compareTo(BigInteger.ZERO) == -1) {
        //the value 0 if x == y; a value less than 0 if x < y; and a value greater than 0 if x > y
        if (startValue.compareTo(BigInteger.ZERO) < 0) {
            throw new IllegalArgumentException("Negative start index cannot be used");
        } else if (startValue.compareTo(endValue) > -1) {
            throw new IllegalArgumentException(
                    "Negative start index cannot be greater then end index");
        }

        return Observable.create(subscriber -> {
            for (BigInteger i = startValue;
                    i.compareTo(endValue) < 1
                            && !subscriber.isUnsubscribed();
                    i = i.add(BigInteger.ONE)) {
                subscriber.onNext(i);
            }

            if (!subscriber.isUnsubscribed()) {
                subscriber.onCompleted();
            }
        });
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy