Skip to content

Instantly share code, notes, and snippets.

View grignaak's full-sized avatar

Michael Deardeuff grignaak

  • Seattle, WA, USA
View GitHub Profile
@grignaak
grignaak / gist:835766
Created February 20, 2011 06:15
Meta-programming in coffescript
class Entity
applyEvent: (event, args) ->
@[Entity._handler event] args...
@_handler: (name) -> "on_#{name}"
@event: (name, handler) ->
@::[name] = ->
@applyEvent name, arguments
@::[Entity._handler name] = handler
@grignaak
grignaak / Global.java
Created February 22, 2011 20:16
Singleton to end all singletons
package common;
import java.util.HashMap;
import java.util.Map;
public class Global {
private static boolean inTests;
private static final Map<Class<?>, Object> globals = new HashMap<Class<?>, Object>();
private Global() {}
@grignaak
grignaak / coffee.js
Created March 10, 2011 23:08
A requireJS plugin to load CoffeeScript
(function() {
var origDefine = define;
function redefine(name, definitions) {
return function (prereqs, definition) {
if (arguments.length > 2) {
throw new Error('Cannot have more than two arguments to define in ' + name);
} else if (arguments.length === 1) {
definition = prereqs;
prereqs = [];
@grignaak
grignaak / PredicatedCursor.java
Created May 25, 2011 06:45
Ceylon vs Java iterators--implementation
public static class PredicatedCursor<T> implements Cursor<T> {
public interface Predicate<T> {
boolean apply(T element);
}
private final Predicate<? super T> predicate;
private final Cursor<T> delegate;
public PredicatedCursor(Cursor<T> delegate, Predicate<? super T> predicate) {
Cursor<T> current = delegate;
@grignaak
grignaak / args.rb
Created June 16, 2011 17:35
Args parser
class Args
def self.expect &proc
parser = self.new
parser.instance_eval &proc
return parser
end
attr_reader :positional
def [](name)
@args.fetch(name.to_s, NullMarshaler).value
@grignaak
grignaak / Exceptions.java
Created July 14, 2011 21:04
Exception Utilities; including undeclared checked exceptions
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
public class Exceptions {
private static Throwable toThrow;
private Exceptions() throws Throwable {
throw toThrow;
}
@grignaak
grignaak / EventQueue.java
Created July 14, 2011 21:06
Swing EDT utilities
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicReference;
import javax.swing.SwingUtilities;
public class EventQueue {
private EventQueue() {}
public static void invokeAndWait(Runnable doRun) {
@grignaak
grignaak / BooleanLatch.java
Created September 20, 2011 17:56
Queues that share a tail, but have independent heads
package hydra;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.AbstractQueuedSynchronizer;
/**
* Taken mostly from the documentation of {@link AbstractQueuedSynchronizer}
*/
public class BooleanLatch {
@SuppressWarnings("serial")
@grignaak
grignaak / WaitFreeTest.java
Created October 4, 2011 08:51
Wait free vs SynchronizedDeque vs Partially Synchronized Queues
// The wait free algorithm is at the very bottom of the page.
package performance;
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
@grignaak
grignaak / ClassKey.java
Created November 22, 2011 05:40
An basic base implementation of the RoleObjectPattern
package net.deardeuff.roleobject;
import com.google.common.base.Preconditions;
/**
* A Key based on a Class
*/
public final class ClassKey<T> implements View.Key<T> {
private final Class<T> delegate;