Class OptionalOrElseThrowExamples

java.lang.Object
net.jrodolfo.java_evolution.java10.OptionalOrElseThrowExamples

public class OptionalOrElseThrowExamples extends Object
Demonstrates the no-argument Optional.orElseThrow() method added in Java 10.

Before Java 10, callers commonly used get() or the Java 8 orElseThrow(Supplier) overload. get() was short but did not communicate intent well. The supplier overload was explicit but verbose when the default NoSuchElementException was acceptable.

Java 10 added a clearer no-argument option for that common case.

  • Constructor Details

    • OptionalOrElseThrowExamples

      public OptionalOrElseThrowExamples()
  • Method Details

    • requiredValue

      public String requiredValue(Optional<String> value)
      Returns the value when the Optional is present.
      Parameters:
      value - the Optional to unwrap
      Returns:
      the contained value
    • describeRequiredFeature

      public String describeRequiredFeature(String featureName)
      Looks up a feature by name and unwraps the result with no-arg Optional.orElseThrow().
      Parameters:
      featureName - the feature to find
      Returns:
      a message for the requested feature