Skip to content

Instantly share code, notes, and snippets.

View bnorm's full-sized avatar
🐢

Brian Norman bnorm

🐢
View GitHub Profile
@bnorm
bnorm / Singleton.java
Created August 23, 2015 13:51
Allows collecting into 1 and only 1 object from a stream. Supports a few different finishing object types.
import java.util.Objects;
import java.util.Optional;
import java.util.StringJoiner;
import java.util.stream.Collector;
import java.util.stream.Collectors;
public class Singleton<T> {
private T value;
private volatile int count;
@bnorm
bnorm / StreamParalizer.java
Last active January 7, 2016 15:27
Utility for customizing the ForkJoinPool that a parallel stream uses.
import java.util.Comparator;
import java.util.DoubleSummaryStatistics;
import java.util.IntSummaryStatistics;
import java.util.Iterator;
import java.util.LongSummaryStatistics;
import java.util.Optional;
import java.util.OptionalDouble;
import java.util.OptionalInt;
import java.util.OptionalLong;
import java.util.PrimitiveIterator;
@bnorm
bnorm / Range.java
Last active February 15, 2016 16:43
Object ranges in Java
import java.util.Comparator;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Optional;
import java.util.function.IntPredicate;
public final class Range<T> {
private final Comparator<? super T> comparator;
private final Endpoint<T> min;
@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(", ");
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 / 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) {
@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 / 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 / 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> {
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)