Skip to content

Instantly share code, notes, and snippets.

Avatar

Mike Hearn mikehearn

View GitHub Profile
@mikehearn
mikehearn / AffinityExecutor.java
Last active Nov 18, 2020
Some code for JavaFX observable collections (maps and sets) which replicate changes between threads. From the open source, Apache licensed Lighthouse project. Check there for the latest code.
View AffinityExecutor.java
// Contact: hearn@vinumeris.com
package lighthouse.threading;
import com.google.common.util.concurrent.Uninterruptibles;
import javafx.application.Platform;
import lighthouse.protocol.LHUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
View build-jfx-jdk.sh
#!/usr/bin/env bash
#
# To use, unpack a regular JDK somewhere and set JDK_HOME to point to it. The default here expects a JDK 15 early access
# in the same directory but you can set JDK_HOME to anything.
#
JDK_HOME=${JDK_HOME:-$PWD/jdk-15.jdk/Contents/Home}
#
@mikehearn
mikehearn / threadbox.kt
Created Aug 15, 2015
More advanced ThreadBox with affinity guards
View threadbox.kt
// This is a class that attempts to stop you accessing variables outside a lock.
//
// It does not do a perfect job, but can catch some common kinds of mistake, in
// particular when you accidentally try to work with objects inside closures that
// end up running later, outside the locked region (or in a different thread).
// EXAMPLE
val bank = ThreadBox(object {
val accounts by arrayListOf(10, 0, 0, 0).guard()
View BriefLogFormatter.kt
// A couple of inlined utility functions: the first is just a syntax convenience, the second lets us use
// Kotlin's string interpolation efficiently: the message is never calculated/concatenated together unless
// logging at that level is enabled.
inline fun <reified T : Any> loggerFor(): org.slf4j.Logger = LoggerFactory.getLogger(T::class.java)
inline fun org.slf4j.Logger.trace(msg: () -> String) {
if (isTraceEnabled) trace(msg())
}
/**
* A Java logging formatter that writes more compact output than the default.
View nodejvm1.java
import net.plan99.nodejs.NodeJS;
public class Demo {
public static void main(String[] args) {
int result = NodeJS.runJS(() ->
NodeJS.eval("return 2 + 3 + 4").asInt()
);
System.out.println(result);
}
}
@mikehearn
mikehearn / Kryo Kotlin class serialiser.kt
Created Dec 17, 2015
A Kryo serialiser that lets you serialise immutable classes like "class Foo(val a: Int, val b: Bar)" without bypassing the c'tor.
View Kryo Kotlin class serialiser.kt
interface SerializeableWithKryo
class ImmutableClassSerializer<T : SerializeableWithKryo>(val klass: KClass<T>) : Serializer<T>() {
val props = klass.memberProperties.sortedBy { it.name }
val propsByName = props.toMapBy { it.name }
val constructor = klass.primaryConstructor!!
init {
// Verify that this class is immutable (all properties are final)
assert(props.none { it is KMutableProperty<*> })
@mikehearn
mikehearn / KotlinDuckTyping.kt
Created Dec 18, 2015
Kotlin duck typing / type classing fiddle
View KotlinDuckTyping.kt
class A {
fun shout() = println("go team A!")
}
class B {
fun shout() = println("go team B!")
}
interface Shoutable {
fun shout()
@mikehearn
mikehearn / kotin-native-win32.kt
Created Sep 28, 2018
Example of using Kotlin/Native on Windows
View kotin-native-win32.kt
typealias WSTR = CPointer<ShortVar>
private fun WSTR.toKString(): String = memScoped {
// Figure out how much memory we need after UTF-8 conversion.
val sz = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, this@toKString, -1, null, 0, null, null)
// Now convert to UTF-8 and from there, a String.
val utf8 = allocArray<ByteVar>(sz)
val r = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, this@toKString, -1, utf8, sz, null, null)
if (r == 0) throw RuntimeException("Could not convert to UTF-8")
utf8.toKString()
View tinyram-opcodes.cpp
enum tinyram_opcode {
tinyram_opcode_AND = 0b00000,
tinyram_opcode_OR = 0b00001,
tinyram_opcode_XOR = 0b00010,
tinyram_opcode_NOT = 0b00011,
tinyram_opcode_ADD = 0b00100,
tinyram_opcode_SUB = 0b00101,
// Multiplications
tinyram_opcode_MULL = 0b00110,
@mikehearn
mikehearn / di-helpers.kt
Created Dec 21, 2017
Simple dependency helpers
View di-helpers.kt
package net.corda.node.internal
import java.time.Clock
import java.time.Duration
import java.time.Instant
import java.time.ZoneId
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
/**
You can’t perform that action at this time.