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

org.wildfly.clustering.ejb.cache.timer.ScheduleTimerMetaDataEntry Maven / Gradle / Ivy

/*
 * Copyright The WildFly Authors
 * SPDX-License-Identifier: Apache-2.0
 */

package org.wildfly.clustering.ejb.cache.timer;

import java.lang.reflect.Method;
import java.time.Instant;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;

import org.wildfly.clustering.ejb.timer.ImmutableScheduleExpression;
import org.wildfly.clustering.ejb.timer.ScheduleTimerConfiguration;

/**
 * An schedule-based timer metadata cache entry.
 * @author Paul Ferraro
 * @param  the timer context type
 */
public class ScheduleTimerMetaDataEntry extends AbstractTimerMetaDataEntry implements ImmutableScheduleTimerMetaDataEntry {

    private final ImmutableScheduleExpression expression;
    private final Predicate timeoutMatcher;

    private volatile UnaryOperator operator = null;

    public ScheduleTimerMetaDataEntry(C context, ScheduleTimerConfiguration config) {
        this(context, config, DefaultTimeoutMatcher.INSTANCE);
    }

    public ScheduleTimerMetaDataEntry(C context, ScheduleTimerConfiguration config, Method method) {
        this(context, config, new TimeoutDescriptor(method));
    }

    private ScheduleTimerMetaDataEntry(C context, ScheduleTimerConfiguration config, Predicate timeoutMatcher) {
        // Create operator eagerly when timer entry is created
        this(context, config, timeoutMatcher, ScheduleTimerOperatorFactory.INSTANCE.createOperator(config.getScheduleExpression()));
    }

    private ScheduleTimerMetaDataEntry(C context, ScheduleTimerConfiguration config, Predicate timeoutMatcher, UnaryOperator operator) {
        // Use operator to calculate start time
        this(context, operator.apply(null), config.getScheduleExpression(), timeoutMatcher, operator);
    }

    ScheduleTimerMetaDataEntry(C context, Instant start, ImmutableScheduleExpression expression, Predicate timeoutMatcher) {
        // Create operator lazily
        this(context, start, expression, timeoutMatcher, null);
    }

    private ScheduleTimerMetaDataEntry(C context, Instant start, ImmutableScheduleExpression expression, Predicate timeoutMatcher, UnaryOperator operator) {
        super(context, start);
        this.expression = expression;
        this.timeoutMatcher = timeoutMatcher;
        this.operator = operator;
    }

    /* Get operator, lazily creating if necessary */
    private UnaryOperator getOperator() {
        if (this.operator == null) {
            synchronized (this) {
                if (this.operator == null) {
                    this.operator = ScheduleTimerOperatorFactory.INSTANCE.createOperator(this.expression);
                }
            }
        }
        return this.operator;
    }

    @Override
    public ImmutableScheduleExpression getScheduleExpression() {
        return this.expression;
    }

    @Override
    public Predicate getTimeoutMatcher() {
        return this.timeoutMatcher;
    }

    @Override
    public Instant apply(Instant lastTimeout) {
        return this.getOperator().apply(lastTimeout);
    }

    @Override
    protected RemappableTimerMetaDataEntry clone() {
        return new ScheduleTimerMetaDataEntry<>(this.getContext(), this.getStart(), this.expression, this.timeoutMatcher, this.operator);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy