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  /** Creates a new {@link FakeTimeLimiter}. */
043  public FakeTimeLimiter() {}
044
045  @CanIgnoreReturnValue // TODO(kak): consider removing this
046  @Override
047  public <T> T newProxy(
048      T target, Class<T> interfaceType, long timeoutDuration, TimeUnit timeoutUnit) {
049    checkNotNull(target);
050    checkNotNull(interfaceType);
051    checkNotNull(timeoutUnit);
052    return target; // ha ha
053  }
054
055  @CanIgnoreReturnValue // TODO(kak): consider removing this
056  @Override
057  @ParametricNullness
058  public <T extends @Nullable Object> T callWithTimeout(
059      Callable<T> callable, long timeoutDuration, TimeUnit timeoutUnit) throws ExecutionException {
060    checkNotNull(callable);
061    checkNotNull(timeoutUnit);
062    try {
063      return callable.call();
064    } catch (RuntimeException e) {
065      throw new UncheckedExecutionException(e);
066    } catch (Exception e) {
067      restoreInterruptIfIsInterruptedException(e);
068      throw new ExecutionException(e);
069    } catch (Error e) {
070      throw new ExecutionError(e);
071    }
072  }
073
074  @CanIgnoreReturnValue // TODO(kak): consider removing this
075  @Override
076  @ParametricNullness
077  public <T extends @Nullable Object> T callUninterruptiblyWithTimeout(
078      Callable<T> callable, long timeoutDuration, TimeUnit timeoutUnit) throws ExecutionException {
079    return callWithTimeout(callable, timeoutDuration, timeoutUnit);
080  }
081
082  @Override
083  @SuppressWarnings("CatchingUnchecked") // sneaky checked exception
084  public void runWithTimeout(Runnable runnable, long timeoutDuration, TimeUnit timeoutUnit) {
085    checkNotNull(runnable);
086    checkNotNull(timeoutUnit);
087    try {
088      runnable.run();
089    } catch (Exception e) { // sneaky checked exception
090      throw new UncheckedExecutionException(e);
091    } catch (Error e) {
092      throw new ExecutionError(e);
093    }
094  }
095
096  @Override
097  public void runUninterruptiblyWithTimeout(
098      Runnable runnable, long timeoutDuration, TimeUnit timeoutUnit) {
099    runWithTimeout(runnable, timeoutDuration, timeoutUnit);
100  }
101}