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

rx.operators.OperationSum Maven / Gradle / Ivy

There is a newer version: 0.20.7
Show newest version
/**
 * Copyright 2014 Netflix, Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package rx.operators;

import rx.Observable;
import rx.functions.Func2;

/**
 * A few operators for implementing the sum operation.
 * 
 * @see MSDN:
 *      Observable.Sum
 */
public final class OperationSum {

    public static Observable sumIntegers(Observable source) {
        return source.reduce(0, ACCUM_INT);
    }

    public static Observable sumLongs(Observable source) {
        return source.reduce(0l, ACCUM_LONG);
    }

    public static Observable sumFloats(Observable source) {
        return source.reduce(0.0f, ACCUM_FLOAT);
    }

    public static Observable sumDoubles(Observable source) {
        return source.reduce(0.0d, ACCUM_DOUBLE);
    }

    public static Observable sumAtLeastOneIntegers(Observable source) {
        return source.reduce(ACCUM_INT);
    }

    public static Observable sumAtLeastOneLongs(Observable source) {
        return source.reduce(ACCUM_LONG);
    }

    public static Observable sumAtLeastOneFloats(Observable source) {
        return source.reduce(ACCUM_FLOAT);
    }

    public static Observable sumAtLeastOneDoubles(Observable source) {
        return source.reduce(ACCUM_DOUBLE);
    }

    private static final Func2 ACCUM_INT = new Func2() {
        @Override
        public Integer call(Integer accu, Integer next) {
            return accu + next;
        }
    };

    private static final Func2 ACCUM_LONG = new Func2() {
        @Override
        public Long call(Long accu, Long next) {
            return accu + next;
        }
    };

    private static final Func2 ACCUM_FLOAT = new Func2() {
        @Override
        public Float call(Float accu, Float next) {
            return accu + next;
        }
    };

    private static final Func2 ACCUM_DOUBLE = new Func2() {
        @Override
        public Double call(Double accu, Double next) {
            return accu + next;
        }
    };
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy