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.commons.lang3.concurrent;
018
019import java.util.Objects;
020import java.util.concurrent.ExecutionException;
021import java.util.concurrent.Future;
022import java.util.concurrent.TimeUnit;
023import java.util.concurrent.TimeoutException;
024
025/**
026 * Proxies to a {@link Future} for subclassing.
027 *
028 * @param <V> The result type returned by this Future's {@link #get()} and {@link #get(long, TimeUnit)} methods.
029 * @since 3.13.0
030 */
031public abstract class AbstractFutureProxy<V> implements Future<V> {
032
033    private final Future<V> future;
034
035    /**
036     * Constructs a new instance.
037     *
038     * @param future the delegate.
039     */
040    public AbstractFutureProxy(final Future<V> future) {
041        this.future = Objects.requireNonNull(future, "future");
042    }
043
044    @Override
045    public boolean cancel(final boolean mayInterruptIfRunning) {
046        return future.cancel(mayInterruptIfRunning);
047    }
048
049    @Override
050    public V get() throws InterruptedException, ExecutionException {
051        return future.get();
052    }
053
054    @Override
055    public V get(final long timeout, final TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
056        return future.get(timeout, unit);
057    }
058
059    /**
060     * Gets the delegate.
061     *
062     * @return the delegate.
063     */
064    public Future<V> getFuture() {
065        return future;
066    }
067
068    @Override
069    public boolean isCancelled() {
070        return future.isCancelled();
071    }
072
073    @Override
074    public boolean isDone() {
075        return future.isDone();
076    }
077
078}