Record Class Option.Some<T>

java.lang.Object
java.lang.Record
org.jhotdraw8.icollection.impl.redblack.Option.Some<T>
All Implemented Interfaces:
Iterable<T>, Option<T>, ReadOnlyCollection<T>
Enclosing interface:
Option<T>

public static record Option.Some<T>(T value) extends Record implements Option<T>
  • Nested Class Summary

    Nested classes/interfaces inherited from interface org.jhotdraw8.icollection.impl.redblack.Option

    Option.None<T>, Option.Some<T>
  • Constructor Summary

    Constructors
    Constructor
    Description
    Some(T value)
    Creates an instance of a Some record class.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Returns true if this collection contains the specified object.
    final boolean
    Indicates whether some other object is "equal to" this one.
    get()
    Gets the value if this is a Some or throws if this is a None.
    final int
    Returns a hash code value for this object.
    boolean
    Returns true, if this is None, otherwise false, if this is Some.
    Returns an iterator over the elements in this collection.
    orElse(@NonNull Option<? extends T> other)
    Returns this Option if it is nonempty, otherwise return the alternative.
    Returns this Option if this is defined, or null if it is empty.
    Returns this Option if this is defined, or throws a NoSuchElementException if it is empty.
    int
    Returns the size of the collection.
    final String
    Returns a string representation of this record class.
    Returns the value of the value record component.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods inherited from interface java.lang.Iterable

    forEach, spliterator

    Methods inherited from interface org.jhotdraw8.icollection.impl.redblack.Option

    getOrElse

    Methods inherited from interface org.jhotdraw8.icollection.readonly.ReadOnlyCollection

    asCollection, characteristics, containsAll, stream, toArray, toArray
  • Constructor Details

    • Some

      public Some(T value)
      Creates an instance of a Some record class.
      Parameters:
      value - the value for the value record component
  • Method Details

    • size

      public int size()
      Description copied from interface: ReadOnlyCollection
      Returns the size of the collection.
      Specified by:
      size in interface ReadOnlyCollection<T>
      Returns:
      the size
    • isEmpty

      public boolean isEmpty()
      Description copied from interface: Option
      Returns true, if this is None, otherwise false, if this is Some.
      
       // Prints "false"
       System.out.println(Option.of(10).isEmpty());
      
       // Prints "true"
       System.out.println(Option.none().isEmpty());
       
      Specified by:
      isEmpty in interface Option<T>
      Specified by:
      isEmpty in interface ReadOnlyCollection<T>
      Returns:
      true, if this Option is empty, false otherwise
    • contains

      public boolean contains(Object o)
      Description copied from interface: ReadOnlyCollection
      Returns true if this collection contains the specified object.
      Specified by:
      contains in interface ReadOnlyCollection<T>
      Parameters:
      o - an object
      Returns:
      true if this collection contains the specified object
    • iterator

      public @NonNull Iterator<T> iterator()
      Description copied from interface: ReadOnlyCollection
      Returns an iterator over the elements in this collection.
      Specified by:
      iterator in interface Iterable<T>
      Specified by:
      iterator in interface ReadOnlyCollection<T>
      Returns:
      an iterator
    • orElse

      public @NonNull Option<T> orElse(@NonNull Option<? extends T> other)
      Description copied from interface: Option
      Returns this Option if it is nonempty, otherwise return the alternative.
      
       Option<String> other = Option.of("Other");
      
       // = Some("Hello World")
       Option.of("Hello World").orElse(other);
      
       // = Some("Other")
       Option.none().orElse(other);
       
      Specified by:
      orElse in interface Option<T>
      Parameters:
      other - An alternative Option
      Returns:
      this Option if it is nonempty, otherwise return the alternative.
    • get

      public T get()
      Description copied from interface: Option
      Gets the value if this is a Some or throws if this is a None.
      
       // Prints "57"
       System.out.println(Option.of(57).get());
      
       // Throws a NoSuchElementException
       Option.none().get();
       
      Specified by:
      get in interface Option<T>
      Returns:
      the value
    • orNull

      public T orNull()
      Description copied from interface: Option
      Returns this Option if this is defined, or null if it is empty.
      
       // = Some("Hello World")
       Option.of("Hello World").orNull();
      
       // = null
       Option.none().orNull();
       
      Specified by:
      orNull in interface Option<T>
      Returns:
      this value if it is defined, or null if it is empty.
    • orThrow

      public T orThrow()
      Description copied from interface: Option
      Returns this Option if this is defined, or throws a NoSuchElementException if it is empty.
      
       // = Some("Hello World")
       Option.of("Hello World").orThrow();
      
       // = null
       Option.none().orThrow();
       
      Specified by:
      orThrow in interface Option<T>
      Returns:
      this value if it is defined, or null if it is empty.
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • value

      public T value()
      Returns the value of the value record component.
      Returns:
      the value of the value record component