All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.twisterrob.gradle.common.Utils.kt Maven / Gradle / Ivy

There is a newer version: 0.17
Show newest version
@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()}"
	)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy