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

functionalj.promise.Retry Maven / Gradle / Ivy

// ============================================================================
// Copyright (c) 2017-2019 Nawapunth Manusitthipol (NawaMan - http://nawaman.net).
// ----------------------------------------------------------------------------
// MIT License
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// ============================================================================
package functionalj.promise;

import functionalj.ref.Ref;
import lombok.val;

public class Retry {
    
    static final int  NO_RETRY      =  0;
    static final int  RETRY_FOREVER = -1;
    static final long NO_WAIT       =  0L;
    
    public static final Ref defaultRetryCount = Ref.ofValue(NO_RETRY);
    public static final Ref    defaultRetryWait  = Ref.ofValue(NO_WAIT);
    
    @SuppressWarnings("rawtypes")
    static final Retry noRetry = new Retry(NO_RETRY, NO_WAIT);
    
    public static final  Retry defaultRetry() {
        return new Retry(
                defaultRetryCount.orElse(NO_RETRY),
                defaultRetryWait .orElse(NO_WAIT));
    }
    
    private final int  times;
    private final long waitTimeMilliSecond;
    
    public Retry(int times, long waitTimeMilliSecond) {
        this.times = times;
        this.waitTimeMilliSecond = waitTimeMilliSecond;
    }
    
    public int times() {
        return times;
    }
    public long waitTimeMilliSecond() {
        return waitTimeMilliSecond;
    }
    
    public Retry times(int times) {
        return new Retry(times, waitTimeMilliSecond);
    }
    
    public Retry waitTimeMilliSecond(long waitTimeMilliSecond) {
        return new Retry(times, waitTimeMilliSecond);
    }
    
    public WaitRetry waitFor(long period) {
        return new WaitRetry(this, period);
    }
    
    public static class WaitRetry {
        
        private final Retry retry;
        private final long  period;
        
        WaitRetry(Retry retry, long period) {
            this.retry  = retry;
            this.period = period;
        }
        
        public Retry milliseconds() {
            return new Retry(retry.times, period);
        }
        
        public Retry seconds() {
            return new Retry(retry.times, period * 1000);
        }
        
        public Retry minutes() {
            return new Retry(retry.times, period * 1000 * 60);
        }
        
        public Retry hours() {
            return new Retry(retry.times, period * 1000 * 60 * 60);
        }
        
        public Retry days() {
            return new Retry(retry.times, period * 1000 * 60 * 60 * 24);
        }
        
        public Retry weeks() {
            return new Retry(retry.times, period * 1000 * 60 * 60 * 24 * 7);
        }
        
    }
    
    DeferAction create(DeferActionBuilder builder) {
        val interruptOnCancel = builder.interruptOnCancel();
        val supplier          = builder.supplier();
        val onStart           = builder.onStart();
        val runner            = builder.runner();
        if (times == Retry.NO_RETRY)
            return DeferAction.create(interruptOnCancel, supplier, onStart, runner);
        
        return RetryableDeferActionCreator.current.value()
                .createRetryDeferAction(interruptOnCancel, onStart, runner, this, supplier);
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy