Commons library containing reusable functions, extensions, and objects for Kotlin
- Java 21
- Spring Boot 3
implementation("io.opengood.commons:kotlin-commons:VERSION")
<dependency>
<groupId>io.opengood.commons</groupId>
<artifactId>kotlin-commons</artifactId>
<version>VERSION</version>
</dependency>
Note: See Release version badge above for latest version.
-
Automatically generate no argument constructor on data class:
Example:
import io.opengood.commons.kotlin.annotation.NoArg @NoArg data class Greeting( val name: String )
Add configuration to
build.gradle.kts
:noArg { annotation("io.opengood.commons.kotlin.annotation.NoArg") invokeInitializers = true }
-
Print all arguments in an array:
Example:
import io.opengood.commons.kotlin.extension.method.printAll arrayOf("foo", "bar").printAll()
-
Rounds decimal value half up to specified scale:
Example:
import io.opengood.commons.kotlin.extension.method.roundUp BigDecimal(12.346).roundUp(2)
-
Case-insensitive string contained in list:
Example:
import io.opengood.commons.kotlin.extension.method.containsIgnoreCase listOf("foo", "bar").containsIgnoreCase("foo")
-
Sorts list of map keys in ascending order:
Example:
import io.opengood.commons.kotlin.extension.method.sortAscending val list = listOf( mapOf( "foo" to "bar", "baz" to "pas" ), mapOf( "foo" to "par", "baz" to "taz" ) ) list.sortAscending("baz")
-
Sorts list of map keys in descending order:
Example:
import io.opengood.commons.kotlin.extension.method.sortDescending val list = listOf( mapOf( "baz" to "pas", "foo" to "bar" ), mapOf( "baz" to "taz", "foo" to "par" ) ) list.sortDescending("baz")
-
Map entry contains more than one value in list:
Example:
import io.opengood.commons.kotlin.extension.method.containsMultipleListItems mapOf("foo" to listOf("bar", "baz")).containsMultipleListItems("foo")
-
Gets map key by index
Example:
import io.opengood.commons.kotlin.extension.method.keyByIndex val map = mapOf("foo" to "bar") map.keyByIndex(0)
-
Key not contained in map:
Example:
import io.opengood.commons.kotlin.extension.method.notContainsKey emptyMap<String, String>().notContainsKey("foo")
-
Value not contained in map:
Example:
import io.opengood.commons.kotlin.extension.method.notContainsValue emptyMap<String, String>().notContainsValue("foo")
-
Puts an entry into a map if it does not exist:
Example:
import io.opengood.commons.kotlin.extension.method.putIfNotAbsent import io.opengood.commons.kotlin.function.makeEntry val map = mutableMapOf<String, String>() val entry = makeEntry("foo", "bar") map.putIfNotAbsent(entry)
-
Gets map value by index
Example:
import io.opengood.commons.kotlin.extension.method.valueByIndex val map = mapOf("foo" to "bar") map.valueByIndex(0)
-
Convert
Pair
to map entry:Example:
import io.opengood.commons.kotlin.extension.method.toEntry Pair("foo", "bar").toEntry()
-
Capitalize word in a string:
Example:
import io.opengood.commons.kotlin.extension.method.capitalizeWord "hello".capitalizeWord()
-
Capitalize all words in a string:
Example:
import io.opengood.commons.kotlin.extension.method.capitalizeWords "hello world!".capitalizeWords()
-
Case-insensitive substring contained in string:
Example:
import io.opengood.commons.kotlin.extension.method.containsIgnoreCase "Hello World!".containsIgnoreCase("HELLO")
-
Case-insensitive string equals substring:
Example:
import io.opengood.commons.kotlin.extension.method.equalsIgnoreCase "Hello World!".equalsIgnoreCase("HELLO WORLD!")
-
Value indicating if string is boolean:
Example:
import io.opengood.commons.kotlin.extension.method.isBoolean "true".isBoolean()
-
Value indicating if string is integer:
Example:
import io.opengood.commons.kotlin.extension.method.isInt "12345".isInt()
-
Value indicating if string is UUIS:
Example:
import io.opengood.commons.kotlin.extension.method.isUuid "12102e90-2918-4578-a9a1-bb16ac81d8f8".isUuid()
-
Value indicating if string is integer, if so then value as integer, and if not, then value is zero:
Example:
import io.opengood.commons.kotlin.extension.method.toIntOrZero "12345".toIntOrZero()
Property | Description | Value |
---|---|---|
zero |
BigDecimal zero value | BigDecimal(0) |
Example:
import io.opengood.commons.kotlin.extension.property.*
Decimal.zero
Property | Description | Value |
---|---|---|
carriageReturn |
Carrier return | \r |
comma |
Comma | , |
crlf |
Carriage return/line feed | \n\r |
empty |
Empty string | |
equalsSign |
Equals sign | = |
greaterThanSign |
Greater than sign | > |
lessThanSign |
Less than sign | < |
newline |
Newline | \n |
pipe |
Pipe | `\ |
tab |
Tab | \t |
whitespace |
Whitespace |
Example:
import io.opengood.commons.kotlin.extension.property.*
String.carriageReturn
String.comma
String.crlf
String.empty
String.equalsSign
String.greaterThanSign
String.lessThanSign
String.newline
String.pipe
String.tab
String.whitespace
Property | Description | Value |
---|---|---|
empty |
Empty UUID | UUID(0L, 0L) |
Example:
import io.opengood.commons.kotlin.extension.property.*
Uuid.empty
-
Captures standard output of specified function:
Example:
import io.opengood.commons.kotlin.function.captureStdOut val output = captureStdOut { print("foo") }
-
Creates a map entry using specified key and value:
Example:
import io.opengood.commons.kotlin.function.makeEntry makeEntry("foo", "bar")
-
Shorthand ternary function for evaluating simple conditions then returning simple results:
Example:
import io.opengood.commons.kotlin.infix.then val condition = 1 val result = (condition == 1) then { true } ?: false
-
Gets value of specified object, if not null, otherwise, gets specified default value:
Example:
import io.opengood.commons.kotlin.infix.firstOrDefault "foo" firstOrDefault "bar"
-
Gets value indicating if specified integer is divisible by a factor of specified value:
Example:
import io.opengood.commons.kotlin.infix.isDivFactorOf 10 isDivFactorOf 5