kotlin-preconditions alternatives and similar libraries
Based on the "Misc" category.
Alternatively, view kotlin-preconditions alternatives based on common mentions on social networks and blogs.
-
jclasslib
jclasslib bytecode editor is a tool that visualizes all aspects of compiled Java class files and the contained bytecode. -
kotlin-logging
Lightweight Multiplatform logging framework for Kotlin. A convenient and performant logging facade. -
lingua
The most accurate natural language detection library for Java and the JVM, suitable for long and short text alike -
Kotlift
DISCONTINUED. Kotlift is the first source-to-source language transpiler from Kotlin to Swift -
Humanizer.jvm
Humanizer.jvm meets all your jvm needs for manipulating and displaying strings, enums, dates, times, timespans, numbers and quantities. -
klutter
A mix of random small libraries for Kotlin, the smallest reside here until big enough for their own repository. -
solr-undertow
Solr / SolrCloud running in high performance server - tiny, fast startup, simple to configure, easy deployment without an application server. -
kassava
This library provides some useful kotlin extension functions for implementing toString(), hashCode() and equals() without all of the boilerplate. -
SimpleDNN
SimpleDNN is a machine learning lightweight open-source library written in Kotlin designed to support relevant neural network architectures in natural language processing tasks -
kasechange
🐫🐍🍢🅿 Multiplatform Kotlin library to convert strings between various case formats including Camel Case, Snake Case, Pascal Case and Kebab Case -
kotlin-futures
A collections of extension functions to make the JVM Future, CompletableFuture, ListenableFuture API more functional and Kotlin like. -
PrimeCalendar
PrimeCalendar provides all of the java.util.Calendar functionalities for Persian, Hijri, and ... dates. It is also possible to convert dates to each other. -
log4k
Lightweight logging library for Kotlin/Multiplatform. Supports Android, iOS, JavaScript and plain JVM environments.
CodeRabbit: AI Code Reviews for Developers
* Code Quality Rankings and insights are calculated and provided by Lumnify.
They vary from L1 to L5 with "L5" being the highest.
Do you think we are missing an alternative of kotlin-preconditions or a related project?
README
Kotlin Preconditions
kotlin-preconditions will assist you in ensuring all of your invariants are met before an operation is executed.
Usage
kotlin-preconditions provides a powerful DSL for defining preconditions:
checkThat(1) { isLt(2) }
checkThat(listOf(1, 2)) { contains(1) }
requireThat(1) { isLt(2) }
requireThat(listOf(1, 2)) { contains(1) }
If any of the preconditions are not met then checkThat
will throw an
IllegalStateException
and requireThat
will throw an IllegalArgumentException
.
The exception message will be generated lazily, so your application does
not waste resources on evaluating objects that may not trigger an exception.
Compose your preconditions the way you like it:
val list = listOf(1, 2)
requireThat(list) { contains(3) or contains(1) and not(hasSize(3)) }
// nested preconditions
val text: String? = "hello"
requireThat(text) {
not(isNull()) and {
hasLength(6) or { startsWith("he") and endsWith("llo") }
}
}
API Overview
Aliases
Instead of checkThat
and requireThat
you can also use check
and require
.
val value = "hello"
check(value) { startsWith("he") and hasLength(5) and not(includes("io")) }
require(value) { startsWith("he") and hasLength(5) and not(includes("io")) }
String preconditions
val value = "hello"
requireThat(value) { startsWith("he") and hasLength(5) and not(includes("io")) }
requireThat(value) { includes("ll") }
requireThat(value) { matches("hello") }
requireThat(value) { endsWith("lo") }
requireThat(value) { hasLength(5) }
requireThat(value) { not(isBlank()) }
requireThat(value) { not(isEmpty()) }
requireThat(value) { hasLengthBetween(1, 5) }
Collection preconditions
val list = listOf(1, 2)
requireThat(list) { hasSize(2) }
requireThat(list) { contains(1) or contains(3) and not(hasSize(3)) }
requireThat(list) { containsAll(1, 2) }
requireThat(list) { containsAll(list) }
requireThat(list) { isSorted() }
Comparable preconditions
requireThat(1) { isLt(2) }
requireThat(1) { isLte(1) }
requireThat(1) { isGt(0) }
requireThat(1) { isGte(1) }
requireThat(1) { isBetween(0..2) }
Map preconditions
val map = mapOf(1 to "1")
requireThat(map) { hasKey(1) }
requireThat(map) { hasValue("1") }
requireThat(map) { contains(1, "1") }
Object preconditions
val result = Result(true)
requireThat(result) { not(isNull()) }
requireThat(result) { isEqualTo(result) }
requireThat(result) { isSameInstanceAs(result) }
Composed preconditions
val value = "hello"
requireThat(value) { startsWith("he") and hasLength(5) and not(includes("io")) }
Labels
val numbers = listOf(1, 2)
requireThat(numbers, "Numbers") { contains(3) or contains(1) and not(hasSize(3)) }
Custom matchers
Custom matchers can be added by using extension functions:
data class Car(val age: Int)
fun PreconditionBlock<Car>.hasAge(expected: Int) = object : Matcher<Car>() {
override fun test(condition: Condition<Car>) = condition.test {
withResult(value.age == expected) { "expected car to be $expected years old, but was ${value.age}" }
}
}
requireThat(Car(22)) { hasAge(22) }
Installation
Maven:
<dependency>
<groupId>com.github.spoptchev</groupId>
<artifactId>kotlin-preconditions</artifactId>
<version>6.1.0</version>
</dependency>
Gradle:
compile 'com.github.spoptchev:kotlin-preconditions:6.1.0'