/** * Helper interface to implement both {@link ListenableFuture} and {@link ScheduledFuture}. * * @author Anthony Zana * @since 15.0 */ @Beta @GwtCompatible public interface ListenableScheduledFuture<V> extends ScheduledFuture<V>, ListenableFuture<V> {}
/** * Returns whether {@link #lazyStackTrace} will use the special implementation described in its * documentation. * * @since 19.0 */ @Beta @GwtIncompatible // getStackTraceElementMethod public static boolean lazyStackTraceIsLazy() { return getStackTraceElementMethod != null && getStackTraceDepthMethod != null; }
/** * Returns a fluent iterable that combines several iterables. The returned iterable has an * iterator that traverses the elements of each iterable in {@code inputs}. The input iterators * are not polled until necessary. * * <p>The returned iterable's iterator supports {@code remove()} when the corresponding input * iterator supports it. The methods of the returned iterable may throw {@code * NullPointerException} if any of the input iterators is {@code null}. * * <p><b>{@code Stream} equivalent:</b> {@code streamOfStreams.flatMap(s -> s)} or {@code * streamOfIterables.flatMap(Streams::stream)}. (See {@link Streams#stream}.) * * @since 20.0 */ @Beta public static <T> FluentIterable<T> concat( final Iterable<? extends Iterable<? extends T>> inputs) { checkNotNull(inputs); return new FluentIterable<T>() { @Override public Iterator<T> iterator() { return Iterators.concat(Iterators.transform(inputs.iterator(), Iterables.<T>toIterator())); } }; }
/** * Creates an {@link AsyncCallable} from a {@link Callable}. * * <p>The {@link AsyncCallable} returns the {@link ListenableFuture} resulting from {@link * ListeningExecutorService#submit(Callable)}. * * @since 20.0 */ @Beta @GwtIncompatible public static <T> AsyncCallable<T> asAsyncCallable( final Callable<T> callable, final ListeningExecutorService listeningExecutorService) { checkNotNull(callable); checkNotNull(listeningExecutorService); return new AsyncCallable<T>() { @Override public ListenableFuture<T> call() throws Exception { return listeningExecutorService.submit(callable); } }; }
/** * Returns a comparator of {@link Optional} values which treats {@link Optional#empty} as less * than all other values, and orders the rest using {@code valueComparator} on the contained * value. * * @since 22.0 */ @Beta public static <T> Comparator<Optional<T>> emptiesFirst(Comparator<? super T> valueComparator) { checkNotNull(valueComparator); return Comparator.comparing(o -> o.orElse(null), Comparator.nullsFirst(valueComparator)); }
@Beta @CanIgnoreReturnValue @GwtIncompatible // BlockingQueue @SuppressWarnings("GoodTime") // should accept a java.time.Duration public static <E> int drain( TimeUnit unit) throws InterruptedException { Preconditions.checkNotNull(buffer);
/** * A callback to be used with the streaming {@code readLines} methods. * * <p>{@link #processLine} will be called for each line that is read, and should return {@code * false} when you want to stop processing. * * @author Miles Barr * @since 1.0 */ @Beta @GwtIncompatible public interface LineProcessor<T> { /** * This method will be called once for each line. * * @param line the line read from the input, without delimiter * @return true to continue processing, false to stop */ @CanIgnoreReturnValue // some uses know that their processor never returns false boolean processLine(String line) throws IOException; /** Return the result of processing all the lines. */ T getResult(); }
/** * Any object which can accept registrations of {@link TearDown} instances. * * @author Kevin Bourrillion * @since 10.0 */ @Beta @GwtCompatible public interface TearDownAccepter { /** * Registers a TearDown implementor which will be run after the test proper. * * <p>In JUnit4 language, that means as an {@code @After}. * * <p>In JUnit3 language, that means during the {@link junit.framework.TestCase#tearDown()} step. */ void addTearDown(TearDown tearDown); }
@Beta public static <T> Iterable<T> presentInstances( final Iterable<? extends Optional<? extends T>> optionals) { checkNotNull(optionals); return new Iterable<T>() { @Override
@Beta @CanIgnoreReturnValue @GwtIncompatible // BlockingQueue @SuppressWarnings("GoodTime") // should accept a java.time.Duration public static <E> int drainUninterruptibly( long timeout, TimeUnit unit) { Preconditions.checkNotNull(buffer); long deadline = System.nanoTime() + unit.toNanos(timeout); int added = 0;
/** * Provides equivalent behavior to {@link String#intern} for other immutable types. Common * implementations are available from the {@link Interners} class. * * @author Kevin Bourrillion * @since 3.0 */ @Beta @GwtIncompatible public interface Interner<E> { /** * Chooses and returns the representative instance for any of a collection of instances that are * equal to each other. If two {@linkplain Object#equals equal} inputs are given to this method, * both calls will return the same instance. That is, {@code intern(a).equals(a)} always holds, * and {@code intern(a) == intern(b)} if and only if {@code a.equals(b)}. Note that {@code * intern(a)} is permitted to return one instance now and a different instance later if the * original interned instance was garbage-collected. * * <p><b>Warning:</b> do not use with mutable objects. * * @throws NullPointerException if {@code sample} is null */ @CanIgnoreReturnValue // TODO(cpovirk): Consider removing this? E intern(E sample); }
/** * Static utility methods pertaining to the {@link Runnable} interface. * * @since 16.0 */ @Beta @GwtCompatible public final class Runnables { private static final Runnable EMPTY_RUNNABLE = new Runnable() { @Override public void run() {} }; /** Returns a {@link Runnable} instance that does nothing when run. */ public static Runnable doNothing() { return EMPTY_RUNNABLE; } private Runnables() {} }
/** * Returns a view of the specified {@code CharSequence} as a {@code List<Character>}, viewing * {@code sequence} as a sequence of Unicode code units. The view does not support any * modification operations, but reflects any changes to the underlying character sequence. * * @param sequence the character sequence to view as a {@code List} of characters * @return an {@code List<Character>} view of the character sequence * @since 7.0 */ @Beta public static List<Character> charactersOf(CharSequence sequence) { return new CharSequenceAsList(checkNotNull(sequence)); }
@Beta @GwtIncompatible // NavigableMap public static <K extends Comparable<? super K>, V> NavigableMap<K, V> subMap( NavigableMap<K, V> map, Range<K> range) { return map.headMap(range.upperEndpoint(), range.upperBoundType() == BoundType.CLOSED); return checkNotNull(map);
/** * A callback interface to process bytes from a stream. * * <p>{@link #processBytes} will be called for each chunk of data that is read, and should return * {@code false} when you want to stop processing. * * @author Chris Nokleberg * @since 1.0 */ @Beta @GwtIncompatible public interface ByteProcessor<T> { /** * This method will be called for each chunk of bytes in an input stream. The implementation * should process the bytes from {@code buf[off]} through {@code buf[off + len - 1]} (inclusive). * * @param buf the byte array containing the data to process * @param off the initial offset into the array * @param len the length of data to be processed * @return true to continue processing, false to stop */ @CanIgnoreReturnValue // some uses know that their processor never returns false boolean processBytes(byte[] buf, int off, int len) throws IOException; /** Return the result of processing all the bytes. */ T getResult(); }
/** * Simple utility for when you want to create a {@link TearDown} that may throw an exception but * should not fail a test when it does. (The behavior of a {@code TearDown} that throws an exception * varies; see its documentation for details.) Use it just like a {@code TearDown}, except override * {@link #sloppyTearDown()} instead. * * @author Luiz-Otavio Zorzella * @since 10.0 */ @Beta @GwtCompatible public abstract class SloppyTearDown implements TearDown { private static final Logger logger = Logger.getLogger(SloppyTearDown.class.getName()); @Override public final void tearDown() { try { sloppyTearDown(); } catch (Throwable t) { logger.log(Level.INFO, "exception thrown during tearDown: " + t.getMessage(), t); } } public abstract void sloppyTearDown() throws Exception; }
/** * Returns a comparator of {@link Optional} values which treats {@link Optional#empty} as greater * than all other values, and orders the rest using {@code valueComparator} on the contained * value. * * @since 22.0 */ @Beta public static <T> Comparator<Optional<T>> emptiesLast(Comparator<? super T> valueComparator) { checkNotNull(valueComparator); return Comparator.comparing(o -> o.orElse(null), Comparator.nullsLast(valueComparator)); } }
@Beta @GwtIncompatible // NavigableSet public static <K extends Comparable<? super K>> NavigableSet<K> subSet( NavigableSet<K> set, Range<K> range) { return set.headSet(range.upperEndpoint(), range.upperBoundType() == BoundType.CLOSED); return checkNotNull(set);
@Beta @GwtIncompatible public interface ListeningScheduledExecutorService extends ScheduledExecutorService, ListeningExecutorService {
/** * Helper interface to implement both {@link ListenableFuture} and {@link ScheduledFuture}. * * @author Anthony Zana * @since 15.0 */ @Beta @GwtCompatible public interface ListenableScheduledFuture<V> extends ScheduledFuture<V>, ListenableFuture<V> {}