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 @Override 050 public <T> T callWithTimeout(Callable<T> callable, long timeoutDuration, TimeUnit timeoutUnit) 051 throws ExecutionException { 052 checkNotNull(callable); 053 checkNotNull(timeoutUnit); 054 try { 055 return callable.call(); 056 } catch (RuntimeException e) { 057 throw new UncheckedExecutionException(e); 058 } catch (Exception e) { 059 throw new ExecutionException(e); 060 } catch (Error e) { 061 throw new ExecutionError(e); 062 } catch (Throwable e) { 063 // It's a non-Error, non-Exception Throwable. Such classes are usually intended to extend 064 // Exception, so we'll treat it like an Exception. 065 throw new ExecutionException(e); 066 } 067 } 068 069 @Override 070 public <T> T callUninterruptiblyWithTimeout( 071 Callable<T> callable, long timeoutDuration, TimeUnit timeoutUnit) throws ExecutionException { 072 return callWithTimeout(callable, timeoutDuration, timeoutUnit); 073 } 074 075 @Override 076 public void runWithTimeout(Runnable runnable, long timeoutDuration, TimeUnit timeoutUnit) { 077 checkNotNull(runnable); 078 checkNotNull(timeoutUnit); 079 try { 080 runnable.run(); 081 } catch (RuntimeException e) { 082 throw new UncheckedExecutionException(e); 083 } catch (Error e) { 084 throw new ExecutionError(e); 085 } catch (Throwable e) { 086 // It's a non-Error, non-Exception Throwable. Such classes are usually intended to extend 087 // Exception, so we'll treat it like a RuntimeException. 088 throw new UncheckedExecutionException(e); 089 } 090 } 091 092 @Override 093 public void runUninterruptiblyWithTimeout( 094 Runnable runnable, long timeoutDuration, TimeUnit timeoutUnit) { 095 runWithTimeout(runnable, timeoutDuration, timeoutUnit); 096 } 097}