Skip to content

Instantly share code, notes, and snippets.

View chids's full-sized avatar
👻
Not my 🎪, not my 🐒's.

Mårten Gustafson chids

👻
Not my 🎪, not my 🐒's.
View GitHub Profile
@chids
chids / dropwizard-json-round-trip-test.java
Created May 22, 2012 14:20
Dropwizard representation round trip test (instance -> json -> instance)
public static <T> void jsonRoundTripTest(final T instance) throws Exception {
final String clazz = instance.getClass().getName();
final String fixtureName = instance.getClass().getSimpleName().toLowerCase();
final String fixture = jsonFixture("fixtures/" + fixtureName + ".json");
assertThat(clazz + " bad json,",
asJson(instance),
is(fixture));
T deserialized;
try {
deserialized = fromJson(fixture, (Class<T>)instance.getClass());
@chids
chids / erl-conway-game-of-life.erl
Created September 17, 2011 14:45
2011-09-17 - Erlang Conway Game Of Life
-module(gol).
-include_lib("eunit/include/eunit.hrl").
next(World) ->
sets:fold(fun(Member, Result) ->
Count = sets:size(live_neighbours(Member, World)),
case Count of
2 ->
sets:add_element(Member, Result);
_ ->
@chids
chids / b0rked-null-check-and-mask.java
Created September 16, 2011 10:11
How not to write null checking/masking logic
if(leftImage != null)
{
leftImage = leftImage.toLowerCase();
}
if(leftImage.equals("null"))
{
leftImage = null;
}
// ... repeated for 5 different string variables
@chids
chids / priority-queue-sorting-weirdness.java
Created September 12, 2011 16:46
PriorityQueue sorting weirdness
System.err.println(new PriorityQueue<Integer>(Arrays.asList(3, 1, 2)));
// Prints: [1, 3, 2]
System.err.println(new TreeSet<Integer>(Arrays.asList(3, 1, 2)));
// Prints: [1, 2, 3]
/*
This is due to PriorityQueue's Iterator. From the javadoc:
"...Iterator provided in method iterator() is not guaranteed to
traverse the elements of the priority queue in any particular order...".
@chids
chids / init.pp
Created June 21, 2011 17:51
Puppet Riak Manifest
class riak {
$configbase = "/etc/riaksearch"
$riak_dir = "/var/lib/riaksearch"
$username = "riak"
package { "riak-search": ensure => latest, }
user { $username:
shell => '/bin/bash',
comment => 'Basho Riak',
@chids
chids / Macro
Created May 30, 2011 20:23
Confluence Macro - Render XML response from HTTP service
#set($path=$param0.replace("::", "="))
#set($space=$renderContext.getOriginalContext().getSpaceKey())
#if($space.equals("PRIVATE"))
#set($domain="http://your.internal.url")
#else
#set($domain="http://your.public.url")
#end
#set($url="${domain}${path}")
-module(a).
-export([tgif/0]).
-define(MUSIC_ASSOCIATIONS, [{ellnestam, "Love Gun"}, {jockeholm, "Family Affair"}, {mahnve, "My name is (Slim Shady)"}]).
tgif() -> tgif(?MUSIC_ASSOCIATIONS).
tgif([{Person, Track}|Others]) ->
io:format("@~w plays '~s'~n", [Person, Track]),
tgif(Others);
tgif([]) -> "Hey! It's friday, have a beer!".
@chids
chids / nginx-file-extension-to-accept-header.txt
Created March 17, 2011 11:54
How to set the HTTP accept header based on the file extension in Nginx
# Copy the accept header to a variable
set $acceptHeader $http_accept;
# Does the request end in xml or json?
if ($uri ~ ^(.*)\.(xml|json)$) {
# Create a new accept header...
set $acceptHeader "application/$2";
# ...and remove the extension from the path
rewrite ^(.*)\.(xml|json)$ $1 last;
}
@chids
chids / neo4j-spatial-test-pom.xml
Created March 16, 2011 13:44
Test POM for playing with Neo4J Spatial
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>neo4j-spatial</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-referencing</artifactId>
@chids
chids / confluence-whats-my-space
Created March 15, 2011 09:57
Confluence Wiki: Get the space key of the space that this page is being rendered in
##
## Atlassian Confluence Wiki User Macro
##
## Example of how to get the space key of the space that this page is being rendered in
## ie, not necessarily the same space as the page belongs to
##
#set($space=$renderContext.getOriginalContext().getSpaceKey())
## This will allow you to do magic based on the space