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 / 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 / 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 / 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 / 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 / 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 / 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 / gist:3748732
Created September 19, 2012 09:41
jdk-7-b147-linux-os-sleep
int os::sleep(Thread* thread, jlong millis, bool interruptible) {
assert(thread == Thread::current(), "thread consistency check");
ParkEvent * const slp = thread->_SleepEvent ;
slp->reset() ;
OrderAccess::fence() ;
if (interruptible) {
jlong prevtime = javaTimeNanos();
@chids
chids / DropwizardPorts.java
Created November 1, 2012 14:23
Runtime setting of Dropwizard HTTP and admin port
package omni.backend.pipeline.service.ports;
import static com.google.common.collect.Maps.immutableEntry;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import org.elasticsearch.common.collect.ImmutableMap;
import com.yammer.dropwizard.config.HttpConfiguration;

So I guess I should explicitly exclude joda-time from the dropwizard-core dep and then declare it as an explicit dependency in my pom, thus locking down the version I actually want?

[WARNING] 
Dependency convergence error for joda-time:joda-time:2.2 paths to dependency are:
+-my:component:0.0.3-SNAPSHOT
  +-com.yammer.dropwizard:dropwizard-core:0.6.2
    +-joda-time:joda-time:2.2
and
+-my:component:0.0.3-SNAPSHOT
@chids
chids / 256kb.json
Last active March 18, 2024 16:07
Roughly 256kb of indented JSON i.e. AWS SNS and AWS SQS message limit and a hint not to publish pretty printed JSON ;)
[
{
"id": 0,
"guid": "1b8c3019-c0b6-44f9-b35c-5b1b2729551a",
"isActive": true,
"balance": "$3,163.00",
"picture": "http://placehold.it/32x32",
"age": 32,
"name": "Saunders Sosa",
"gender": "male",