Skip to content

Instantly share code, notes, and snippets.

View raphw's full-sized avatar

Rafael Winterhalter raphw

View GitHub Profile
@raphw
raphw / Pyramid.java
Last active August 29, 2015 13:57
Solution to Reaktor's pyramid challenge: http://reaktor.fi/careers/fast_track/
import org.junit.Test;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
@raphw
raphw / Java8Rule.java
Created June 4, 2014 12:27
This rule allows to write tests that are only executed if the running JVM supports Java 8 byte code.
public class Java8Rule implements MethodRule {
private final boolean java8OrHigher;
public Java8Rule() {
java8OrHigher = currentByteCodeLevel() >= (0 << 16 | 52);
}
@Override
public Statement apply(Statement base, FrameworkMethod method, Object target) {
@raphw
raphw / HashCodeEqualsTester.java
Created September 15, 2014 09:25
An automatic tester for the hashCode and equals method using Mockito.
import org.objectweb.asm.Opcodes;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.List;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
@raphw
raphw / TypeSafeJPA.java
Created September 26, 2014 13:41
A concept for making JPA queries type-safe using code generation.
public class TypeSafeJPA {
class MyBean {
private String foo;
private int bar;
public String getFoo() {
return foo;
@raphw
raphw / SynchronizationBenchmark.java
Last active August 29, 2015 14:07
Benchmark for calling synchronized code either by synchronizing a method or by synchronizing a block of code inside this method.
package benchmark;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import java.util.concurrent.TimeUnit;
@State(Scope.Group)
@raphw
raphw / ManifestAnnotation.java
Created November 17, 2014 12:13
NullPointerException demo
import java.lang.annotation.*;
public class ManifestAnnotation implements SampleAnnotation {
private final String value;
public ManifestAnnotation(String value) {
this.value = value;
}
@raphw
raphw / HashCode.java
Last active August 29, 2015 14:11
Manual hash code computation
class HashCode {
public static void main(String[] args) {
Object object = new Object();
printHash(object.hashCode());
Field field = Unsafe.class.getDeclaredField("theUnsafe");
field.setAccessible(true);
Unsafe unsafe = (Unsafe) field.get(null);
long hashCode = 0;
for (long index = 7; index > 0; index--) { // First byte is not part of the hash code
hashCode |= (unsafe.getByte(object, index) & 0x00FF) << ((index - 1) * 8);
@raphw
raphw / ClassLayout.java
Last active August 29, 2015 14:11
Modified version of JOL's class layout
/*
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
@raphw
raphw / FastListBenchmark.java
Last active August 29, 2015 14:11
Benchmark of FastList
@State(Scope.Thread)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
public class FastListBenchmark {
private static final int LOAD = 10;
private ArrayList<Object> arrayList;
@raphw
raphw / EduAVLTree.java
Created March 13, 2015 08:26
Educational AVL tree in Java (from Google code)
/*
* EduAVLTree: An AVL tree implementation of educational intention.
*
* Copyright (C) 2011 Rafael W.
*
* EduAVLTree is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*