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; 025 026/** 027 * A TimeLimiter implementation which actually does not attempt to limit time at all. This may be 028 * desirable to use in some unit tests. More importantly, attempting to debug a call which is 029 * time-limited would be extremely annoying, so this gives you a time-limiter you can easily swap in 030 * for your real time-limiter while you're debugging. 031 * 032 * @author Kevin Bourrillion 033 * @author Jens Nyman 034 * @since 1.0 035 */ 036@Beta 037@CanIgnoreReturnValue 038@GwtIncompatible 039public final class FakeTimeLimiter implements TimeLimiter { 040 @Override 041 public <T> T newProxy( 042 T target, Class<T> interfaceType, long timeoutDuration, TimeUnit timeoutUnit) { 043 checkNotNull(target); 044 checkNotNull(interfaceType); 045 checkNotNull(timeoutUnit); 046 return target; // ha ha 047 } 048 049 @Deprecated 050 @Override 051 public <T> T callWithTimeout( 052 Callable<T> callable, long timeoutDuration, TimeUnit timeoutUnit, boolean amInterruptible) 053 throws Exception { 054 checkNotNull(timeoutUnit); 055 return callable.call(); // fooled you 056 } 057 058 @Override 059 public <T> T callWithTimeout(Callable<T> callable, long timeoutDuration, TimeUnit timeoutUnit) 060 throws ExecutionException { 061 checkNotNull(callable); 062 checkNotNull(timeoutUnit); 063 try { 064 return callable.call(); 065 } catch (RuntimeException e) { 066 throw new UncheckedExecutionException(e); 067 } catch (Exception e) { 068 throw new ExecutionException(e); 069 } catch (Error e) { 070 throw new ExecutionError(e); 071 } catch (Throwable e) { 072 // It's a non-Error, non-Exception Throwable. Such classes are usually intended to extend 073 // Exception, so we'll treat it like an Exception. 074 throw new ExecutionException(e); 075 } 076 } 077 078 @Override 079 public <T> T callUninterruptiblyWithTimeout( 080 Callable<T> callable, long timeoutDuration, TimeUnit timeoutUnit) throws ExecutionException { 081 return callWithTimeout(callable, timeoutDuration, timeoutUnit); 082 } 083 084 @Override 085 public void runWithTimeout(Runnable runnable, long timeoutDuration, TimeUnit timeoutUnit) { 086 checkNotNull(runnable); 087 checkNotNull(timeoutUnit); 088 try { 089 runnable.run(); 090 } catch (RuntimeException e) { 091 throw new UncheckedExecutionException(e); 092 } catch (Error e) { 093 throw new ExecutionError(e); 094 } catch (Throwable e) { 095 // It's a non-Error, non-Exception Throwable. Such classes are usually intended to extend 096 // Exception, so we'll treat it like a RuntimeException. 097 throw new UncheckedExecutionException(e); 098 } 099 } 100 101 @Override 102 public void runUninterruptiblyWithTimeout( 103 Runnable runnable, long timeoutDuration, TimeUnit timeoutUnit) { 104 runWithTimeout(runnable, timeoutDuration, timeoutUnit); 105 } 106}