Skip to content
This repository has been archived by the owner on Jul 27, 2023. It is now read-only.

Commit

Permalink
Rewrote extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
adriantodt committed Aug 25, 2019
1 parent ed2dfed commit cc15236
Show file tree
Hide file tree
Showing 19 changed files with 233 additions and 220 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ plugins {
}

group = "pw.aru.psi"
version = "1.5"
version = "1.6"

//Repositories and Dependencies
repositories {
Expand Down
9 changes: 4 additions & 5 deletions src/main/kotlin/pw/aru/psi/bootstrap/CommandBootstrap.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import pw.aru.psi.executor.service.TaskExecutorService
import pw.aru.psi.logging.DiscordLogger
import pw.aru.utils.Colors
import pw.aru.utils.extensions.lang.allOf
import pw.aru.utils.extensions.lang.classOf
import pw.aru.utils.extensions.lib.field
import java.time.OffsetDateTime

Expand Down Expand Up @@ -58,7 +57,7 @@ class CommandBootstrap(private val scanResult: ScanResult, private val kodein: K
.loadClasses(ICommand::class.java)
.forEach {
try {
val meta = it.getAnnotation(classOf<Command>())
val meta = it.getAnnotation(Command::class.java)
val command = kodein.jitInstance(it)
registry.register(meta.value.toList(), command)
processExecutable(command)
Expand Down Expand Up @@ -144,11 +143,11 @@ class CommandBootstrap(private val scanResult: ScanResult, private val kodein: K
private fun processExecutable(it: Any) {
if (it is Executable) {
when {
it.javaClass.isAnnotationPresent(classOf<RunEvery>()) -> {
val meta = it.javaClass.getAnnotation(classOf<RunEvery>())
it.javaClass.isAnnotationPresent(RunEvery::class.java) -> {
val meta = it.javaClass.getAnnotation(RunEvery::class.java)
tasks.task(meta.amount, meta.unit, meta.initialDelay, it.simpleName + meta, it::run)
}
it.javaClass.isAnnotationPresent(classOf<RunAtStartup>()) -> {
it.javaClass.isAnnotationPresent(RunAtStartup::class.java) -> {
tasks.queue("${it.simpleName}@RunAtStartup", it::run)
}
else -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package pw.aru.psi.commands.manager

import mu.KLogging
import pw.aru.psi.commands.ICommand
import pw.aru.utils.extensions.lang.classOf

class CommandRegistry {
interface Listener {
Expand All @@ -15,8 +14,8 @@ class CommandRegistry {

companion object : KLogging() {
private val helpInterfaces = listOf(
classOf<ICommand.HelpDialogProvider>(),
classOf<ICommand.HelpDialog>()
ICommand.HelpDialogProvider::class.java,
ICommand.HelpDialog::class.java
)

val NOOP_LISTENER = object : Listener {
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/pw/aru/psi/exported/exported.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ package pw.aru.psi.exported
/**
* Psi Version
*/
const val psi_version = "1.5"
const val psi_version = "1.6"
41 changes: 41 additions & 0 deletions src/main/kotlin/pw/aru/utils/extensions/lang/Collections.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
@file:Suppress("NOTHING_TO_INLINE")
@file:JvmName("LangExt")
@file:JvmMultifileClass

package pw.aru.utils.extensions.lang

fun <E> List<E>.bestSplit(minSize: Int, maxSize: Int): List<List<E>> {
return when {
size < maxSize -> listOf(this)
else -> chunked((minSize..maxSize).minBy { it - size % it } ?: (minSize + maxSize) / 2)
}
}

fun <T> Iterable<Iterable<T>>.roundRobinFlatten(): List<T> {
val result = ArrayList<T>()
val iterators = mapTo(ArrayList(), Iterable<T>::iterator)

while (iterators.isNotEmpty()) {
val i = iterators.iterator()
while (i.hasNext()) {
val ii = i.next()
if (ii.hasNext()) {
result.add(ii.next())
} else {
i.remove()
}
}
}

return result
}

inline fun <K, V> Map<K, V>.ifContains(k: K, function: (V) -> Unit) {
if (containsKey(k)) function(get(k)!!)
}

inline fun <E> List<E>.randomOrNull(): E? = if (isEmpty()) null else random()

inline fun <E> Array<E>.randomOrNull(): E? = if (isEmpty()) null else random()

inline fun <E> randomOf(vararg objects: E): E = objects.random()
11 changes: 11 additions & 0 deletions src/main/kotlin/pw/aru/utils/extensions/lang/Conditions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@file:Suppress("NOTHING_TO_INLINE")
@file:JvmName("LangExt")
@file:JvmMultifileClass

package pw.aru.utils.extensions.lang

inline fun anyOf(vararg cases: Boolean) = cases.any { it }

inline fun allOf(vararg cases: Boolean) = cases.all { it }

inline fun noneOf(vararg cases: Boolean) = cases.none { it }
12 changes: 12 additions & 0 deletions src/main/kotlin/pw/aru/utils/extensions/lang/Environiment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package pw.aru.utils.extensions.lang

import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty

object Environiment : ReadOnlyProperty<Any?, String> {
private val env by lazy { System.getenv() }

override fun getValue(thisRef: Any?, property: KProperty<*>): String {
return env.get(property.name) ?: throw IllegalStateException("No environment property ${property.name}")
}
}
11 changes: 11 additions & 0 deletions src/main/kotlin/pw/aru/utils/extensions/lang/Functions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@file:Suppress("NOTHING_TO_INLINE")
@file:JvmName("LangExt")
@file:JvmMultifileClass

package pw.aru.utils.extensions.lang

inline fun <T, U> T.applyOn(thisObj: U, block: U.() -> Unit): T {
thisObj.block()
return this
}

50 changes: 50 additions & 0 deletions src/main/kotlin/pw/aru/utils/extensions/lang/Futures.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
@file:Suppress("NOTHING_TO_INLINE")
@file:JvmName("LangExt")
@file:JvmMultifileClass

package pw.aru.utils.extensions.lang

import java.util.concurrent.CompletableFuture
import java.util.concurrent.CompletionStage
import java.util.concurrent.Future
import kotlin.reflect.KProperty

@JvmName("futureValue")
fun <V> Future<V>.value(): V = get()

@JvmName("completionValue")
fun <V> CompletionStage<V>.value(): V = stageToFuture().join()

@JvmName("futureCompletionValue")
fun <V, T> T.value(): V where T : Future<V>, T : CompletionStage<V> = get()

@JvmName("futureGetValue")
operator fun <V> Future<V>.getValue(r: Any?, p: KProperty<*>): V = get()

@JvmName("completionGetValue")
operator fun <V> CompletionStage<V>.getValue(r: Any?, p: KProperty<*>): V = stageToFuture().join()

@JvmName("futureCompletionGetValue")
operator fun <V, T> T.getValue(r: Any?, p: KProperty<*>): V where T : Future<V>, T : CompletionStage<V> = get()

private fun <T> CompletionStage<T>.stageToFuture(): CompletableFuture<T> {
return if (this is CompletableFuture<*>) {
this as CompletableFuture<T>
} else {
toCompletableFuture()
}
}

fun <T> Array<CompletionStage<T>>.awaitAll(): CompletionStage<Void> {
return CompletableFuture.allOf(
*map { it.stageToFuture() }
.toTypedArray()
)
}

fun <T> Collection<CompletionStage<T>>.awaitAll(): CompletionStage<Void> {
return CompletableFuture.allOf(
*map { it.stageToFuture() }
.toTypedArray()
)
}
23 changes: 13 additions & 10 deletions src/main/kotlin/pw/aru/utils/extensions/lang/JavaHttpClient.kt
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
@file:Suppress("NOTHING_TO_INLINE")
@file:JvmName("LangExt")
@file:JvmMultifileClass

package pw.aru.utils.extensions.lang

import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpResponse
import java.net.http.HttpResponse.BodyHandler
import java.util.concurrent.CompletableFuture
import java.net.http.HttpRequest.newBuilder as requestBuilder

private typealias RequestBuilder = HttpRequest.Builder.() -> Unit
private typealias CompletableResponse<T> = CompletableFuture<HttpResponse<T>>

fun <T> HttpClient.send(
bodyHandler: HttpResponse.BodyHandler<T>,
block: HttpRequest.Builder.() -> Unit
): HttpResponse<T> {
return send(HttpRequest.newBuilder().also(block).build(), bodyHandler)
inline fun <T> HttpClient.send(bodyHandler: BodyHandler<T>, block: RequestBuilder): HttpResponse<T> {
return send(requestBuilder().also(block).build(), bodyHandler)
}

fun <T> HttpClient.sendAsync(
bodyHandler: HttpResponse.BodyHandler<T>,
block: HttpRequest.Builder.() -> Unit
): CompletableFuture<HttpResponse<T>> {
return sendAsync(HttpRequest.newBuilder().also(block).build(), bodyHandler)
inline fun <T> HttpClient.sendAsync(bodyHandler: BodyHandler<T>, block: RequestBuilder): CompletableResponse<T> {
return sendAsync(requestBuilder().also(block).build(), bodyHandler)
}
Loading

0 comments on commit cc15236

Please sign in to comment.