Skip to content

Instantly share code, notes, and snippets.

View tobyweston's full-sized avatar

Toby tobyweston

View GitHub Profile
@tobyweston
tobyweston / gist:caefc3b5ec36348387e5
Created September 22, 2014 18:52
Naive implementation of Either in JAva
package bad.robot;
import fj.F;
import java.util.function.Function;
import java.util.stream.Stream;
public interface Either<A, B> {
static <A, B> Either<A, B> left(final A a) {
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
tobyweston / FunctionInterfaceOps.scala
Created July 21, 2015 16:51
Java 8 to Scala bridge methods
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
tobyweston / AnnouncerTest.java
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
tobyweston / octopress_me
Created February 9, 2012 19:04
What am I doing wrong with local Ocotpress generate?
# 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 (https://github.com/tobyweston/blog) and with a CNAME badrobot.com
@tobyweston
tobyweston / gist:2157239
Created March 22, 2012 09:01
mapping widget - a little bit of yuk
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
tobyweston / gist:2819829
Created May 28, 2012 15:56
When running genuine multithreaded test with JMock, I don't want to synchronise
Name: Interrupter-Thread-1
State: BLOCKED on java.lang.Object@381eb0c6 owned by: main
Total blocked: 1 Total waited: 0
Stack trace:
org.jmock.lib.concurrent.Synchroniser.synchroniseInvocation(Synchroniser.java:82)
org.jmock.lib.concurrent.Synchroniser.access$000(Synchroniser.java:23)
org.jmock.lib.concurrent.Synchroniser$1.invoke(Synchroniser.java:74)
org.jmock.lib.legacy.ClassImposteriser$4.invoke(ClassImposteriser.java:136)
com.google.code.tempusfugit.temporal.Clock$$EnhancerByCGLIB$$489249d6.create(<generated>)
@tobyweston
tobyweston / gist:2841959
Created May 31, 2012 08:38
Avoid finalizer problems with JMocks SingleThreadedPolicy
public static class SingleThreadedPolicyAvoidingFinaliseProblems extends SingleThreadedPolicy {
@Override
public Invokable synchroniseAccessTo(final Invokable mockObject) {
final Invokable synchronizedMockObject = super.synchroniseAccessTo(mockObject);
return new Invokable() {
@Override
public Object invoke(Invocation invocation) throws Throwable {
if (Thread.currentThread().getName().equalsIgnoreCase("Finalizer"))
return mockObject.invoke(invocation);
return synchronizedMockObject.invoke(invocation);
@tobyweston
tobyweston / gist:3362116
Created August 15, 2012 18:19
c3p0 configuration settings
class ProductionHibernateConfiguration implements HibernateConfiguration {
@Override
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
tobyweston / gist:3733479
Created September 16, 2012 17:57
Sales tax calculations
public class TaxRate extends AbstractValueType<Double> {
public static TaxRate taxRate(Double value) {
return new TaxRate(value);
}
private TaxRate(Double value) {
super(value);
}