diff --git a/gradle.properties b/gradle.properties index 8348b3f..5f16a11 100644 --- a/gradle.properties +++ b/gradle.properties @@ -16,10 +16,10 @@ # ProjectName mingli-utils # ModuleName mingli-utils # CurrentFile gradle.properties -# LastUpdate 2025-09-15 11:19:10 +# LastUpdate 2025-09-15 12:01:24 # UpdateUser MingLiPro # JDKVERSIONS=1.8 GROUPSID=com.mingliqiye.utils ARTIFACTID=mingli-utils -VERSIONS=4.0.1 +VERSIONS=4.0.2 diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar index 8bdaf60..1b33c55 100644 Binary files a/gradle/wrapper/gradle-wrapper.jar and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 11573d4..137a92b 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -16,12 +16,12 @@ # ProjectName mingli-utils # ModuleName mingli-utils # CurrentFile gradle-wrapper.properties -# LastUpdate 2025-09-09 08:37:34 +# LastUpdate 2025-09-15 12:01:36 # UpdateUser MingLiPro # distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://mirrors.aliyun.com/macports/distfiles/gradle/gradle-8.14-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.14.2-bin.zip networkTimeout=10000 validateDistributionUrl=true zipStoreBase=GRADLE_USER_HOME diff --git a/gradlew b/gradlew index d1b5330..c6d0206 100755 --- a/gradlew +++ b/gradlew @@ -18,7 +18,7 @@ # ProjectName mingli-utils # ModuleName mingli-utils # CurrentFile gradlew -# LastUpdate 2025-09-09 08:37:33 +# LastUpdate 2025-09-15 12:01:36 # UpdateUser MingLiPro # diff --git a/src/main/kotlin/com/mingliqiye/utils/foreach/ForEach.kt b/src/main/kotlin/com/mingliqiye/utils/foreach/ForEach.kt new file mode 100644 index 0000000..2ee89fd --- /dev/null +++ b/src/main/kotlin/com/mingliqiye/utils/foreach/ForEach.kt @@ -0,0 +1,355 @@ +/* + * Copyright 2025 mingliqiye + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ProjectName mingli-utils + * ModuleName mingli-utils.main + * CurrentFile ForEach.kt + * LastUpdate 2025-09-15 12:01:36 + * UpdateUser MingLiPro + */ +@file:JvmName("ForEach") + +package com.mingliqiye.utils.foreach + +import com.mingliqiye.utils.functions.P1Function +import com.mingliqiye.utils.functions.P1RFunction +import com.mingliqiye.utils.functions.P2Function +import com.mingliqiye.utils.functions.P2RFunction + + +fun forEach( + iterable: Iterable, action: P2Function +) { + when (iterable) { + is RandomAccess if iterable is MutableList<*> -> { + val list = iterable as MutableList + for (i in list.indices) { + action.call(list[i], i) + } + } + + is MutableList<*> -> { + var index = 0 + val it = iterable.iterator() + while (it.hasNext()) { + action.call(it.next(), index) + index++ + } + } + + else -> { + var index = 0 + for (element in iterable) { + action.call(element, index) + index++ + } + } + } +} + +/** + * 对给定的可迭代对象执行指定的操作,仅处理元素值。 + * 根据可迭代对象是否实现 RandomAccess 接口选择最优的遍历方式。 + * + * @param iterable 要遍历的可迭代对象 + * @param action 要对每个元素执行的操作,只接收元素值作为参数 + * @param 可迭代对象中元素的类型 + **/ +fun forEach( + iterable: Iterable, action: P1Function +) { + if (iterable is RandomAccess) { + val list = iterable as MutableList + for (i in list.indices) { + action.call(list[i]) + } + } else { + for (element in iterable) { + action.call(element) + } + } +} + +fun forEach( + iterable: Iterable, action: P2RFunction +) { + when (iterable) { + is RandomAccess if iterable is MutableList<*> -> { + val list = iterable as MutableList + for (i in list.indices) { + if (action.call(list[i], i)) return + } + } + + is MutableList<*> -> { + var index = 0 + val it = iterable.iterator() + while (it.hasNext()) { + if (action.call(it.next(), index)) return + index++ + } + } + + else -> { + var index = 0 + for (element in iterable) { + if (action.call(element, index)) return + index++ + } + } + } +} + +/** + * 对给定的可迭代对象执行指定的操作,仅处理元素值。 + * 根据可迭代对象是否实现 RandomAccess 接口选择最优的遍历方式。 + * + * @param iterable 要遍历的可迭代对象 + * @param action 要对每个元素执行的操作,只接收元素值作为参数 + * @param 可迭代对象中元素的类型 + **/ +fun forEach( + iterable: Iterable, action: P1RFunction +) { + if (iterable is RandomAccess) { + val list = iterable as MutableList + for (i in list.indices) { + if (action.call(list[i])) return + } + } else { + for (element in iterable) { + if (action.call(element)) return + } + } +} + +fun forEach( + array: Array, action: P2Function +) { + forEach(array.toList(), action) +} + +fun forEach( + array: Array, action: P1Function +) { + forEach(array.toList(), action) +} + + +fun forEach( + array: Array, action: P2RFunction +) { + forEach(array.toList(), action) +} + +fun forEach( + array: Array, action: P1RFunction +) { + forEach(array.toList(), action) +} + + +fun forEach( + array: ByteArray, action: P2Function +) { + forEach(array.toList(), action) +} + +fun forEach( + array: ByteArray, action: P1Function +) { + forEach(array.toList(), action) +} + + +fun forEach( + array: ByteArray, action: P2RFunction +) { + forEach(array.toList(), action) +} + +fun forEach( + array: ByteArray, action: P1RFunction +) { + forEach(array.toList(), action) +} + +fun forEach( + array: CharArray, action: P2Function +) { + forEach(array.toList(), action) +} + +fun forEach( + array: CharArray, action: P1Function +) { + forEach(array.toList(), action) +} + +fun forEach( + array: CharArray, action: P2RFunction +) { + forEach(array.toList(), action) +} + +fun forEach( + array: CharArray, action: P1RFunction +) { + forEach(array.toList(), action) +} + +fun forEach( + array: IntArray, action: P2Function +) { + forEach(array.toList(), action) +} + +fun forEach( + array: IntArray, action: P1Function +) { + forEach(array.toList(), action) +} + +fun forEach( + array: IntArray, action: P2RFunction +) { + forEach(array.toList(), action) +} + +fun forEach( + array: IntArray, action: P1RFunction +) { + forEach(array.toList(), action) +} + +fun forEach( + array: LongArray, action: P2Function +) { + forEach(array.toList(), action) +} + +fun forEach( + array: LongArray, action: P1Function +) { + forEach(array.toList(), action) +} + +fun forEach( + array: LongArray, action: P2RFunction +) { + forEach(array.toList(), action) +} + +fun forEach( + array: LongArray, action: P1RFunction +) { + forEach(array.toList(), action) +} + +fun forEach( + array: ShortArray, action: P2Function +) { + forEach(array.toList(), action) +} + +fun forEach( + array: ShortArray, action: P1Function +) { + forEach(array.toList(), action) +} + +fun forEach( + array: ShortArray, action: P2RFunction +) { + forEach(array.toList(), action) +} + +fun forEach( + array: ShortArray, action: P1RFunction +) { + forEach(array.toList(), action) +} + +fun forEach( + array: FloatArray, action: P2Function +) { + forEach(array.toList(), action) +} + +fun forEach( + array: FloatArray, action: P1Function +) { + forEach(array.toList(), action) +} + +fun forEach( + array: FloatArray, action: P2RFunction +) { + forEach(array.toList(), action) +} + +fun forEach( + array: FloatArray, action: P1RFunction +) { + forEach(array.toList(), action) +} + +fun forEach( + array: DoubleArray, action: P2Function +) { + forEach(array.toList(), action) +} + +fun forEach( + array: DoubleArray, action: P1Function +) { + forEach(array.toList(), action) +} + +fun forEach( + array: DoubleArray, action: P2RFunction +) { + forEach(array.toList(), action) +} + +fun forEach( + array: DoubleArray, action: P1RFunction +) { + forEach(array.toList(), action) +} + +fun forEach( + array: BooleanArray, action: P2Function +) { + forEach(array.toList(), action) +} + +fun forEach( + array: BooleanArray, action: P1Function +) { + forEach(array.toList(), action) +} + +fun forEach( + array: BooleanArray, action: P2RFunction +) { + forEach(array.toList(), action) +} + +fun forEach( + array: BooleanArray, action: P1RFunction +) { + forEach(array.toList(), action) +}