com.github.robozonky.common.async.TimeBasedReload 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.time.Instant;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import com.github.robozonky.internal.test.DateUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
final class TimeBasedReload implements ReloadDetection {
private static final Logger LOGGER = LogManager.getLogger(TimeBasedReload.class);
private final AtomicReference lastReloaded = new AtomicReference<>();
private final AtomicReference reloadAfter = new AtomicReference<>();
private final Function reloadFunction;
public TimeBasedReload(final Function reloadAfter) {
this.reloadFunction = reloadAfter;
}
@Override
public boolean getAsBoolean() {
final Instant lastReloadedInstant = lastReloaded.get();
return lastReloadedInstant == null ||
lastReloadedInstant.plus(reloadAfter.get()).isBefore(DateUtil.now());
}
Optional getReloadAfter() {
return Optional.ofNullable(reloadAfter.get());
}
@Override
public void markReloaded(final T newValue) {
final Duration newReload = reloadFunction.apply(newValue);
lastReloaded.set(DateUtil.now());
reloadAfter.set(newReload);
LOGGER.trace("Marked reloaded on {}, will be reloaded after {}.", this, newReload);
}
@Override
public void forceReload() {
lastReloaded.set(null);
reloadAfter.set(null);
LOGGER.trace("Forcing reload on {}.", this);
}
}