Optional (Groovy JDK enhancements)

Package: java.util

## Class Optional

- - ### Methods Summary

    Methods
    Return type | Name and parameters
    --- | ---
    `boolean` | `asBoolean()` Coerce an `Optional` instance to a `boolean` value.
    `Optional` | `collect(Closure transform)` If the optional contains a value, returns an optional containing the transformed value obtained using the `transform` closure or otherwise an empty optional.
    `Optional` | `filter(Class type)` Tests given value against specified type and changes generics of result.
    `OptionalDouble` | `mapToDouble(ToDoubleFunction mapper)` If a value is present in the `OptionalDouble`, returns an `OptionalDouble` consisting of the result of applying the given function to the value or else empty.
    `OptionalInt` | `mapToInt(ToIntFunction mapper)` If a value is present in the `OptionalInt`, returns an `OptionalInt` consisting of the result of applying the given function to the value or else empty.
    `OptionalLong` | `mapToLong(ToLongFunction mapper)` If a value is present in the `OptionalLong`, returns an `OptionalLong` consisting of the result of applying the given function to the value or else empty.
    `Optional` | `orOptional(Supplier supplier)` Provides similar functionality to JDK9 `or` on JDK8.
    `Stream` | `stream()` If a value is present in the Optional, returns a Stream with the value as its source or else an empty stream.

  - #### Methods inherited from class java.lang.Object

    `addShutdownHook`, `any`, `any`, `asBoolean`, `asType`, `collect`, `collect`, `collect`, `dump`, `each`, `eachMatch`, `eachMatch`, `eachWithIndex`, `every`, `every`, `find`, `find`, `findAll`, `findAll`, `findIndexOf`, `findIndexOf`, `findIndexValues`, `findIndexValues`, `findLastIndexOf`, `findLastIndexOf`, `findResult`, `findResult`, `findResult`, `findResult`, `getAt`, `getMetaClass`, `getMetaPropertyValues`, `getProperties`, `grep`, `grep`, `hasProperty`, `identity`, `inject`, `inject`, `inspect`, `invokeMethod`, `is`, `isCase`, `isNotCase`, `iterator`, `metaClass`, `print`, `print`, `printf`, `printf`, `println`, `println`, `println`, `putAt`, `respondsTo`, `respondsTo`, `setMetaClass`, `split`, `sprintf`, `sprintf`, `stream`, `tap`, `toString`, `use`, `use`, `use`, `with`, `with`, `withCloseable`, `withStream`, `withTraits`

- - ### Methods Detail

    - #### public boolean asBoolean()

      Coerce an `Optional` instance to a `boolean` value.

          assert !Optional.empty().asBoolean()
          assert Optional.of(1234).asBoolean()

      Returns:
        `true` if a value is present, otherwise `false`
      Since:
        2.5.0

    - #### public Optional collect(Closure transform)

      If the optional contains a value, returns an optional containing the transformed value obtained using the `transform` closure or otherwise an empty optional.

          assert Optional.of("foobar").collect{ it.size() }.get() == 6
          assert !Optional.empty().collect{ it.size() }.isPresent()

      Parameters:
        `transform` - the closure used to transform the optional value if present
      Returns:
        an Optional containing the transformed value or empty if the optional is empty or the transform returns null
      Since:
        3.0.0

    - #### public Optional filter(Class type)

      Tests given value against specified type and changes generics of result. This is equivalent to: `self.filter(it -> it instanceof Type).map(it -> (Type) it)`

          assert !Optional.empty().filter(Number).isPresent()
          assert !Optional.of('x').filter(Number).isPresent()
          assert Optional.of(1234).filter(Number).isPresent()
          assert Optional.of(1234).filter(Number).get().equals(1234)

      Parameters:
      Since:
        3.0.0

    - #### public OptionalDouble mapToDouble(ToDoubleFunction mapper)

      If a value is present in the `OptionalDouble`, returns an `OptionalDouble` consisting of the result of applying the given function to the value or else empty.

          assert !Optional.empty().mapToDouble(x -> Math.PI).isPresent()
          assert  Optional.of('x').mapToDouble(x -> Math.PI).getAsDouble() == Math.PI

      Parameters:
      Since:
        3.0.0

    - #### public OptionalInt mapToInt(ToIntFunction mapper)

      If a value is present in the `OptionalInt`, returns an `OptionalInt` consisting of the result of applying the given function to the value or else empty.

          assert !Optional.empty().mapToInt(x -> 42).isPresent()
          assert  Optional.of('x').mapToInt(x -> 42).getAsInt() == 42

      Parameters:
      Since:
        3.0.0

    - #### public OptionalLong mapToLong(ToLongFunction mapper)

      If a value is present in the `OptionalLong`, returns an `OptionalLong` consisting of the result of applying the given function to the value or else empty.

          assert !Optional.empty().mapToLong(x -> 42L).isPresent()
          assert  Optional.of('x').mapToLong(x -> 42L).getAsLong() == 42L

      Parameters:
      Since:
        3.0.0

    - #### public Optional orOptional(Supplier supplier)

      Provides similar functionality to JDK9 `or` on JDK8.

          def x = Optional.empty()
          def y = Optional.of('y')
          assert y.orOptional(() -> Optional.of('z')).get() == 'y'
          assert x.orOptional(() -> Optional.of('z')).get() == 'z'

      Parameters:
      Since:
        3.0.6

    - #### public Stream stream()

      If a value is present in the Optional, returns a Stream with the value as its source or else an empty stream.

      Since:
        3.0.0