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 17, 2024
1 parent b159dcf commit 134bc67
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 17 deletions.
4 changes: 4 additions & 0 deletions EzXHelper/api/EzXHelper.api
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,11 @@ public abstract class com/github/kyuubiran/ezxhelper/finders/base/BaseFinder : c
public final fun plusAssign (Ljava/lang/Iterable;)V
public final fun plusAssign (Lkotlin/sequences/Sequence;)V
public final fun plusAssign ([Ljava/lang/Object;)V
public final fun requireSingle ()Ljava/lang/Object;
public final fun requireSingleOrNull ()Ljava/lang/Object;
protected final fun setSequence (Lkotlin/sequences/Sequence;)V
public final fun single ()Ljava/lang/Object;
public final fun singleOrNull ()Ljava/lang/Object;
public final fun toCollection (Ljava/util/Collection;)Ljava/util/Collection;
public final fun toHashSet ()Ljava/util/HashSet;
public final fun toList ()Ljava/util/List;
Expand Down
8 changes: 4 additions & 4 deletions EzXHelper/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ plugins {
id 'signing'
}

def versionName = "2.0.8"
def versionName = "2.0.9"

android {
compileSdkVersion = 33
buildToolsVersion = "33.0.1"
compileSdkVersion = 34
buildToolsVersion = "34.0.0"
namespace = "com.github.kyuubiran"

defaultConfig {
minSdkVersion 24
targetSdkVersion 33
targetSdkVersion 34

consumerProguardFiles "consumer-rules.pro"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@ import com.github.kyuubiran.ezxhelper.Config
import com.github.kyuubiran.ezxhelper.interfaces.INamed
import com.github.kyuubiran.ezxhelper.misc.FinderExceptionMessage

abstract class BaseFinder<T, Self> constructor(protected var sequence: Sequence<T>) : INamed {
abstract class BaseFinder<T, Self>(protected var sequence: Sequence<T>) : INamed {
// region exception message

protected val exceptMsg: FinderExceptionMessage? =
if (Config.enableFinderExceptionMessage) FinderExceptionMessage() else null

protected val exceptionMessageEnabled = exceptMsg != null

protected inline fun exceptMessageScope(block: FinderExceptionMessage.() -> Unit) = exceptMsg?.block()
protected inline fun exceptMessageScope(block: FinderExceptionMessage.() -> Unit) =
exceptMsg?.block()

// endregion

@Suppress("UNCHECKED_CAST")
protected inline fun applyThis(block: BaseFinder<T, Self>.() -> Unit) = this.apply(block) as Self
protected inline fun applyThis(block: BaseFinder<T, Self>.() -> Unit) =
this.apply(block) as Self

// region get elem

Expand Down Expand Up @@ -103,6 +105,34 @@ abstract class BaseFinder<T, Self> constructor(protected var sequence: Sequence<
*/
open fun lastOrNull(condition: T.() -> Boolean) = sequence.lastOrNull(condition)

/**
* Check sequence only has one element.
*/
@Throws(IllegalArgumentException::class, NoSuchElementException::class)
fun requireSingle() = applyThis {
sequence.single()
}

/**
* Check sequence only has one element or null if not found.
*/
@Suppress("UNCHECKED_CAST")
fun requireSingleOrNull(): Self? {
val o = sequence.singleOrNull()
return if (o == null) null else this as Self
}

/**
* Get the single element or throw an exception if there is no such element.
*/
@Throws(IllegalArgumentException::class, NoSuchElementException::class)
fun single() = sequence.single()

/**
* Get the single element or null if not found.
*/
fun singleOrNull() = sequence.singleOrNull()

// endregion

/**
Expand All @@ -128,7 +158,8 @@ abstract class BaseFinder<T, Self> constructor(protected var sequence: Sequence<
* @param action the action
* @return [Self] the finder
*/
fun onEachIndexed(action: (index: Int, T) -> Unit): Self = applyThis { sequence.forEachIndexed(action) }
fun onEachIndexed(action: (index: Int, T) -> Unit): Self =
applyThis { sequence.forEachIndexed(action) }

/**
* For-each loop for.
Expand Down Expand Up @@ -174,7 +205,8 @@ abstract class BaseFinder<T, Self> constructor(protected var sequence: Sequence<
* @param transform the transform action
* @return [MutableSet] the mutable set
*/
fun <R> mapToMutableSet(transform: (T) -> R): MutableSet<R> = sequence.map(transform).toMutableSet()
fun <R> mapToMutableSet(transform: (T) -> R): MutableSet<R> =
sequence.map(transform).toMutableSet()

/**
* Map to the hashset.
Expand All @@ -188,7 +220,10 @@ abstract class BaseFinder<T, Self> constructor(protected var sequence: Sequence<
* @param transform the transform action
* @return [MutableCollection] the collection
*/
fun <R, C> mapToCollection(destination: C, transform: (T) -> R): C where C : MutableCollection<in R> =
fun <R, C> mapToCollection(
destination: C,
transform: (T) -> R
): C where C : MutableCollection<in R> =
sequence.map(transform).toCollection(destination)
// endregion

Expand Down Expand Up @@ -227,7 +262,8 @@ abstract class BaseFinder<T, Self> constructor(protected var sequence: Sequence<
* Make sequence to the collection.
* @return [MutableCollection] the collection
*/
fun <C> toCollection(collection: C): C where C : MutableCollection<T> = sequence.toCollection(collection)
fun <C> toCollection(collection: C): C where C : MutableCollection<T> =
sequence.toCollection(collection)

// endregion

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
加入Telegram群组来获取帮助: [![Telegram](https://img.shields.io/badge/Join-Telegram-blue)](https://t.me/EzXHelper)

一个使Xposed模块开发变的更轻松的工具库,2.x版本已支持Java!
2.x版本已经发布!最新版为`2.0.8` 旧版本请看`1.x`分支
2.x版本已经发布!最新版为`2.0.9` 旧版本请看`1.x`分支

### 使用本库的项目

Expand Down
2 changes: 1 addition & 1 deletion README_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Join Telegram group to get
help: [![Telegram](https://img.shields.io/badge/Join-Telegram-blue)](https://t.me/EzXHelper)

A library to make Xposed modules development easier, 2.x version supports Java!
2.x is released now! The latest version is `2.0.8`. Old version please look the `1.x` branch.
2.x is released now! The latest version is `2.0.9`. Old version please look the `1.x` branch.

### Projects that use this library

Expand Down
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '8.1.2' apply false
id 'com.android.library' version '8.1.2' apply false
id 'org.jetbrains.kotlin.android' version '1.9.10' apply false
id 'com.android.application' version '8.2.2' apply false
id 'com.android.library' version '8.2.2' apply false
id 'org.jetbrains.kotlin.android' version '1.9.22' apply false
id 'org.jetbrains.dokka' version '1.9.10' apply false
id 'org.jetbrains.kotlinx.binary-compatibility-validator' version '0.13.2'
}
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.1.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
networkTimeout=10000
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

0 comments on commit 134bc67

Please sign in to comment.