net.twisterrob.gradle.common.Utils.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of twister-quality-common Show documentation
Show all versions of twister-quality-common Show documentation
Shared classes between checkers (not to be consumed directly).
@file:JvmName("Utils")
package net.twisterrob.gradle.common
import java.io.File
import java.util.function.BinaryOperator
import java.util.function.Function
import java.util.stream.Collector
import java.util.stream.Collectors
fun safeAdd(a: Int?, b: Int?): Int? =
@Suppress("KotlinConstantConditions") // Make it clearly explicit, sadly this is an inspection not exhaustiveness.
when {
a != null && b != null -> a + b
a != null && b == null -> a
a == null && b != null -> b
a == null && b == null -> null
else -> throw InternalError("No other possibility")
}
fun nullSafeSum(): Collector =
nullSafeSum(Function.identity())
fun nullSafeSum(mapper: Function): Collector {
return Collectors.reducing(null, mapper, BinaryOperator(::safeAdd))
}
fun File.listFilesInDirectory(filter: ((File) -> Boolean)? = null): Array {
val listFiles: Array? =
if (filter != null)
this.listFiles(filter)
else
this.listFiles()
return listFiles ?: error(
"$this does not denote a directory or an error occurred" +
"\nisDirectory=${this.isDirectory}, exists=${this.exists()}, canRead=${this.canRead()}"
)
}