Skip to content

Commit

Permalink
Add single() for Finders
Browse files Browse the repository at this point in the history
  • Loading branch information
KyuubiRan committed Feb 21, 2024
1 parent 5bae542 commit dcb2905
Showing 1 changed file with 11 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ abstract class BaseFinder<T, Self>(protected var sequence: Sequence<T>) : INamed
sequence.single()
}

@Throws(IllegalArgumentException::class, NoSuchElementException::class)
fun requireSingle(condition: T.() -> Boolean) = applyThis {
sequence.single(condition)
}

/**
* Check sequence only has one element or null if not found.
*/
Expand All @@ -122,6 +127,12 @@ abstract class BaseFinder<T, Self>(protected var sequence: Sequence<T>) : INamed
return if (o == null) null else this as Self
}

@Suppress("UNCHECKED_CAST")
fun requireSingleOrNull(condition: T.() -> Boolean): Self? {
val o = sequence.singleOrNull(condition)
return if (o == null) null else this as Self
}

/**
* Get the single element or throw an exception if there is no such element or more than one element.
*/
Expand Down

0 comments on commit dcb2905

Please sign in to comment.