Skip to content

Commit eccad2d

Browse files
More functions
1 parent 8265fcb commit eccad2d

3 files changed

Lines changed: 115 additions & 1 deletion

File tree

src/main/kotlin/cc/modlabs/klassicx/extensions/Exceptions.kt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,49 @@ inline fun <T> tryOrNull(
9898
process: () -> T,
9999
): T? = tryOrNull(silent = silent(), catch = catch, process = process)
100100

101+
inline fun <T> tryCatch(
102+
catch: (Exception) -> T,
103+
process: () -> T,
104+
): T = try {
105+
process()
106+
} catch (exception: Exception) {
107+
catch(exception)
108+
}
109+
110+
inline fun <T> tryCatch(
111+
silent: Boolean = true,
112+
exceptionCatch: ExceptionCatch<Exception> = ExceptionCatch.ignore(),
113+
catch: (Exception) -> T,
114+
process: () -> T,
115+
): T = try {
116+
process()
117+
} catch (exception: Exception) {
118+
if (silent) {
119+
exceptionCatch(exception, "")
120+
} else {
121+
catchException(exception, exceptionCatch)
122+
}
123+
catch(exception)
124+
}
125+
126+
inline fun tryCatch(
127+
silent: Boolean = true,
128+
exceptionCatch: ExceptionCatch<Exception> = ExceptionCatch.ignore(),
129+
catch: (Exception) -> Unit,
130+
process: () -> Unit,
131+
) {
132+
try {
133+
process()
134+
} catch (exception: Exception) {
135+
if (silent) {
136+
exceptionCatch(exception, "")
137+
} else {
138+
catchException(exception, exceptionCatch)
139+
}
140+
catch(exception)
141+
}
142+
}
143+
101144
inline fun tryOrIgnore(
102145
silent: Boolean = true,
103146
catch: ExceptionCatch<Exception> = ExceptionCatch.ignore(),

src/main/kotlin/cc/modlabs/klassicx/extensions/GeneralExtensions.kt

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,55 @@ inline fun <T> T?.ifNotNull(process: () -> Unit) {
5656
fun <T, D> Pair<T?, D>.asDefaultNullDodge(): Any? {
5757
return if (first != null) first else second
5858
}
59+
60+
fun <T> Iterable<T>.getOrNull(index: Int): T? {
61+
if (index < 0) {
62+
return null
63+
}
64+
65+
return when (this) {
66+
is List<T> -> this.getOrNull(index)
67+
else -> this.drop(index).firstOrNull()
68+
}
69+
}
70+
71+
inline fun <T> Iterable<T>.getOrElse(index: Int, defaultValue: (Int) -> T): T {
72+
return getOrNull(index) ?: defaultValue(index)
73+
}
74+
75+
operator fun <T> Iterable<T>.get(index: Int): T {
76+
return getOrNull(index) ?: throw IndexOutOfBoundsException("Index $index out of bounds for iterable")
77+
}
78+
79+
fun <T> MutableList<T>.setOrNull(index: Int, value: T): T? {
80+
if (index !in indices) {
81+
return null
82+
}
83+
84+
return set(index, value)
85+
}
86+
87+
fun <T> MutableList<T>.setIfPresent(index: Int, value: T): Boolean {
88+
return if (index in indices) {
89+
this[index] = value
90+
true
91+
} else {
92+
false
93+
}
94+
}
95+
96+
fun <T> MutableList<T>.setOrAdd(index: Int, value: T): MutableList<T> {
97+
require(index >= 0) { "Index must be non-negative" }
98+
99+
when {
100+
index < size -> this[index] = value
101+
index == size -> add(value)
102+
else -> throw IndexOutOfBoundsException("Index $index out of bounds for mutable list of size $size")
103+
}
104+
105+
return this
106+
}
107+
108+
fun <K, V> MutableMap<K, V>.getOrSet(key: K, defaultValue: () -> V): V {
109+
return getOrPut(key, defaultValue)
110+
}

src/main/kotlin/cc/modlabs/klassicx/extensions/TimeExtensions.kt

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package cc.modlabs.klassicx.extensions
22

33
import java.text.SimpleDateFormat
4+
import java.time.Clock
45
import java.time.DayOfWeek
6+
import java.time.Instant
57
import java.time.LocalDateTime
68
import java.util.*
79
import kotlin.time.Duration
10+
import kotlin.time.toKotlinDuration
811

912
fun <T> Iterable<T>.sumOf(selector: (T) -> Duration): Duration {
1013
var sum = Duration.ZERO
@@ -34,6 +37,22 @@ fun isWeekend(): Boolean {
3437
}
3538
}
3639

40+
fun Instant.durationToNow(clock: Clock = Clock.systemUTC()): Duration {
41+
return java.time.Duration.between(this, clock.instant()).toKotlinDuration()
42+
}
43+
44+
fun Instant.durationFromNow(clock: Clock = Clock.systemUTC()): Duration {
45+
return java.time.Duration.between(clock.instant(), this).toKotlinDuration()
46+
}
47+
48+
fun Duration.fromNow(clock: Clock = Clock.systemUTC()): Instant {
49+
return clock.instant().plus(this.inWholeNanoseconds, java.time.temporal.ChronoUnit.NANOS)
50+
}
51+
52+
fun Duration.ago(clock: Clock = Clock.systemUTC()): Instant {
53+
return clock.instant().minus(this.inWholeNanoseconds, java.time.temporal.ChronoUnit.NANOS)
54+
}
55+
3756

3857
fun parseDurationStringToEpoch(durationString: String): Long? {
3958
if (durationString.isEmpty()) return null
@@ -60,4 +79,4 @@ fun parseDurationStringToEpoch(durationString: String): Long? {
6079
}
6180

6281
return null
63-
}
82+
}

0 commit comments

Comments
 (0)