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;
018import static com.google.common.util.concurrent.Platform.restoreInterruptIfIsInterruptedException;
019
020import com.google.common.annotations.GwtIncompatible;
021import com.google.common.annotations.J2ktIncompatible;
022import com.google.errorprone.annotations.CanIgnoreReturnValue;
023import java.util.concurrent.Callable;
024import java.util.concurrent.ExecutionException;
025import java.util.concurrent.TimeUnit;
026import org.checkerframework.checker.nullness.qual.Nullable;
027
028/**
029 * A TimeLimiter implementation which actually does not attempt to limit time at all. This may be
030 * desirable to use in some unit tests. More importantly, attempting to debug a call which is
031 * time-limited would be extremely annoying, so this gives you a time-limiter you can easily swap in
032 * for your real time-limiter while you're debugging.
033 *
034 * @author Kevin Bourrillion
035 * @author Jens Nyman
036 * @since 1.0
037 */
038@J2ktIncompatible
039@GwtIncompatible
040@ElementTypesAreNonnullByDefault
041public final class FakeTimeLimiter implements TimeLimiter {
042  @CanIgnoreReturnValue // TODO(kak): consider removing this
043  @Override
044  public <T> T newProxy(
045      T target, Class<T> interfaceType, long timeoutDuration, TimeUnit timeoutUnit) {
046    checkNotNull(target);
047    checkNotNull(interfaceType);
048    checkNotNull(timeoutUnit);
049    return target; // ha ha
050  }
051
052  @CanIgnoreReturnValue // TODO(kak): consider removing this
053  @Override
054  @ParametricNullness
055  public <T extends @Nullable Object> T callWithTimeout(
056      Callable<T> callable, long timeoutDuration, TimeUnit timeoutUnit) throws ExecutionException {
057    checkNotNull(callable);
058    checkNotNull(timeoutUnit);
059    try {
060      return callable.call();
061    } catch (RuntimeException e) {
062      throw new UncheckedExecutionException(e);
063    } catch (Exception e) {
064      restoreInterruptIfIsInterruptedException(e);
065      throw new ExecutionException(e);
066    } catch (Error e) {
067      throw new ExecutionError(e);
068    }
069  }
070
071  @CanIgnoreReturnValue // TODO(kak): consider removing this
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  @SuppressWarnings("CatchingUnchecked") // sneaky checked exception
081  public void runWithTimeout(Runnable runnable, long timeoutDuration, TimeUnit timeoutUnit) {
082    checkNotNull(runnable);
083    checkNotNull(timeoutUnit);
084    try {
085      runnable.run();
086    } catch (Exception e) { // sneaky checked exception
087      throw new UncheckedExecutionException(e);
088    } catch (Error e) {
089      throw new ExecutionError(e);
090    }
091  }
092
093  @Override
094  public void runUninterruptiblyWithTimeout(
095      Runnable runnable, long timeoutDuration, TimeUnit timeoutUnit) {
096    runWithTimeout(runnable, timeoutDuration, timeoutUnit);
097  }
098}