001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.core.config;
018
019import java.util.Date;
020import java.util.concurrent.Delayed;
021import java.util.concurrent.ExecutionException;
022import java.util.concurrent.ScheduledFuture;
023import java.util.concurrent.TimeUnit;
024import java.util.concurrent.TimeoutException;
025
026/**
027 *
028 */
029public class CronScheduledFuture<V> implements ScheduledFuture<V> {
030
031    private volatile FutureData futureData;
032
033    public CronScheduledFuture(final ScheduledFuture<V> future, final Date runDate) {
034        this.futureData = new FutureData(future, runDate);
035    }
036
037    public Date getFireTime() {
038        return futureData.runDate;
039    }
040
041    void reset(final ScheduledFuture<?> future, final Date runDate) {
042        futureData = new FutureData(future, runDate);
043    }
044
045    @Override
046    public long getDelay(final TimeUnit unit) {
047        return futureData.scheduledFuture.getDelay(unit);
048    }
049
050    @Override
051    public int compareTo(final Delayed delayed) {
052        return futureData.scheduledFuture.compareTo(delayed);
053    }
054
055    @Override
056    public boolean cancel(final boolean mayInterruptIfRunning) {
057        return futureData.scheduledFuture.cancel(mayInterruptIfRunning);
058    }
059
060    @Override
061    public boolean isCancelled() {
062        return futureData.scheduledFuture.isCancelled();
063    }
064
065    @Override
066    public boolean isDone() {
067        return futureData.scheduledFuture.isDone();
068    }
069
070    @Override
071    @SuppressWarnings("unchecked")
072    public V get() throws InterruptedException, ExecutionException {
073        return (V) futureData.scheduledFuture.get();
074    }
075
076    @Override
077    @SuppressWarnings("unchecked")
078    public V get(final long timeout, final TimeUnit unit)
079            throws InterruptedException, ExecutionException, TimeoutException {
080        return (V) futureData.scheduledFuture.get(timeout, unit);
081    }
082
083    private class FutureData {
084
085        private final ScheduledFuture<?> scheduledFuture;
086        private final Date runDate;
087
088        FutureData(final ScheduledFuture<?> future, final Date runDate) {
089            this.scheduledFuture = future;
090            this.runDate = runDate;
091        }
092    }
093}