Skip to content

Instantly share code, notes, and snippets.

View bnorm's full-sized avatar
🐢

Brian Norman bnorm

🐢
View GitHub Profile
@bnorm
bnorm / ByteBuffers.java
Last active June 28, 2022 03:50
Okio Source/Sink for ByteBuffers
import java.io.IOException;
import java.nio.ByteBuffer;
import okio.Buffer;
import okio.BufferedSink;
import okio.BufferedSource;
import okio.Okio;
import okio.Sink;
import okio.Source;
import okio.Timeout;
@bnorm
bnorm / strings.kt
Created December 20, 2017 20:33
Turns a Kotlin Data class toString() into formatted JSON-like output
fun Any?.toIndentString(): String {
val notFancy = toString()
return buildString(notFancy.length) {
var indent = 0
fun StringBuilder.line() {
appendln()
repeat(2 * indent) { append(' ') }
}
@bnorm
bnorm / javax.script.ScriptEngineFactory
Last active November 29, 2017 14:56
Kotlin Script Engine
org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmLocalScriptEngineFactory
import java.util.*
fun main(args: Array<String>) {
val i = DoubleMatrix.identity(4)
val a = DoubleMatrix.ofRows {
row(1.0, 2.0, 3.0, 4.0)
row(5.0, 6.0, 7.0, 8.0)
row(9.0, 10.0, 11.0, 12.0)
row(13.0, 14.0, 15.0, 16.0)
@bnorm
bnorm / rxjava.kt
Created October 18, 2017 13:00
Turn a RxJava Single or Maybe into a Kotlin delegate
import io.reactivex.Maybe
import io.reactivex.Single
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty
operator fun <T> Single<T>.provideDelegate(thisRef: Any?, property: KProperty<*>): ReadOnlyProperty<Any?, T> {
return SingleDelegate(this)
}
private class SingleDelegate<out T>(single: Single<T>) : ReadOnlyProperty<Any?, T> {
@bnorm
bnorm / xref.kt
Last active November 29, 2020 08:41
One to many relationship via property delegation - Inspiration: https://github.com/jcornaz/xref
object OneToMany {
private class One<O, M, C : MutableCollection<M>>(initial: O?, private val rightProperty: KMutableProperty1<O, C>) : ReadWriteProperty<M, O?> {
var one: O? = initial
override fun getValue(thisRef: M, property: KProperty<*>): O? = one
override fun setValue(thisRef: M, property: KProperty<*>, value: O?) {
when {
value != null -> add(value, thisRef)
@bnorm
bnorm / Try.kt
Last active October 4, 2017 23:56
Try for Kotlin
sealed class Try<out T> {
companion object {
operator fun <T> invoke(f: () -> T) = try {
Success(f())
} catch (t: Throwable) {
Failure(t)
}
@JvmStatic
@bnorm
bnorm / HexCharset.java
Created March 29, 2017 21:22
Charset for encoding and decoding bytes as hexidecimal
public class HexCharset extends Charset {
public static final HexCharset INSTANCE = new HexCharset();
private HexCharset() {
super("hex", new String[0]);
}
@Override
public boolean contains(Charset cs) {
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
import org.apache.commons.math3.geometry.euclidean.twod.Line;
import org.apache.commons.math3.geometry.euclidean.twod.Vector2D;
import com.ibm.coderally.agent.CRSFileLog;
import com.ibm.coderally.agent.DefaultCarAIAgent;
import com.ibm.coderally.api.agent.AIUtils;
@bnorm
bnorm / Grammar.java
Created March 11, 2016 17:12
Class for collecting strings and producing a grammatically correct sentence fragment
public class Grammar<T> {
private T last;
private int count;
private final StringJoiner joiner;
public Grammar() {
this.last = null;
this.count = 0;
this.joiner = new StringJoiner(", ");