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

com.github.robozonky.common.async.AbstractReloadableImpl Maven / Gradle / Ivy

/*
 * Copyright 2019 The RoboZonky Project
 *
 * 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 com.github.robozonky.common.async;

import java.time.Duration;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

abstract class AbstractReloadableImpl implements Reloadable {

    protected final Logger logger = LogManager.getLogger(getClass());
    private final UnaryOperator operation;
    private final ReloadDetection needsReload;

    public AbstractReloadableImpl(final Supplier supplier, final UnaryOperator reloader,
                                  final Consumer runWhenReloaded) {
        this.operation = getOperation(supplier, reloader, runWhenReloaded);
        this.needsReload = new ManualReload<>();
    }

    public AbstractReloadableImpl(final Supplier supplier, final UnaryOperator reloader,
                                  final Consumer runWhenReloaded, final Function reloadAfter) {
        this.operation = getOperation(supplier, reloader, runWhenReloaded);
        this.needsReload = new TimeBasedReload<>(reloadAfter);
    }

    private  UnaryOperator getOperation(final Supplier supplier, final UnaryOperator reloader,
                                              final Consumer runWhenReloaded) {
        return original -> {
            logger.trace("Running operation on {}, previous value given: {}.", this, original);
            final X value = original == null ? supplier.get() : reloader.apply(original);
            runWhenReloaded.accept(value);  // first run finisher before setting the value, in case finisher fails
            logger.trace("Operation finished on {}.", this);
            return value;
        };
    }

    protected UnaryOperator getOperation() {
        return operation;
    }

    protected void processRetrievedValue(final T value, final Consumer valueSetter) {
        logger.trace("Supplier finished on {}.", this);
        valueSetter.accept(value);
        needsReload.markReloaded(value);
        logger.debug("Reloaded {}, new value is {}.", this, value);
    }

    protected boolean needsReload() {
        return needsReload.getAsBoolean();
    }

    @Override
    public void clear() {
        needsReload.forceReload();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy