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 / Keycount.java
Created July 25, 2012 10:45
Java HashMap count keys dummy
new HashMap<Integer, Integer>().put(value, Optional.fromNullable(actual.get(value)).or(0) + 1);
@chids
chids / jmdns.java
Created May 25, 2012 09:59
How to break JmDNS (trunk as of 2012-05-25 - revision 340, fixed in revision 341)
package javax.jmdns.test;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.util.Enumeration;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;
@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;
}