Skip to content

Instantly share code, notes, and snippets.

View tobyweston's full-sized avatar

Toby tobyweston

View GitHub Profile
tobyweston / gist:caefc3b5ec36348387e5
Created September 22, 2014 18:52
Naive implementation of Either in JAva
View gist:caefc3b5ec36348387e5
package bad.robot;
import fj.F;
import java.util.function.Function;
public interface Either<A, B> {
static <A, B> Either<A, B> left(final A a) {
View gist:2c7075e5e3311faca5ef
def maprec[B](f: A => B): List[B] = {
def recur(head: A, tail: List[A]): List[B] = {
tail match {
case Nil => List(f.apply(head))
case _ => f.apply(head) +: recur(tail.head, tail.tail)
recur(elements.head, elements.tail)
tobyweston / FunctionInterfaceOps.scala
Created July 21, 2015 16:51
Java 8 to Scala bridge methods
View FunctionInterfaceOps.scala
package bad.robot.radiate
import java.util.concurrent.Callable
import java.util.function.{Consumer, Supplier}
object FunctionInterfaceOps {
implicit def toConsumer[A](function: A => Unit): Consumer[A] = new Consumer[A]() {
override def accept(arg: A): Unit = function.apply(arg)
tobyweston /
Created November 18, 2011 16:31
Is this how the Announcer is used?
package auctionsniper.util;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.jmock.integration.junit4.JMock;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.util.EventListener;
tobyweston / octopress_me
Created February 9, 2012 19:04
What am I doing wrong with local Ocotpress generate?
View octopress_me
# my _config.yml section
root: /
permalink: /blog/:year/:month/:day/:title/
source: source
destination: public/blog
code_dir: downloads/code
category_dir: blog/categories
This deploys fine to github pages ( and with a CNAME
tobyweston / gist:2157239
Created March 22, 2012 09:01
mapping widget - a little bit of yuk
View gist:2157239
public static class ButtWipe {
public static <K, V> MapBuilder<K, V> map(K key, V value) {
return new MapBuilder<K, V>().map(key, value);
public static class MapBuilder<K, V> extends HashMap<K, V> {
private MapBuilder() {
tobyweston / gist:2819829
Created May 28, 2012 15:56
When running genuine multithreaded test with JMock, I don't want to synchronise
View gist:2819829
Name: Interrupter-Thread-1
State: BLOCKED on java.lang.Object@381eb0c6 owned by: main
Total blocked: 1 Total waited: 0
Stack trace:
tobyweston / gist:2841959
Created May 31, 2012 08:38
Avoid finalizer problems with JMocks SingleThreadedPolicy
View gist:2841959
public static class SingleThreadedPolicyAvoidingFinaliseProblems extends SingleThreadedPolicy {
public Invokable synchroniseAccessTo(final Invokable mockObject) {
final Invokable synchronizedMockObject = super.synchroniseAccessTo(mockObject);
return new Invokable() {
public Object invoke(Invocation invocation) throws Throwable {
if (Thread.currentThread().getName().equalsIgnoreCase("Finalizer"))
return mockObject.invoke(invocation);
return synchronizedMockObject.invoke(invocation);
tobyweston / gist:3362116
Created August 15, 2012 18:19
c3p0 configuration settings
View gist:3362116
class ProductionHibernateConfiguration implements HibernateConfiguration {
public Properties getProperties(String connectionUrl, String userName, String password) {
Properties properties = new Properties();
properties.setProperty("hibernate.connection.driver_class", "net.bull.javamelody.JdbcDriver");
properties.setProperty("hibernate.connection.driver", "oracle.jdbc.driver.OracleDriver");
properties.setProperty("hibernate.connection.url", connectionUrl);
properties.setProperty("hibernate.connection.username", userName);
properties.setProperty("hibernate.connection.password", password);
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect");
tobyweston / gist:3733479
Created September 16, 2012 17:57
Sales tax calculations
View gist:3733479
public class TaxRate extends AbstractValueType<Double> {
public static TaxRate taxRate(Double value) {
return new TaxRate(value);
private TaxRate(Double value) {