View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.config;
18  
19  import java.util.Date;
20  import java.util.concurrent.Delayed;
21  import java.util.concurrent.ExecutionException;
22  import java.util.concurrent.ScheduledFuture;
23  import java.util.concurrent.TimeUnit;
24  import java.util.concurrent.TimeoutException;
25  
26  /**
27   *
28   */
29  public class CronScheduledFuture<V> implements ScheduledFuture<V> {
30  
31      private volatile FutureData futureData;
32  
33      public CronScheduledFuture(final ScheduledFuture<V> future, final Date runDate) {
34          this.futureData = new FutureData(future, runDate);
35      }
36  
37      public Date getFireTime() {
38          return futureData.runDate;
39      }
40  
41      void reset(final ScheduledFuture<?> future, final Date runDate) {
42          futureData = new FutureData(future, runDate);
43      }
44  
45      @Override
46      public long getDelay(final TimeUnit unit) {
47          return futureData.scheduledFuture.getDelay(unit);
48      }
49  
50      @Override
51      public int compareTo(final Delayed delayed) {
52          return futureData.scheduledFuture.compareTo(delayed);
53      }
54  
55      @Override
56      public boolean cancel(final boolean mayInterruptIfRunning) {
57          return futureData.scheduledFuture.cancel(mayInterruptIfRunning);
58      }
59  
60      @Override
61      public boolean isCancelled() {
62          return futureData.scheduledFuture.isCancelled();
63      }
64  
65      @Override
66      public boolean isDone() {
67          return futureData.scheduledFuture.isDone();
68      }
69  
70      @Override
71      @SuppressWarnings("unchecked")
72      public V get() throws InterruptedException, ExecutionException {
73          return (V) futureData.scheduledFuture.get();
74      }
75  
76      @Override
77      @SuppressWarnings("unchecked")
78      public V get(final long timeout, final TimeUnit unit)
79              throws InterruptedException, ExecutionException, TimeoutException {
80          return (V) futureData.scheduledFuture.get(timeout, unit);
81      }
82  
83      private class FutureData {
84  
85          private final ScheduledFuture<?> scheduledFuture;
86          private final Date runDate;
87  
88          FutureData(final ScheduledFuture<?> future, final Date runDate) {
89              this.scheduledFuture = future;
90              this.runDate = runDate;
91          }
92      }
93  }