001/* 002 * ModeShape (http://www.modeshape.org) 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.modeshape.common.util; 017 018import java.util.concurrent.Future; 019import java.util.concurrent.TimeUnit; 020import org.modeshape.common.annotation.Immutable; 021 022/** 023 * A {@link Future} implementation that is immediately done. 024 * 025 * @param <V> the type of result 026 */ 027@Immutable 028public final class ImmediateFuture<V> implements Future<V> { 029 030 /** 031 * Factory method to more easily create an immediate future 032 * 033 * @param <T> the value type 034 * @param value the value that the future should return 035 * @return the new future; never null 036 */ 037 public static <T> ImmediateFuture<T> create( T value ) { 038 return new ImmediateFuture<T>(value); 039 } 040 041 private final V result; 042 043 /** 044 * Create a new {@link Future} instance that is completed immediately and returns the supplied result. 045 * 046 * @param value the value that the future should return 047 */ 048 public ImmediateFuture( V value ) { 049 this.result = value; 050 } 051 052 @Override 053 public boolean cancel( boolean mayInterruptIfRunning ) { 054 return false; // completed 055 } 056 057 @Override 058 public V get() { 059 return result; 060 } 061 062 @Override 063 public V get( long timeout, 064 TimeUnit unit ) { 065 return result; 066 } 067 068 @Override 069 public boolean isCancelled() { 070 return false; 071 } 072 073 @Override 074 public boolean isDone() { 075 return true; 076 } 077 078}