Coverage Summary for Class: Callables (com.google.common.util.concurrent)
| Class | Method, % | Line, % |
|---|---|---|
| Callables | 0% (0/7) | 0% (0/16) |
| Callables$1 | 0% (0/2) | 0% (0/2) |
| Callables$2 | 0% (0/2) | 0% (0/2) |
| Callables$3 | 0% (0/2) | 0% (0/8) |
| Callables$4 | 0% (0/2) | 0% (0/9) |
| Total | 0% (0/15) | 0% (0/37) |
1 /* 2 * Copyright (C) 2009 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 15 package com.google.common.util.concurrent; 16 17 import static com.google.common.base.Preconditions.checkNotNull; 18 19 import com.google.common.annotations.Beta; 20 import com.google.common.annotations.GwtCompatible; 21 import com.google.common.annotations.GwtIncompatible; 22 import com.google.common.base.Supplier; 23 import java.util.concurrent.Callable; 24 import org.checkerframework.checker.nullness.qual.Nullable; 25 26 /** 27 * Static utility methods pertaining to the {@link Callable} interface. 28 * 29 * @author Isaac Shum 30 * @since 1.0 31 */ 32 @GwtCompatible(emulated = true) 33 @ElementTypesAreNonnullByDefault 34 public final class Callables { 35 private Callables() {} 36 37 /** Creates a {@code Callable} which immediately returns a preset value each time it is called. */ 38 public static <T extends @Nullable Object> Callable<T> returning( 39 @ParametricNullness final T value) { 40 return new Callable<T>() { 41 @Override 42 @ParametricNullness 43 public T call() { 44 return value; 45 } 46 }; 47 } 48 49 /** 50 * Creates an {@link AsyncCallable} from a {@link Callable}. 51 * 52 * <p>The {@link AsyncCallable} returns the {@link ListenableFuture} resulting from {@link 53 * ListeningExecutorService#submit(Callable)}. 54 * 55 * @since 20.0 56 */ 57 @Beta 58 @GwtIncompatible 59 public static <T extends @Nullable Object> AsyncCallable<T> asAsyncCallable( 60 final Callable<T> callable, final ListeningExecutorService listeningExecutorService) { 61 checkNotNull(callable); 62 checkNotNull(listeningExecutorService); 63 return new AsyncCallable<T>() { 64 @Override 65 public ListenableFuture<T> call() throws Exception { 66 return listeningExecutorService.submit(callable); 67 } 68 }; 69 } 70 71 /** 72 * Wraps the given callable such that for the duration of {@link Callable#call} the thread that is 73 * running will have the given name. 74 * 75 * @param callable The callable to wrap 76 * @param nameSupplier The supplier of thread names, {@link Supplier#get get} will be called once 77 * for each invocation of the wrapped callable. 78 */ 79 @GwtIncompatible // threads 80 static <T extends @Nullable Object> Callable<T> threadRenaming( 81 final Callable<T> callable, final Supplier<String> nameSupplier) { 82 checkNotNull(nameSupplier); 83 checkNotNull(callable); 84 return new Callable<T>() { 85 @Override 86 @ParametricNullness 87 public T call() throws Exception { 88 Thread currentThread = Thread.currentThread(); 89 String oldName = currentThread.getName(); 90 boolean restoreName = trySetName(nameSupplier.get(), currentThread); 91 try { 92 return callable.call(); 93 } finally { 94 if (restoreName) { 95 boolean unused = trySetName(oldName, currentThread); 96 } 97 } 98 } 99 }; 100 } 101 102 /** 103 * Wraps the given runnable such that for the duration of {@link Runnable#run} the thread that is 104 * running with have the given name. 105 * 106 * @param task The Runnable to wrap 107 * @param nameSupplier The supplier of thread names, {@link Supplier#get get} will be called once 108 * for each invocation of the wrapped callable. 109 */ 110 @GwtIncompatible // threads 111 static Runnable threadRenaming(final Runnable task, final Supplier<String> nameSupplier) { 112 checkNotNull(nameSupplier); 113 checkNotNull(task); 114 return new Runnable() { 115 @Override 116 public void run() { 117 Thread currentThread = Thread.currentThread(); 118 String oldName = currentThread.getName(); 119 boolean restoreName = trySetName(nameSupplier.get(), currentThread); 120 try { 121 task.run(); 122 } finally { 123 if (restoreName) { 124 boolean unused = trySetName(oldName, currentThread); 125 } 126 } 127 } 128 }; 129 } 130 131 /** Tries to set name of the given {@link Thread}, returns true if successful. */ 132 @GwtIncompatible // threads 133 private static boolean trySetName(final String threadName, Thread currentThread) { 134 /* 135 * setName should usually succeed, but the security manager can prohibit it. Is there a way to 136 * see if we have the modifyThread permission without catching an exception? 137 */ 138 try { 139 currentThread.setName(threadName); 140 return true; 141 } catch (SecurityException e) { 142 return false; 143 } 144 } 145 }