T
- Type of value represented by providerBinaryProvider<T>
, DirectoryProperty
, FileSystemLocationProperty<T>
, ListProperty<T>
, MapProperty<K,V>
, NamedDomainObjectProvider<T>
, Property<T>
, RegularFileProperty
, SetProperty<T>
, TaskProvider<T>
@NonExtensible public interface Provider<T>
get()
or getOrNull()
.
A provider may not always have a value available, for example when the value may not yet be known but will be known at some point in the future.
When a value is not available, isPresent()
returns false
and retrieving the value will fail with an exception.
A provider may not always provide the same value. Although there are no methods on this interface to change the value, the provider implementation may be mutable or use values from some changing source. A provider may also provide a value that is mutable and that changes over time.
A provider may be used to represent a task output. Such a provider carries information about which task produces its value. When attached to a task input, this allows Gradle to automatically add dependencies between tasks based on the values they use as inputs and produce as outputs.
A typical use of a provider is to pass values from one Gradle model element to another, e.g. from a project extension to a task, or between tasks. Providers also allow expensive computations to be deferred until their value is actually needed, usually at task execution time.
There are a number of ways to create a Provider
instance. Some common methods:
Property
, extend Provider
and can be used directly as a provider.map(Transformer)
to create a new provider from an existing provider.TaskContainer.register(String)
, which is a provider that represents the task instance.Directory
and DirectoryProperty
to produce file providers.ProviderFactory.provider(Callable)
or Project.provider(Callable)
to create a new provider from a Callable
.
For a provider whose value can be mutated, see Property
and the methods on ObjectFactory
.
Note: This interface is not intended for implementation by build script or plugin authors.
Modifier and Type | Method | Description |
---|---|---|
<S> Provider<S> |
flatMap(Transformer<? extends Provider<? extends S>,? super T> transformer) |
Returns a new
Provider from the value of this provider transformed using the given function. |
Provider<T> |
forUseAtConfigurationTime() |
Returns a view of this
Provider which can be safely read at configuration time. |
T |
get() |
Returns the value of this provider if it has a value present, otherwise throws
java.lang.IllegalStateException . |
T |
getOrElse(T defaultValue) |
Returns the value of this provider if it has a value present.
|
T |
getOrNull() |
Returns the value of this provider if it has a value present.
|
boolean |
isPresent() |
Returns
true if there is a value present, otherwise false . |
<S> Provider<S> |
map(Transformer<? extends S,? super T> transformer) |
Returns a new
Provider whose value is the value of this provider transformed using the given function. |
Provider<T> |
orElse(Provider<? extends T> provider) |
Returns a
Provider whose value is the value of this provider, if present, otherwise uses the value from the given provider, if present. |
Provider<T> |
orElse(T value) |
Returns a
Provider whose value is the value of this provider, if present, otherwise the given default value. |
<B,R> |
zip(Provider<B> right,
BiFunction<T,B,R> combiner) |
Returns a provider which value will be computed by combining this provider value with another
provider value using the supplied combiner function.
|
T get()
java.lang.IllegalStateException
.IllegalStateException
- if there is no value present@Nullable T getOrNull()
null
a value is not available.null
T getOrElse(T defaultValue)
<S> Provider<S> map(Transformer<? extends S,? super T> transformer)
Provider
whose value is the value of this provider transformed using the given function.
The new provider will be live, so that each time it is queried, it queries this provider and applies the transformation to the result. Whenever this provider has no value, the new provider will also have no value and the transformation will not be called.
When this provider represents a task or the output of a task, the new provider will be considered an output of the task and will carry dependency information that Gradle can use to automatically attach task dependencies to tasks that use the new provider for input values.
transformer
- The transformer to apply to values. May return null
, in which case the provider will have no value.@Incubating <S> Provider<S> flatMap(Transformer<? extends Provider<? extends S>,? super T> transformer)
Provider
from the value of this provider transformed using the given function.
The new provider will be live, so that each time it is queried, it queries this provider and applies the transformation to the result. Whenever this provider has no value, the new provider will also have no value and the transformation will not be called.
Any task details associated with this provider are ignored. The new provider will use whatever task details are associated with the return value of the function.
transformer
- The transformer to apply to values. May return null
, in which case the provider will have no value.boolean isPresent()
true
if there is a value present, otherwise false
.true
if there is a value present, otherwise false
@Incubating Provider<T> orElse(T value)
Provider
whose value is the value of this provider, if present, otherwise the given default value.value
- The default value to use when this provider has no value.@Incubating Provider<T> orElse(Provider<? extends T> provider)
Provider
whose value is the value of this provider, if present, otherwise uses the value from the given provider, if present.provider
- The provider whose value should be used when this provider has no value.@Incubating Provider<T> forUseAtConfigurationTime()
Provider
which can be safely read at configuration time.@Incubating <B,R> Provider<R> zip(Provider<B> right, BiFunction<T,B,R> combiner)
B
- the type of the second providerR
- the type of the result of the combinerright
- the second provider to combine withcombiner
- the combiner of values