com.twitter.common.base
Class Closures

java.lang.Object
  extended by com.twitter.common.base.Closures

public final class Closures
extends Object

Utilities for dealing with Closures.


Method Summary
static
<T> Function<T,Void>
asFunction(ExceptionalClosure<T,?> closure)
          Converts a closure into a function returning null.
static
<T> Closure<T>
combine(Closure<T>... closures)
          Varargs equivalent of combine(Iterable).
static
<T> Closure<T>
combine(Iterable<Closure<T>> closures)
          Combines multiple closures into a single closure, whose calls are replicated sequentially in the order that they were provided.
static
<T> Closure<T>
filter(Predicate<T> filter, Closure<T> closure)
          Applies a filter to a closure, such that the closure will only be called when the filter is satisfied (returns true}.
static
<T> Closure<T>
noop()
          Returns a closure that will do nothing.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

asFunction

public static <T> Function<T,Void> asFunction(ExceptionalClosure<T,?> closure)
Converts a closure into a function returning null.


combine

public static <T> Closure<T> combine(Closure<T>... closures)
Varargs equivalent of combine(Iterable).

Type Parameters:
T - Type accepted by the closures.
Parameters:
closures - Closures to combine.
Returns:
A single closure that will fan out all calls to ExceptionalClosure.execute(Object) to the wrapped closures.

combine

public static <T> Closure<T> combine(Iterable<Closure<T>> closures)
Combines multiple closures into a single closure, whose calls are replicated sequentially in the order that they were provided. If an exception is encountered from a closure it propagates to the top-level closure and the remaining closures are not executed.

Type Parameters:
T - Type accepted by the closures.
Parameters:
closures - Closures to combine.
Returns:
A single closure that will fan out all calls to ExceptionalClosure.execute(Object) to the wrapped closures.

filter

public static <T> Closure<T> filter(Predicate<T> filter,
                                    Closure<T> closure)
Applies a filter to a closure, such that the closure will only be called when the filter is satisfied (returns true}.

Type Parameters:
T - Type handled by the filter and the closure.
Parameters:
filter - Filter to determine when closure is called.
closure - Closure to filter.
Returns:
A filtered closure.

noop

public static <T> Closure<T> noop()
Returns a closure that will do nothing.

Type Parameters:
T - The closure argument type.
Returns:
A closure that does nothing.