1 /* 2 * Copyright (C) 2006 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 15 package com.google.common.util.concurrent; 16 17 import static com.google.common.base.Preconditions.checkNotNull; 18 import static com.google.common.util.concurrent.Platform.restoreInterruptIfIsInterruptedException; 19 20 import com.google.common.annotations.GwtIncompatible; 21 import com.google.common.annotations.J2ktIncompatible; 22 import com.google.errorprone.annotations.CanIgnoreReturnValue; 23 import java.util.concurrent.Callable; 24 import java.util.concurrent.ExecutionException; 25 import java.util.concurrent.TimeUnit; 26 import org.checkerframework.checker.nullness.qual.Nullable; 27 28 /** 29 * A TimeLimiter implementation which actually does not attempt to limit time at all. This may be 30 * desirable to use in some unit tests. More importantly, attempting to debug a call which is 31 * time-limited would be extremely annoying, so this gives you a time-limiter you can easily swap in 32 * for your real time-limiter while you're debugging. 33 * 34 * @author Kevin Bourrillion 35 * @author Jens Nyman 36 * @since 1.0 37 */ 38 @J2ktIncompatible 39 @GwtIncompatible 40 @ElementTypesAreNonnullByDefault 41 public final class FakeTimeLimiter implements TimeLimiter { 42 @CanIgnoreReturnValue // TODO(kak): consider removing this 43 @Override newProxy( T target, Class<T> interfaceType, long timeoutDuration, TimeUnit timeoutUnit)44 public <T> T newProxy( 45 T target, Class<T> interfaceType, long timeoutDuration, TimeUnit timeoutUnit) { 46 checkNotNull(target); 47 checkNotNull(interfaceType); 48 checkNotNull(timeoutUnit); 49 return target; // ha ha 50 } 51 52 @CanIgnoreReturnValue // TODO(kak): consider removing this 53 @Override 54 @ParametricNullness callWithTimeout( Callable<T> callable, long timeoutDuration, TimeUnit timeoutUnit)55 public <T extends @Nullable Object> T callWithTimeout( 56 Callable<T> callable, long timeoutDuration, TimeUnit timeoutUnit) throws ExecutionException { 57 checkNotNull(callable); 58 checkNotNull(timeoutUnit); 59 try { 60 return callable.call(); 61 } catch (RuntimeException e) { 62 throw new UncheckedExecutionException(e); 63 } catch (Exception e) { 64 restoreInterruptIfIsInterruptedException(e); 65 throw new ExecutionException(e); 66 } catch (Error e) { 67 throw new ExecutionError(e); 68 } 69 } 70 71 @CanIgnoreReturnValue // TODO(kak): consider removing this 72 @Override 73 @ParametricNullness callUninterruptiblyWithTimeout( Callable<T> callable, long timeoutDuration, TimeUnit timeoutUnit)74 public <T extends @Nullable Object> T callUninterruptiblyWithTimeout( 75 Callable<T> callable, long timeoutDuration, TimeUnit timeoutUnit) throws ExecutionException { 76 return callWithTimeout(callable, timeoutDuration, timeoutUnit); 77 } 78 79 @Override 80 @SuppressWarnings("CatchingUnchecked") // sneaky checked exception runWithTimeout(Runnable runnable, long timeoutDuration, TimeUnit timeoutUnit)81 public void runWithTimeout(Runnable runnable, long timeoutDuration, TimeUnit timeoutUnit) { 82 checkNotNull(runnable); 83 checkNotNull(timeoutUnit); 84 try { 85 runnable.run(); 86 } catch (Exception e) { // sneaky checked exception 87 throw new UncheckedExecutionException(e); 88 } catch (Error e) { 89 throw new ExecutionError(e); 90 } 91 } 92 93 @Override runUninterruptiblyWithTimeout( Runnable runnable, long timeoutDuration, TimeUnit timeoutUnit)94 public void runUninterruptiblyWithTimeout( 95 Runnable runnable, long timeoutDuration, TimeUnit timeoutUnit) { 96 runWithTimeout(runnable, timeoutDuration, timeoutUnit); 97 } 98 } 99