diff --git a/EzXHelper/src/main/java/com/github/kyuubiran/ezxhelper/finders/base/BaseFinder.kt b/EzXHelper/src/main/java/com/github/kyuubiran/ezxhelper/finders/base/BaseFinder.kt index 509bcf6..719aea2 100644 --- a/EzXHelper/src/main/java/com/github/kyuubiran/ezxhelper/finders/base/BaseFinder.kt +++ b/EzXHelper/src/main/java/com/github/kyuubiran/ezxhelper/finders/base/BaseFinder.kt @@ -113,6 +113,11 @@ abstract class BaseFinder(protected var sequence: Sequence) : 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. */ @@ -122,6 +127,12 @@ abstract class BaseFinder(protected var sequence: Sequence) : 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. */