Skip to content

Instantly share code, notes, and snippets.

View UnquietCode's full-sized avatar

Benjamin Fagin UnquietCode

View GitHub Profile
@UnquietCode
UnquietCode / ContextAware.java
Last active December 19, 2015 03:29
An encapsulation of the State Pattern in Java (http://en.wikipedia.org/wiki/State_pattern)
public interface ContextAware<T> {
void beforeMethod(AtomicReference<T> context);
}
@UnquietCode
UnquietCode / RedisResource.java
Last active December 19, 2015 09:49
TryWithResource, because sometimes you don't have Java7, and even if you do the actual try-with-resource is somewhat clunky.
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
public class RedisResource extends TryWithResource<Jedis> {
private final JedisPool pool;
public RedisResource(JedisPool pool) {
this.pool = pool;
}
@UnquietCode
UnquietCode / Example.java
Last active December 20, 2015 21:09
I use this handler class when I want to have dynamically typed, variable length parameters.
registerStudent(student, new HandlerChain<RegistrationStep>()
.on(RegistrationStep.NEW_STUDENT, new FlexibleHandler() {
void handle(NewRegistrant registrant) {
System.out.println(registrant.info());
}
})
.on(RegistrationStep.USER_EXISTS, new FlexibleHandler() {
void handle(NewRegistrant registrant, UserContainer existingUser) {
System.out.println(registrant.info());
System.out.println(existingUser.info());
@UnquietCode
UnquietCode / convert_to_tag.sh
Created August 16, 2013 22:13
Converts a git branch to a git tag, by passing in the branch name. Turns a branch "my-branch" into a tag "my-branch.final" and deletes the branch.
git checkout $1
git pull origin $1
git tag $1.final
git checkout master
git branch -d $1
git push origin --tags :$1
@UnquietCode
UnquietCode / CustomRequestMappingHandlerMapping.java
Created August 23, 2013 20:32
Custom implementation of Spring's RequestMappingHandlerMapping which automatically registers a redirect for the same url with (or without) a trailing slash. This is good in cases where for SEO purposes you want 'url' and 'url/' to resolve to the same page.
package com.sb.server.web;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import org.springframework.web.servlet.view.RedirectView;
@UnquietCode
UnquietCode / API.java
Created November 14, 2013 19:49
Keep track of your API in your code. This also allows you to easily identify places in your application where legacy blocks ('hacks') can be removed.
public enum API {
@Deprecated Version1,
Version2(
add(Feature.BLUE_ICONS)
),
Version3(
parent(Version2),
{
name: "Object"
version: "1"
parents: []
properties: {
name: "string"
version: "string"
parents: "string*"
properties: "object"
@UnquietCode
UnquietCode / MapperModule.java
Last active April 8, 2019 19:06
Jackson module which can instantiate interface types using a proxy.
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.deser.Deserializers;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.io.IOException;
import java.util.Iterator;
@UnquietCode
UnquietCode / A.js
Last active August 29, 2015 14:07
gulp-browatchify test
var B = require('./B');
console.log("hello");
@UnquietCode
UnquietCode / JsonMerge.coffee
Last active July 25, 2017 15:05
Merge two or more JSON objects in JavaScript (CoffeeScript).
valueOrCopy = (obj) ->
if not obj
return undefined
else if obj instanceof Array
newObj = []
newObj.push(x) for x in obj
return newObj
else if (typeof obj).toLowerCase() is 'object'