Top Description Inners Constructors Methods
com.google.common.base

public final Class Suppliers

extends Object
Class Inheritance
Annotations
@GwtCompatible
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.NullnessCasts.uncheckedCastNullableTToT, .Preconditions.checkArgument, .Preconditions.checkNotNull

Useful suppliers.

All methods return serializable suppliers as long as they're given serializable parameters.

Authors
Laurence Gonsalves, Harry Heymann
Since
2.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
pack-priv static class
pack-priv static class
private static class
private static interface
private static enum
private static class
private static class

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static <F extends @Nullable Object, T extends @Nullable Object> Supplier<T>
compose(Function<? super F, T> function, Supplier<F> supplier)

Returns a new supplier which is the composition of the provided function and supplier.

public static <T extends @Nullable Object> Supplier<T>
memoize(Supplier<T> delegate)

Returns a supplier which caches the instance retrieved during the first call to get() and returns that value on subsequent calls to get().

public static <T extends @Nullable Object> Supplier<T>
memoizeWithExpiration(Supplier<T> delegate, long
the length of time after a value is created that it should stop being returned by subsequent get() calls
duration
,
TimeUnit
the unit that duration is expressed in
unit
)

Returns a supplier that caches the instance supplied by the delegate and removes the cached value after the specified time has passed.

public static <T extends @Nullable Object> Supplier<T>
ofInstance(T instance)

Returns a supplier that always supplies instance.

public static <T extends @Nullable Object> Function<Supplier<T>, T>
supplierFunction()

Returns a function that accepts a supplier and returns the result of invoking Supplier#get on that supplier.

public static <T extends @Nullable Object> Supplier<T>
synchronizedSupplier(Supplier<T> delegate)

Returns a supplier whose get() method synchronizes on delegate before calling it, making it thread-safe.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

Suppliersback to summary
private Suppliers()

Method Detail

composeback to summary
public static <F extends @Nullable Object, T extends @Nullable Object> Supplier<T> compose(Function<? super F, T> function, Supplier<F> supplier)

Returns a new supplier which is the composition of the provided function and supplier. In other words, the new supplier's value will be computed by retrieving the value from supplier, and then applying function to that value. Note that the resulting supplier will not call supplier or invoke function until it is called.

memoizeback to summary
public static <T extends @Nullable Object> Supplier<T> memoize(Supplier<T> delegate)

Returns a supplier which caches the instance retrieved during the first call to get() and returns that value on subsequent calls to get(). See: memoization

The returned supplier is thread-safe. The delegate's get() method will be invoked at most once unless the underlying get() throws an exception. The supplier's serialized form does not contain the cached value, which will be recalculated when get() is called on the deserialized instance.

When the underlying delegate throws an exception then this memoizing supplier will keep delegating calls until it returns valid data.

If delegate is an instance created by an earlier call to memoize, it is returned directly.

memoizeWithExpirationback to summary
public static <T extends @Nullable Object> Supplier<T> memoizeWithExpiration(Supplier<T> delegate, long duration, TimeUnit unit)

Returns a supplier that caches the instance supplied by the delegate and removes the cached value after the specified time has passed. Subsequent calls to get() return the cached value if the expiration time has not passed. After the expiration time, a new value is retrieved, cached, and returned. See: memoization

The returned supplier is thread-safe. The supplier's serialized form does not contain the cached value, which will be recalculated when get() is called on the reserialized instance. The actual memoization does not happen when the underlying delegate throws an exception.

When the underlying delegate throws an exception then this memoizing supplier will keep delegating calls until it returns valid data.

Parameters
duration:long

the length of time after a value is created that it should stop being returned by subsequent get() calls

unit:TimeUnit

the unit that duration is expressed in

Annotations
@SuppressWarnings:GoodTime
Exceptions
IllegalArgumentException:
if duration is not positive
Since
2.0
ofInstanceback to summary
public static <T extends @Nullable Object> Supplier<T> ofInstance(T instance)

Returns a supplier that always supplies instance.

supplierFunctionback to summary
public static <T extends @Nullable Object> Function<Supplier<T>, T> supplierFunction()

Returns a function that accepts a supplier and returns the result of invoking Supplier#get on that supplier.

Java 8 users: use the method reference Supplier::get instead.

Since
8.0
synchronizedSupplierback to summary
public static <T extends @Nullable Object> Supplier<T> synchronizedSupplier(Supplier<T> delegate)

Returns a supplier whose get() method synchronizes on delegate before calling it, making it thread-safe.

com.google.common.base back to summary

pack-priv Class Suppliers.ExpiringMemoizingSupplier<T extends @Nullable Object>

extends Object
implements Supplier<T extends @Nullable Object>, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, com.google.common.base.Supplier, java.util.function.Supplier
Annotations
@VisibleForTesting
@SuppressWarnings:GoodTime

Field Summary

Modifier and TypeField and Description
pack-priv final Supplier<T>
pack-priv final long
pack-priv transient volatile long
private static final long
pack-priv transient volatile T

Constructor Summary

AccessConstructor and Description
pack-priv
ExpiringMemoizingSupplier(Supplier<T> delegate, long duration, TimeUnit unit)

Method Summary

Modifier and TypeMethod and Description
public T
get()

Implements com.google.common.base.Supplier.get.

Retrieves an instance of the appropriate type.
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

delegateback to summary
pack-priv final Supplier<T> delegate
durationNanosback to summary
pack-priv final long durationNanos
expirationNanosback to summary
pack-priv transient volatile long expirationNanos
serialVersionUIDback to summary
private static final long serialVersionUID
valueback to summary
pack-priv transient volatile T value
Annotations
@CheckForNull

Constructor Detail

ExpiringMemoizingSupplierback to summary
pack-priv ExpiringMemoizingSupplier(Supplier<T> delegate, long duration, TimeUnit unit)

Method Detail

getback to summary
public T get()

Implements com.google.common.base.Supplier.get.

Doc from com.google.common.base.Supplier.get.

Retrieves an instance of the appropriate type. The returned object may or may not be a new instance, depending on the implementation.

Returns:T

an instance of the appropriate type

Annotations
@Override
@ParametricNullness
@SuppressWarnings:GoodTime
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override
com.google.common.base back to summary

pack-priv Class Suppliers.MemoizingSupplier<T extends @Nullable Object>

extends Object
implements Supplier<T extends @Nullable Object>, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, com.google.common.base.Supplier, java.util.function.Supplier
Annotations
@VisibleForTesting

Field Summary

Modifier and TypeField and Description
pack-priv final Supplier<T>
pack-priv transient volatile boolean
private static final long
pack-priv transient T

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public T
get()

Implements com.google.common.base.Supplier.get.

Retrieves an instance of the appropriate type.
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

delegateback to summary
pack-priv final Supplier<T> delegate
initializedback to summary
pack-priv transient volatile boolean initialized
serialVersionUIDback to summary
private static final long serialVersionUID
valueback to summary
pack-priv transient T value
Annotations
@CheckForNull

Constructor Detail

MemoizingSupplierback to summary
pack-priv MemoizingSupplier(Supplier<T> delegate)

Method Detail

getback to summary
public T get()

Implements com.google.common.base.Supplier.get.

Doc from com.google.common.base.Supplier.get.

Retrieves an instance of the appropriate type. The returned object may or may not be a new instance, depending on the implementation.

Returns:T

an instance of the appropriate type

Annotations
@Override
@ParametricNullness
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override
com.google.common.base back to summary

pack-priv Class Suppliers.NonSerializableMemoizingSupplier<T extends @Nullable Object>

extends Object
implements Supplier<T extends @Nullable Object>
Class Inheritance
  • java.lang.Object
  • com.google.common.base.Suppliers.NonSerializableMemoizingSupplier
All Implemented Interfaces
com.google.common.base.Supplier, java.util.function.Supplier
Annotations
@VisibleForTesting

Field Summary

Modifier and TypeField and Description
private volatile Supplier<T>
private static final Supplier<Void>
private T

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public T
get()

Implements com.google.common.base.Supplier.get.

Retrieves an instance of the appropriate type.
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

delegateback to summary
private volatile Supplier<T> delegate
SUCCESSFULLY_COMPUTEDback to summary
private static final Supplier<Void> SUCCESSFULLY_COMPUTED
Annotations
@SuppressWarnings:UnnecessaryLambda
valueback to summary
private T value
Annotations
@CheckForNull

Constructor Detail

NonSerializableMemoizingSupplierback to summary
pack-priv NonSerializableMemoizingSupplier(Supplier<T> delegate)

Method Detail

getback to summary
public T get()

Implements com.google.common.base.Supplier.get.

Doc from com.google.common.base.Supplier.get.

Retrieves an instance of the appropriate type. The returned object may or may not be a new instance, depending on the implementation.

Returns:T

an instance of the appropriate type

Annotations
@Override
@ParametricNullness
@SuppressWarnings:unchecked
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override
com.google.common.base back to summary

private Class Suppliers.SupplierComposition<F extends @Nullable Object, T extends @Nullable Object>

extends Object
implements Supplier<T extends @Nullable Object>, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, com.google.common.base.Supplier, java.util.function.Supplier

Field Summary

Modifier and TypeField and Description
pack-priv final Function<? super F, T>
private static final long
pack-priv final Supplier<F>

Constructor Summary

AccessConstructor and Description
pack-priv
SupplierComposition(Function<? super F, T> function, Supplier<F> supplier)

Method Summary

Modifier and TypeMethod and Description
public boolean
equals(Object
the reference object with which to compare.
obj
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.
public T
get()

Implements com.google.common.base.Supplier.get.

Retrieves an instance of the appropriate type.
public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for the object.
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

functionback to summary
pack-priv final Function<? super F, T> function
serialVersionUIDback to summary
private static final long serialVersionUID
supplierback to summary
pack-priv final Supplier<F> supplier

Constructor Detail

SupplierCompositionback to summary
pack-priv SupplierComposition(Function<? super F, T> function, Supplier<F> supplier)

Method Detail

equalsback to summary
public boolean equals(Object obj)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
obj:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
getback to summary
public T get()

Implements com.google.common.base.Supplier.get.

Doc from com.google.common.base.Supplier.get.

Retrieves an instance of the appropriate type. The returned object may or may not be a new instance, depending on the implementation.

Returns:T

an instance of the appropriate type

Annotations
@Override
@ParametricNullness
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object.

Annotations
@Override
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override
com.google.common.base back to summary

private Interface Suppliers.SupplierFunction<T extends @Nullable Object>

extends Function<Supplier<T extends @Nullable Object>, T extends @Nullable Object>
Known Direct Implementers
com.google.common.base.Suppliers.SupplierFunctionImpl

Method Summary

Inherited from com.google.common.base.Function:
applyequals
com.google.common.base back to summary

private final Enum Suppliers.SupplierFunctionImpl

extends Enum<Suppliers.SupplierFunctionImpl>
implements Suppliers.SupplierFunction<@Nullable Object>
Class Inheritance
All Implemented Interfaces
com.google.common.base.Suppliers.SupplierFunction, com.google.common.base.Function, java.util.function.Function

Field Summary

Modifier and TypeField and Description
public static final Suppliers.SupplierFunctionImpl

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public Object
apply(Supplier<@Nullable Object>
the function argument
input
)

Implements com.google.common.base.Function.apply.

Applies this function to the given argument.
public String
toString()

Overrides java.lang.Enum.toString.

Returns the name of this enum constant, as contained in the declaration.
public static Suppliers.SupplierFunctionImpl
public static Suppliers.SupplierFunctionImpl[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinalvalueOf

Field Detail

INSTANCEback to summary
public static final Suppliers.SupplierFunctionImpl INSTANCE

Constructor Detail

SupplierFunctionImplback to summary
private SupplierFunctionImpl()

Method Detail

applyback to summary
public Object apply(Supplier<@Nullable Object> input)

Implements com.google.common.base.Function.apply.

Doc from java.util.function.Function.apply.

Applies this function to the given argument.

Parameters
input:Supplier<@Nullable Object>

the function argument

Returns:Object

the function result

Annotations
@Override
@CheckForNull
toStringback to summary
public String toString()

Overrides java.lang.Enum.toString.

Doc from java.lang.Enum.toString.

Returns the name of this enum constant, as contained in the declaration. This method may be overridden, though it typically isn't necessary or desirable. An enum class should override this method when a more "programmer-friendly" string form exists.

Returns:String

the name of this enum constant

Annotations
@Override
valueOfback to summary
public static Suppliers.SupplierFunctionImpl valueOf(String name)
valuesback to summary
public static Suppliers.SupplierFunctionImpl[] values()
com.google.common.base back to summary

private Class Suppliers.SupplierOfInstance<T extends @Nullable Object>

extends Object
implements Supplier<T extends @Nullable Object>, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, com.google.common.base.Supplier, java.util.function.Supplier

Field Summary

Modifier and TypeField and Description
pack-priv final T
private static final long

Constructor Summary

AccessConstructor and Description
pack-priv
SupplierOfInstance(T instance)

Method Summary

Modifier and TypeMethod and Description
public boolean
equals(Object
the reference object with which to compare.
obj
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.
public T
get()

Implements com.google.common.base.Supplier.get.

Retrieves an instance of the appropriate type.
public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for the object.
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

instanceback to summary
pack-priv final T instance
Annotations
@ParametricNullness
serialVersionUIDback to summary
private static final long serialVersionUID

Constructor Detail

SupplierOfInstanceback to summary
pack-priv SupplierOfInstance(T instance)

Method Detail

equalsback to summary
public boolean equals(Object obj)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
obj:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
getback to summary
public T get()

Implements com.google.common.base.Supplier.get.

Doc from com.google.common.base.Supplier.get.

Retrieves an instance of the appropriate type. The returned object may or may not be a new instance, depending on the implementation.

Returns:T

an instance of the appropriate type

Annotations
@Override
@ParametricNullness
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object.

Annotations
@Override
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override
com.google.common.base back to summary

private Class Suppliers.ThreadSafeSupplier<T extends @Nullable Object>

extends Object
implements Supplier<T extends @Nullable Object>, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, com.google.common.base.Supplier, java.util.function.Supplier

Field Summary

Modifier and TypeField and Description
pack-priv final Supplier<T>
private static final long

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public T
get()

Implements com.google.common.base.Supplier.get.

Retrieves an instance of the appropriate type.
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

delegateback to summary
pack-priv final Supplier<T> delegate
serialVersionUIDback to summary
private static final long serialVersionUID

Constructor Detail

ThreadSafeSupplierback to summary
pack-priv ThreadSafeSupplier(Supplier<T> delegate)

Method Detail

getback to summary
public T get()

Implements com.google.common.base.Supplier.get.

Doc from com.google.common.base.Supplier.get.

Retrieves an instance of the appropriate type. The returned object may or may not be a new instance, depending on the implementation.

Returns:T

an instance of the appropriate type

Annotations
@Override
@ParametricNullness
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override