Skip to content

Commit

Permalink
Create Traversable::getAny()
Browse files Browse the repository at this point in the history
  • Loading branch information
Glavo committed Nov 16, 2024
1 parent f9eb0d1 commit fce64ec
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ public interface OrderedTraversable<T> extends Traversable<T> {
return Iterators.firstOption(reverseIterator(), predicate);
}

@Override
default T getAny() {
return getFirst();
}

default T getFirst() {
return this.iterator().next();
}
Expand Down
12 changes: 12 additions & 0 deletions kala-base/src/main/java/kala/collection/base/Traversable.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,18 @@ default T elementAt(int index) {
}
}

default T getAny() {
return iterator().next();
}

default @Nullable T getAnyOrNull() {
return isNotEmpty() ? getAny() : null;
}

default @NotNull Option<T> getAnyOption() {
return isNotEmpty() ? Option.some(getAny()) : Option.none();
}

//region Element Retrieval Operations

default @NotNull Option<T> find(@NotNull Predicate<? super T> predicate) {
Expand Down

0 comments on commit fce64ec

Please sign in to comment.