001/*
002 * Copyright (C) 2006 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 */
014
015package com.google.common.util.concurrent;
016
017import static com.google.common.base.Preconditions.checkNotNull;
018
019import com.google.common.annotations.Beta;
020import com.google.common.annotations.GwtIncompatible;
021import com.google.errorprone.annotations.CanIgnoreReturnValue;
022import java.util.concurrent.Callable;
023import java.util.concurrent.ExecutionException;
024import java.util.concurrent.TimeUnit;
025import org.checkerframework.checker.nullness.qual.Nullable;
026
027/**
028 * A TimeLimiter implementation which actually does not attempt to limit time at all. This may be
029 * desirable to use in some unit tests. More importantly, attempting to debug a call which is
030 * time-limited would be extremely annoying, so this gives you a time-limiter you can easily swap in
031 * for your real time-limiter while you're debugging.
032 *
033 * @author Kevin Bourrillion
034 * @author Jens Nyman
035 * @since 1.0
036 */
037@Beta
038@CanIgnoreReturnValue
039@GwtIncompatible
040@ElementTypesAreNonnullByDefault
041public final class FakeTimeLimiter implements TimeLimiter {
042  @Override
043  public <T> T newProxy(
044      T target, Class<T> interfaceType, long timeoutDuration, TimeUnit timeoutUnit) {
045    checkNotNull(target);
046    checkNotNull(interfaceType);
047    checkNotNull(timeoutUnit);
048    return target; // ha ha
049  }
050
051  @Override
052  @ParametricNullness
053  public <T extends @Nullable Object> T callWithTimeout(
054      Callable<T> callable, long timeoutDuration, TimeUnit timeoutUnit) throws ExecutionException {
055    checkNotNull(callable);
056    checkNotNull(timeoutUnit);
057    try {
058      return callable.call();
059    } catch (RuntimeException e) {
060      throw new UncheckedExecutionException(e);
061    } catch (Exception e) {
062      throw new ExecutionException(e);
063    } catch (Error e) {
064      throw new ExecutionError(e);
065    } catch (Throwable e) {
066      // It's a non-Error, non-Exception Throwable. Such classes are usually intended to extend
067      // Exception, so we'll treat it like an Exception.
068      throw new ExecutionException(e);
069    }
070  }
071
072  @Override
073  @ParametricNullness
074  public <T extends @Nullable Object> T callUninterruptiblyWithTimeout(
075      Callable<T> callable, long timeoutDuration, TimeUnit timeoutUnit) throws ExecutionException {
076    return callWithTimeout(callable, timeoutDuration, timeoutUnit);
077  }
078
079  @Override
080  public void runWithTimeout(Runnable runnable, long timeoutDuration, TimeUnit timeoutUnit) {
081    checkNotNull(runnable);
082    checkNotNull(timeoutUnit);
083    try {
084      runnable.run();
085    } catch (RuntimeException e) {
086      throw new UncheckedExecutionException(e);
087    } catch (Error e) {
088      throw new ExecutionError(e);
089    } catch (Throwable e) {
090      // It's a non-Error, non-Exception Throwable. Such classes are usually intended to extend
091      // Exception, so we'll treat it like a RuntimeException.
092      throw new UncheckedExecutionException(e);
093    }
094  }
095
096  @Override
097  public void runUninterruptiblyWithTimeout(
098      Runnable runnable, long timeoutDuration, TimeUnit timeoutUnit) {
099    runWithTimeout(runnable, timeoutDuration, timeoutUnit);
100  }
101}