Package com.hexagonkt.core

JVM information and other useful utilities.

Classpath URLs in native images

To use the 'classpath' URL scheme on GraalVM native images, the native-image command requires to add the Classpath handler manually with the --enable-url-protocols=classpath parameter.

Flags (System Properties)

  • DISABLE_CHECKS: set to true to disable some checks in order to shave a few ms. in production. Do not enable it in application development and turn it on only when the application is extensively tested.

Types

Link copied to clipboard
object Ansi

Constants for console formatting with ANSI codes. They can be used in strings to enable or disable a display option.

Link copied to clipboard
object ClasspathHandler : URLStreamHandler
Link copied to clipboard
class CodedException(    val code: Int,     message: String = "",     cause: Throwable? = null) : RuntimeException

Exception with a numeric code.

Link copied to clipboard
data class Glob(val pattern: String)
Link copied to clipboard
object Jvm

Object with utilities to gather information about the running JVM.

Link copied to clipboard
abstract class MapResourceBundle(val map: Map<*, *>) : ListResourceBundle
Link copied to clipboard
class MultiMap<K, V>(mapData: Map<K, List<V>>) : Map<K, V>

Simplify access to multivalued maps. It looks for first elements, but all values can be accessed through the allValues field.

Link copied to clipboard
class MultipleException(val causes: List<Throwable>, message: String = "") : RuntimeException

Exception with a list of causes. Cause is null as it can't be tell which one of the list is the cause.

Link copied to clipboard
class ResourceNotFoundException(message: String) : IOException

Functions

Link copied to clipboard
fun String.banner(bannerDelimiter: String = "*"): String

Format the string as a banner with a delimiter above and below text. The character used to render the delimiter is defined.

Link copied to clipboard
fun UUID.bytes(): ByteArray
Link copied to clipboard
fun String.camelToSnake(): String

Transform the target string from camel case to snake case.

Link copied to clipboard
fun check(message: String = "Multiple exceptions", vararg blocks: () -> Unit)
Link copied to clipboard
fun String.decodeBase64(): ByteArray

Decode this base64 encoded string.

Link copied to clipboard
fun ByteArray.encodeToBase64(): String

Encode the content of this byteArray to base64.

fun String.encodeToBase64(): String

Encode this string to base64.

Link copied to clipboard
fun <Z> Collection<Z>.ensureSize(count: IntRange): Collection<Z>
Link copied to clipboard
fun String.exec(    workingDirectory: File = File(System.getProperty("user.dir")),     timeout: Long = Long.MAX_VALUE,     fail: Boolean = false): String

TODO Add use case and example in documentation. TODO Support multiple words parameters by processing " and '

fun List<String>.exec(    workingDirectory: File = File(System.getProperty("user.dir")),     timeout: Long = Long.MAX_VALUE,     fail: Boolean = false): String
Link copied to clipboard
fun <T : Any> fieldsMapOf(vararg fields: Pair<KProperty1<T, *>, *>): Map<String, *>
Link copied to clipboard
fun String.filter(    prefix: String,     suffix: String,     vararg parameters: Pair<String, *>): String
Link copied to clipboard
fun <V> List<V?>.filterEmpty(): List<V>
fun <K, V> Map<K, V?>.filterEmpty(): Map<K, V>
Link copied to clipboard
fun List<*>.filterEmptyRecursive(): List<*>
fun Map<*, *>.filterEmptyRecursive(): Map<*, *>
Link copied to clipboard
fun Throwable.filterStackTrace(prefix: String): Array<out StackTraceElement>

Return the stack trace array of the frames that starts with the given prefix.

Link copied to clipboard
fun String.filterVars(vararg parameters: Pair<*, *>): String
fun String.filterVars(parameters: Map<*, *>): String

Filter the target string substituting each key by its value. The keys format resembles Mustache's one: {{key}} and all occurrences are replaced by the supplied value.

Link copied to clipboard
fun Regex.findGroups(text: String): List<MatchGroup>
Link copied to clipboard
fun freePort(): Int

Return a random free port (not used by any other local process).

Link copied to clipboard
inline operator fun <T : Any> Map<*, *>.invoke(vararg keys: Any): T?
Link copied to clipboard
fun isPortOpened(port: Int): Boolean

Check if a port is already opened.

Link copied to clipboard
inline fun <T : Any> Map<*, *>.keys(vararg keys: Any): T?
Link copied to clipboard
fun <K, V> multiMapOf(vararg pairs: Pair<K, V>): MultiMap<K, V>
Link copied to clipboard
fun <K, V> multiMapOfLists(vararg pairs: Pair<K, List<V>>): MultiMap<K, V>
Link copied to clipboard
fun <V> notEmpty(value: V?): Boolean
Link copied to clipboard
fun String.prependIndent(count: Int = 4, pad: String = " "): String
Link copied to clipboard
fun <T> T.println(prefix: String = ""): T

Print receiver to stdout. Convenient utility to debug variables quickly.

Link copied to clipboard
fun <K, V> Map<K, V>.require(name: K): V
Link copied to clipboard
inline fun <T : Any> Map<*, *>.requireKeys(vararg name: Any): T
Link copied to clipboard
inline fun <T : ResourceBundle> resourceBundle(locale: Locale = Locale.getDefault()): ResourceBundle
fun <T : ResourceBundle> resourceBundle(type: KClass<T>, locale: Locale = Locale.getDefault()): ResourceBundle
Link copied to clipboard
fun <T> retry(    times: Int,     delay: Long,     block: () -> T): T

Execute a lambda until no exception is thrown or a number of times is reached.

Link copied to clipboard
fun String.snakeToCamel(): String

Transform the target string from snake case to camel case.

Link copied to clipboard
fun String.stripAccents(): String
Link copied to clipboard
fun String.stripAnsi(): String
Link copied to clipboard
fun UUID.toBase64(): String
Link copied to clipboard
fun LocalDate.toDate(): Date

Convert a local date to a date.

fun LocalDateTime.toDate(): Date

Convert a local date time to a date.

fun ZonedDateTime.toDate(): Date

Convert a zoned date time to a date.

Link copied to clipboard
fun Date.toLocalDate(): LocalDate

Convert a date to a local date.

fun Int.toLocalDate(): LocalDate

Parse a date from a formatted integer with this format: YYYYMMDD.

Link copied to clipboard
fun Date.toLocalDateTime(): LocalDateTime

Convert a date to a local date time.

fun Long.toLocalDateTime(): LocalDateTime

Parse a date time from a formatted number with this format: YYYYMMDDHHmmss.

Link copied to clipboard
fun Int.toLocalTime(): LocalTime

Parse a time from a formatted integer with this format: HHmmssSSS.

Link copied to clipboard
fun <K, V> Map<K, List<V>>.toMultiMap(): MultiMap<K, V>
Link copied to clipboard
fun LocalDate.toNumber(): Int

Convert a date to an integer with the following format: YYYYMMDD.

fun LocalDateTime.toNumber(): Long

Convert a date time to a number with the following format: YYYYMMDDHHmmss.

fun LocalTime.toNumber(): Int

Convert a time to an integer with the following format: HHmmssSSS.

Link copied to clipboard
fun String.toStream(): InputStream
Link copied to clipboard
fun Throwable.toText(prefix: String = ""): String

Return this throwable as a text.

Link copied to clipboard
fun utf8(vararg bytes: Int): String
Link copied to clipboard
fun uuid(bytes: ByteArray): UUID
fun uuid(text: String): UUID
Link copied to clipboard
fun LocalDateTime.withZone(zoneId: ZoneId = Jvm.timeZone.toZoneId()): ZonedDateTime

Return the date time in a given time zone for a local date time.

Properties

Link copied to clipboard
var disableChecks: Boolean

Disable heavy and optional checks in runtime. This flag can be enabled to get a small performance boost. Do NOT do this on development (it could mask problems) and enable it on production only if you have tested your application extensively.

Link copied to clipboard
val eol: String

Runtime specific end of line.

Link copied to clipboard
val fail: Nothing

Syntax sugar to throw errors.