Skip to content

Instantly share code, notes, and snippets.

View thomasnield's full-sized avatar

Thomas Nield thomasnield

  • Frisco, Texas (United States)
View GitHub Profile
@thomasnield
thomasnield / GridBagBuilder
Created April 20, 2015 17:14
GridBagBuilder
import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.function.Consumer;
/**
* This greatly streamlines using the GridBagLayout
* Instantiate with the targeted container as an argument through the static factory forContainer()
* Then use the "addItem" method to add components. After calling addItem, start chaining the GridBag parameters and then call "finalizeConstraints".
@thomasnield
thomasnield / LazyObject
Created April 20, 2015 17:18
LazyObject
import java.util.function.Supplier;
/**<html>A simple but powerful concurrency utility that simplifies lazy initialization. <br><br>Client simply provides a
* supplier for the lazy-initialized value to the static factory method <b>forSupplier()</b><br><br>
* A LazyObject will be returned by the static factory, and the first time <b>get()</b> is called, the value is initalized and cached in a concurrent/threadsafe manner.
* </html>
*/
public final class LazyObject<T> {
@thomasnield
thomasnield / BufferedLatch
Created April 20, 2015 18:04
BufferedLatch
/**<html>A synchronizer simliar to CountDownLatch, except it is used for situations where the countdown number is not known until later.<br><br>
*This makes it ideal for mulitthreading record-buffering tasks, where you want to kick off a concurrent task for each record but do not know how many records there will be</html>
*/
public final class BufferedLatch {
private int leadCount;
private int chaseCount;
private boolean leaderComplete = false;
public synchronized void incrementLeadCount() {
if (leaderComplete) {
@thomasnield
thomasnield / NestedInfiniteTest.java
Created August 31, 2015 13:56
Consolidated Infinite Observable<List<T>> Test
import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;
import rx.Observable;
import rx.functions.FuncN;
import rx.subjects.BehaviorSubject;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
@Test
public void hikariTest() {
try {
HikariConfig config = new HikariConfig();
config.setJdbcUrl("jdbc:sqlite://C:/mydatabases/mydb.db");
config.setUsername("bart");
config.setPassword("51mp50n");
config.setMinimumIdle(1);
config.setMaximumPoolSize(5);
import java.util.concurrent.atomic.AtomicBoolean
import kotlin.concurrent.thread
fun main(args: Array<String>) {
val queue = SingleBlockingQueue<Int>()
val isDone = AtomicBoolean(false)
thread {
for (i in 1..100) {
import java.util.concurrent.Semaphore;
public final class SingleBlockingQueue<T> {
private volatile T value;
private final Semaphore full = new Semaphore(0);
private final Semaphore empty = new Semaphore(1);
private volatile boolean hasValue = false;
public class JavaLauncher {
public static void main(String[] args) {
Observable<Integer> source = Observable.range(1,10);
source.map(i -> sleep(i, 250))
.doOnNext(i -> System.out.println("Emitting " + i + " on thread " + Thread.currentThread().getName()))
.flatMap(i ->
Observable.just(i)
.subscribeOn(Schedulers.computation())
.map(i2 -> sleep(i2 * 10, 500))
@thomasnield
thomasnield / kryo.kt
Created July 30, 2016 03:12 — forked from MarioAriasC/kryo.kt
Kryo serialization works out-of-the-box with Kotlin
package org.cakesolutions.spark.kryo
import com.esotericsoftware.kryo.Kryo
import com.esotericsoftware.kryo.io.Input
import com.esotericsoftware.kryo.io.Output
import java.io.FileInputStream
import java.io.FileOutputStream
fun main(args: Array<String>) {
val kryo = Kryo()
//compile 'de.javakaffee:kryo-serializers:0.38'
class KryoImmutableListTest {
class MyItem(val x: Int, val y: Int) {
private constructor(): this(-1,-1)
override fun toString() = "$x-$y"
}
@Test