Skip to content

Instantly share code, notes, and snippets.

Avatar
🦎

Sergey Mashkov cy6erGn0m

🦎
View GitHub Profile
@cy6erGn0m
cy6erGn0m / Example.kt
Created Sep 15, 2021
Using Java LambdaMetafactory to convert Kotlin property to lambda without reflections and faster
View Example.kt
// Please note that you don't even need kotlin-reflect.jar
class C {
val f: String
get() = "ok"
}
fun usage() {
val fn = makeFunction(C::f)
println(fn(C()))
@cy6erGn0m
cy6erGn0m / MyJavaBlockingQueue.kt
Created Nov 21, 2018
Simplified async to blocking adapter
View MyJavaBlockingQueue.kt
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
import java.util.concurrent.locks.*
import kotlin.coroutines.*
private val MyEventLoop: CoroutineDispatcher = TODO()
/**
* This is simplified blocking adapter example.
* NEVER use in production as it is just clarification, not actual implementation
@cy6erGn0m
cy6erGn0m / ktor-route-attributes
Last active Oct 23, 2018
Assigning call attributes based or route with default
View ktor-route-attributes
private val XFrameOptionsAttribute = AttributeKey<XFrameOptions>("X-Frame-Options")
sealed class XFrameOptions {
object Deny : XFrameOptions() {
override fun toString() = "deny"
}
object SameOrigin : XFrameOptions() {
override fun toString() = "sameorigin"
}
class AllowFrom(val url: String) : XFrameOptions() {
View ktor-route-interceptors.kt
sealed class XFrameOptions {
object Deny : XFrameOptions() {
override fun toString() = "deny"
}
object SameOrigin : XFrameOptions() {
override fun toString() = "sameorigin"
}
class AllowFrom(val url: String) : XFrameOptions() {
override fun toString(): String = "allow-from $url"
}
@cy6erGn0m
cy6erGn0m / JettyClientTest.kt
Created Jul 28, 2016
Jetty HTTP client issue testcase
View JettyClientTest.kt
package org.jetbrains.ktor.client
import org.eclipse.jetty.http2.client.*
import org.eclipse.jetty.http2.client.http.*
import org.eclipse.jetty.http2.server.*
import org.eclipse.jetty.server.*
import org.eclipse.jetty.servlet.*
import org.eclipse.jetty.util.thread.*
import org.junit.*
import java.util.concurrent.*
@cy6erGn0m
cy6erGn0m / kotlin-ehcache.kt
Created Feb 5, 2016
Kotlin ehcache example
View kotlin-ehcache.kt
val cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
.with(CacheManagerPersistenceConfiguration(storagePath))
.withCache("kweetsCache",
CacheConfigurationBuilder.newCacheConfigurationBuilder<Int, Kweet>()
.withResourcePools(ResourcePoolsBuilder.newResourcePoolsBuilder()
.heap(1000, EntryUnit.ENTRIES)
.offheap(10, MemoryUnit.MB)
.disk(100, MemoryUnit.MB, true)
)
.buildConfig(Int::class.javaObjectType, Kweet::class.java))
@cy6erGn0m
cy6erGn0m / freeport.kt
Created Jan 18, 2016
Kotlin get free port
View freeport.kt
fun findFreePort() = ServerSocket(0).use { it.localPort }
@cy6erGn0m
cy6erGn0m / build.gradle
Last active Dec 23, 2015
Configuring testing library in kotlin
View build.gradle
dependencies {
testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlinVersion"
}
@cy6erGn0m
cy6erGn0m / draganddrop.kt
Created Dec 1, 2015
Drag and drop example
View draganddrop.kt
package dnd
import java.awt.*
import java.awt.dnd.*
import javax.swing.*
fun main(args: Array<String>) {
SwingUtilities.invokeAndWait {
val f = JFrame("test")
f.defaultCloseOperation = JFrame.EXIT_ON_CLOSE
@cy6erGn0m
cy6erGn0m / pairs-sequence.kt
Created Nov 12, 2015
Returns a sequence that consists of all possible pair of original list elements, does nothing with potential duplicates
View pairs-sequence.kt
/**
* Returns a sequence that consists of all possible pair of original list elements, does nothing with potential duplicates
* @param skipSamePairs indicates whether it should produce pairs from the same element at both first and second positions
*/
fun <T> List<T>.allPairs(skipSamePairs: Boolean = true): Sequence<Pair<T, T>> = PairsSequence(this, skipSamePairs)
private class PairsSequence<T>(val source: List<T>, val skipSamePairs: Boolean) : Sequence<Pair<T, T>> {
override fun iterator(): Iterator<Pair<T, T>> = PairsIterator(source, skipSamePairs)
}