Package org.gradle.api.provider

# Interface Provider<T>

Type Parameters:
  `T` - Type of value represented by provider

All Known Subinterfaces:
  `BinaryProvider<T>`, `DirectoryProperty`, `FileSystemLocationProperty<T>`, `ListProperty<T>`, `MapProperty<K,V>`, `NamedDomainObjectProvider<T>`, `Property<T>`, `RegularFileProperty`, `SetProperty<T>`, `TaskProvider<T>`

@NonExtensible public interface Provider<T>
A container object that provides a value of a specific type. The value can be retrieved using one of the query methods such as `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 represent a task output. Such a provider carries information about the task producing its value. When this provider is attached to an input of another task, Gradle will automatically determine the task dependencies based on this connection.

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:

- A number of Gradle types, such as `Property`, extend `Provider` and can be used directly as a provider.
- Calling `map(Transformer)` to create a new provider from an existing provider.
- Using the return value of `TaskContainer.register(String)`, which is a provider that represents the task instance.
- Using the methods on `Directory` and `DirectoryProperty` to produce file providers.
- By calling `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.

## Configuration Cache

The Configuration Cache handles providers in a special way. When a provider is discovered to be part of the cached build configuration (e.g. when it is a task input), one of two things happen:

- Cache by value. The value of the provider is computed and stored. This typically happens if the provider is derived from purely configuration-time data.
- Cache by computation. The provider is stored as a lazy computation. For example, if the provider is a result of `map(Transformer)`, then the origin provider (on which `map` was called) and the `Transformer` (including its captured variables) are stored. Note that the origin provider may still be cached by value. After loading from cache, the value is only computed if requested (e.g. with `get()`), as usual. This typically happens when the value of the provider is only available at execution time (e.g. derived from a task output) or if it is sourced from the environment (e.g. a system property or an environment variable).

The Configuration Cache operates under the assumption that most of the stored providers will be used at execution time, so it attempts to cache values of as many providers as possible without introducing more build configuration inputs. However, the exact definition of which provider to cache is intentionally left underspecified. For example, even a provider derived from a system property provider may be cached by value if that system property is already an input.

Following the rules below ensures that your Providers remain configuration-cache compatible regardless of applied optimizations.

1. Providers returned by `ProviderFactory.provider(Callable)` are always computed at configuration time. The `Callable` may capture arbitrary data types and freely call configuration-time-only APIs. This is the preferred way to bridge non-lazy and lazy APIs, like passing `Project.getVersion()` into task's input `Property`.
2. Any value returned (provided) by the provider must conform to the configuration cache requirements. This requirement may not be enforced when the value is an intermediate result and the Configuration Cache runs the computation chain to cache the end result.
3. Any value captured by the computation of the provider (e.g. a variable captured by the `Transformer` lambda supplied to `map(Transformer)`) must conform to the configuration cache requirements. This requirement may not be enforced when the Configuration Cache runs the computation chain containing non-conforming transformation to cache the end result.
4. The computation of the provider (except the `Callable` of `ProviderFactory.provider(Callable)`) should not invoke configuration-time only APIs. This requirement may not be enforced when the Configuration Cache runs the computation chain containing non-conforming transformation to cache the end result.
5. The computation of the provider should not use external state (read system properties, environment variables, or files) to avoid introducing accidental build configuration inputs. Preferably, these should be obtained through providers returned by `ProviderFactory` and `zip(Provider, BiFunction)` should be used to mix them into the computation chain. The `Callable` of `ProviderFactory.provider(Callable)` must ignore this requirement if the external state is used by configuration-time-only API because adding an input is unavoidable in this case.

Configuration Cache's support of providers that do not conform to these requirements is unspecified.

Some providers returned by Gradle APIs provide types that cannot be configuration-cached, for example `TaskProvider` or `Provider<Configuration>`. Such providers should not be used as part of the cached build configuration directly, but the providers returned by their `map(Transformer)` or `flatMap(Transformer)` can be if their value or the downstream transformation chain conforms to the requirements above. In most cases it is also safe to add these non-cacheable providers to ConfigurableFileCollections when it makes sense.

Since:
  4.0

- ## Method Summary

  Modifier and Type
  Method
  Description
  `Provider<T>`
  `filter(Spec<? super T> spec)`
  Returns a new `Provider` with the value of this provider if the passed spec is satisfied and no value otherwise.
  `<S> Provider<S>`
  `flatMap(Transformer<? extends @Nullable Provider<? extends S>,? super T> transformer)`
  Returns a new `Provider` from the value of this provider transformed using the given function.
  `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.
  `@Nullable 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 @Nullable 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.
  `<U, R> Provider<R>`
  `zip(Provider<U> right, BiFunction<? super T,? super U,? extends @Nullable R> combiner)`
  Returns a provider which value will be computed by combining this provider value with another provider value using the supplied combiner function.

- ## Method Details

  - ### get

    T get()
    Returns the value of this provider if it has a value present, otherwise throws `java.lang.IllegalStateException`.
    Returns:
      the current value of this provider.
    Throws:
      `IllegalStateException` - if there is no value present

  - ### getOrNull

    @Nullable T getOrNull()
    Returns the value of this provider if it has a value present. Returns `null` a value is not available.
    Returns:
      the value or `null`

  - ### getOrElse

    T getOrElse(T defaultValue)
    Returns the value of this provider if it has a value present. Returns the given default value if a value is not available.
    Returns:
      the value or the default value.
    Since:
      4.3

  - ### map

    <S> Provider<S> map(Transformer<? extends @Nullable S,? super T> transformer)
    Returns a new `Provider` whose value is the value of this provider transformed using the given function.

    The resulting provider will be live, so that each time it is queried, it queries the original (this) provider and applies the transformation to the result. Whenever the original 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.

    Parameters:
      `transformer` - The transformer to apply to values. May return `null`, in which case the provider will have no value.
    Since:
      4.3

  - ### filter

    @Incubating Provider<T> filter(Spec<? super T> spec)
    Returns a new `Provider` with the value of this provider if the passed spec is satisfied and no value otherwise.

    The resulting provider will be live, so that each time it is queried, it queries the original (this) provider and applies the spec to the result. Whenever the original provider has no value, the new provider will also have no value and the spec will not be called.

    Parameters:
      `spec` - The spec to test the value.
    Since:
      8.5

  - ### flatMap

    <S> Provider<S> flatMap(Transformer<? extends @Nullable Provider<? extends S>,? super T> transformer)
    Returns a new `Provider` from the value of this provider transformed using the given function.

    While very similar in functionality to the regular `map` operation, this method offers a convenient way of connecting together task inputs and outputs. (For a deeper understanding of the topic see the Lazy Configuration section of the Gradle manual.)

    Task inputs and outputs often take the form of `providers` or `properties`, the latter being a special case of provider whose value can be changed at will. An example of using `flatMap` for connecting such properties would be following:

         class Producer extends DefaultTask {
             @OutputFile
             abstract RegularFileProperty getOutputFile()

             //irrelevant details omitted
         }

         class Consumer extends DefaultTask {
             @InputFile
             abstract RegularFileProperty getInputFile()

             //irrelevant details omitted
         }

         def producer = tasks.register("producer", Producer)
         def consumer = tasks.register("consumer", Consumer)

         consumer.configure {
             inputFile = producer.flatMap { it.outputFile }
         }

    An added benefit of connecting input and output properties like this is that Gradle can automatically detect task dependencies based on such connections. To make this happen at code level, any task details associated with this provider (the one on which `flatMap` is being called) are ignored. The new provider will use whatever task details are associated with the return value of the transformation.

    The new provider returned by `flatMap` 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.

    Parameters:
      `transformer` - The transformer to apply to values. May return `null`, in which case the provider will have no value.
    Since:
      5.0

  - ### isPresent

    boolean isPresent()
    Returns `true` if there is a value present, otherwise `false`.
    Returns:
      `true` if there is a value present, otherwise `false`

  - ### orElse

    Provider<T> orElse(T value)
    Returns a `Provider` whose value is the value of this provider, if present, otherwise the given default value.
    Parameters:
      `value` - The default value to use when this provider has no value.
    Since:
      5.6

  - ### orElse

    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.
    Parameters:
      `provider` - The provider whose value should be used when this provider has no value.
    Since:
      5.6

  - ### zip

    <U, R> Provider<R> zip(Provider<U> right, BiFunction<? super T,? super U,? extends @Nullable R> combiner)
    Returns a provider which value will be computed by combining this provider value with another provider value using the supplied combiner function.

    The resulting provider will be live, so that each time it is queried, it queries both this and the supplied provider and applies the combiner to the results. Whenever any of the providers has no value, the new provider will also have no value and the combiner will not be called.

    If the supplied providers represents a task or the output of a task, the resulting provider will carry the dependency information.

    Type Parameters:
      `U` - the type of the second provider
      `R` - the type of the result of the combiner
    Parameters:
      `right` - the second provider to combine with
      `combiner` - the combiner of values. May return `null`, in which case the provider will have no value.
    Returns:
      a combined provider
    Since:
      6.6