Force-Framework

Mark Brennand

OptionalV1

global with sharing class OptionalV1
Description

Class providing an implementation of Optional which should be used in preference to using null to indicate a value is un-assigned.

See README for full details of Optional.

Copyright (c) 2025 Mark Brennand, released under MIT License.

AuthorMark Brennand

Methods

  collection

global ImmutableV1.Collection collection()
Description

Returns the value assigned to the Optional as a Collection allowing the functionality of ArrayV1 to be used.

If the Optional is un-assigned, a NoSuchElementException is thrown.

ReturnsA Collection representing the value.

  collection

global ImmutableV1.Collection collection(final Object other)
Description

Returns the value assigned to the Optional as a Collection allowing the functionality of ArrayV1 to be used.

If the Optional is un-assigned, the collection represented by the argument is used.

Parameterother: Fallback collection to use if Optional has no value.
ReturnsA Collection representing the value.

  empty

global static OptionalV1 empty()
Description

Returns an un-assigned Optional.

ReturnsAn un-assigned Optional.

  get

global Object get()
Description

Returns the value assigned to the Optional.

If the Optional is un-assigned, a NoSuchElementException is thrown.

ReturnsThe value assigned to the Optional.

  isPresent

global Boolean isPresent()
Description

Returns true if the Optional has a value, false if not.

ReturnsTrue if the Optional has a value.

  of

global static OptionalV1 of(final Object value)
Description

Returns an Optional assigned to a value.

Parametervalue: The value to assign the the Optional.
ReturnsThe assigned Optional.

  ofNullable

global static OptionalV1 ofNullable(final Object value)
Description

Returns an Optional assigned to a value or an unassigned Optional if the argument is null.

Parametervalue: The value to assign the the Optional.
ReturnsThe assigned Optional.

  orElse

global Object orElse(final Object other)
Description

Returns the Optional value if assigned, the value of the other argument if not.

Parameterother: The value to return if the Optional is un-assigned.
ReturnsThe Optional value, or the other value.

  then

global OptionalV1 then(final OptionalV1.Function function)
Description

Uses the given Function to process an Optional.

The method may be used to build a chain of functions to process an Optional.

Parameterfunction: The transformer to create the value.
ReturnsThe value returned by the transformer.

OptionalV1.Function

global interface Function
Description

Interface to allow Optional value to be processed.

Methods

  apply

OptionalV1 apply(final OptionalV1 value)
Description

Method called to process the value of an Optional.

The implementor must return an Optional representing the result of the processing.

Parametervalue: The Optional to be processed.
ReturnsThe result of the processing.

OptionalV1.NoSuchElementException

global with sharing class NoSuchElementException extends Exception
Description

Exception thrown to indicate an attempt was made to use an un-assigned value.