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

netflix.ocelli.functions.Delays Maven / Gradle / Ivy

There is a newer version: 0.1.0-rc.2
Show newest version
package netflix.ocelli.functions;

import java.util.concurrent.TimeUnit;

import rx.functions.Func1;

public abstract class Delays {
    public static Func1 fixed(final long delay, final TimeUnit units) {
        return new Func1() {
            @Override
            public Long call(Integer t1) {
                return TimeUnit.MILLISECONDS.convert(delay, units);
            }
        };
    }

    public static Func1 linear(final long delay, final TimeUnit units) {
        return new Func1() {
            @Override
            public Long call(Integer counter) {
                return counter * TimeUnit.MILLISECONDS.convert(delay, units);
            }
        };
    }

    public static Func1 exp(final long step, final TimeUnit units) {
        return new Func1() {
            @Override
            public Long call(Integer count) {
                if (count < 0) 
                    count = 0;
                else if (count > 30) 
                    count = 30;
                return (1 << count) * TimeUnit.MILLISECONDS.convert(step, units);
            }
        };
    }
    
    public static Func1 boundedExp(final long step, final long max, final TimeUnit units) {
        return new Func1() {
            @Override
            public Long call(Integer count) {
                if (count < 0) 
                    count = 0;
                else if (count > 30) 
                    count = 30;
                long delay = (1 << count) * TimeUnit.MILLISECONDS.convert(step, units);
                if (delay > max) {
                    return max;
                }
                return delay;
            }
        };
    }

    public static Func1 immediate() {
        return new Func1() {
            @Override
            public Long call(Integer t1) {
                return 0L;
            }
        };
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy