Skip to content

Instantly share code, notes, and snippets.

@sorokod
sorokod / StubsAndSpiesTest
Created March 5, 2015 11:28
A Mockito cheat sheet / Junit test - subs and spies
import org.junit.Before;
import org.junit.Test;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import java.util.LinkedList;
import java.util.List;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
@sorokod
sorokod / TreeSetExample.java
Last active August 29, 2015 14:20
An example of pathological behaviour of TreeSet when ordering is inconsistent with equals
import java.util.Comparator;
import java.util.TreeSet;
// Blog post: http://david-soroko.blogspot.co.uk/2015/05/a-kink-in-api.html
public class TreeSetExample {
public static class Transaction {
private final String id;
private final int amount;
public Transaction(String id, int amount) {
@sorokod
sorokod / StringHashCollisions
Last active August 29, 2015 14:22
Code for generating collisions in String.hashCode
public class StringHashCollisions {
public static String padByZeroes(String original, int numberOfExtraZeroes) {
int length = original.length();
char[] chars = new char[length + numberOfExtraZeroes];
for (int i = 0; i < length; i++) {
chars[i + numberOfExtraZeroes] = original.charAt(i);
}
return new String(chars);
}
@sorokod
sorokod / List traversing benchmark.java
Last active August 27, 2016 11:56
Compare the performance of traversing LinkedList and ArrayList
package benchmark;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;
import java.util.*;
/**
* Run with:
* <code>
@sorokod
sorokod / Java 8 Map idioms.java
Last active August 27, 2016 11:56
Java 8 Map idioms
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
import static java.util.Arrays.stream;
import static java.util.Map.Entry.comparingByValue;
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.*;
@sorokod
sorokod / ReentrantLockVsSynchronized.java
Created February 18, 2016 13:04
A JMH benchmark comparing the throughput of synchronized vs ReentrantLock
package concurrent;
/**
* java -jar target\benchmarks.jar concurrent -t N -wi 10 -i 20 -rf csv -tu ms -si true
*/
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
@sorokod
sorokod / Sampler.java
Last active August 31, 2016 15:11
Sample a population of T according to provided distribution
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import static java.util.Arrays.binarySearch;
/**
* Sample a population of T given distribution provided by consecutive calls to update().
* <p>
* Example invocation:
@sorokod
sorokod / new_gist_file_1.js
Last active August 31, 2016 14:54
Custom Elastisearch analiyzer [es] [wtr]
DELETE /test
GET /test/_analyze
{ "field": "categories", "text": "123=Foxes"}
PUT /test
{
"settings": {
"analysis": {
"char_filter": {
@sorokod
sorokod / keybase.md
Last active September 30, 2016 22:09

Keybase proof

I hereby claim:

  • I am sorokod on github.
  • I am sorokod (https://keybase.io/sorokod) on keybase.
  • I have a public key ASDV2ZrLKohCLFWiAqG_mACguTv0XuNvX3v87mftoQos6Ao

To claim this, I am signing this object:

import java.io.IOException;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Stream;
import static java.nio.file.Files.lines;
import static java.nio.file.Paths.get;
import static java.util.stream.Collectors.toList;
public class FileLineByLine {