singleOptional property

Optional<T> get singleOptional

The single element of the iterable, or Optional.empty().

The value is Optional.empty() if the iterable is empty or it contains more than one element.

Implementation

Optional<T> get singleOptional {
  var iterator = this.iterator;
  if (iterator.moveNext()) {
    var result = iterator.current;
    if (!iterator.moveNext()) {
      return Optional.of(result);
    }
  }
  return Optional.empty();
}