Skip to content

Instantly share code, notes, and snippets.

@jhorstmann
jhorstmann / ConstantTracker.java
Created September 7, 2011 22:01
Extract constant arguments to `PrintStream#println(String)` from a Java class file using the ASM bytecode framework
import org.objectweb.asm.tree.MethodNode;
import java.util.Iterator;
import org.objectweb.asm.tree.analysis.Analyzer;
import java.io.IOException;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.tree.ClassNode;
import java.io.InputStream;
import java.util.List;
import org.objectweb.asm.Type;
@jhorstmann
jhorstmann / jiffybox-android-x86.txt
Created January 8, 2012 10:59
Setup an android-x86 build environment on a JiffyBox cloud server by domainFACTORY
Setup a JiffyBox by following the instructions, using Ubuntu 10.04 as the operating system. I created a CloudLevel 4 server with 16GB swap but it seems a smaller one would also be sufficient.
Login as root and create a normal user with sudo rights, as in a normal Ubuntu installation. Working as root should be really discouraged.
$ adduser jh
$ adduser jh adm sudo
Logout and login as the user you created above. Install a nicer editor, vim in my case.
$ sudo apt-get install vim
@jhorstmann
jhorstmann / RecurseiveParsingWithJsonBuilder.java
Created November 12, 2012 22:41
Getting familiar with the JsonBuilder API of JSR 353
package net.jhorstmann.jsonp;
import java.io.IOException;
import java.io.PushbackReader;
import java.io.StringReader;
import javax.json.JsonArray;
import javax.json.JsonArrayBuilder;
import javax.json.JsonBuilder;
import javax.json.JsonBuilder.JsonBuildable;
import javax.json.JsonObject;
@jhorstmann
jhorstmann / RecurseiveParsingWithCollections.java
Created November 12, 2012 22:43
Using java collections instead of JsonBuilder
package net.jhorstmann.jsonp;
import java.io.IOException;
import java.io.PushbackReader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
package org.modelmapper;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.modelmapper.spi.MappingContext;
import org.modelmapper.spi.MappingEngine;
import org.testng.Assert;
WITH n AS (SELECT 100 AS n)
SELECT COALESCE(fizz || buzz, fizz, buzz, i::text)
FROM n
CROSS JOIN LATERAL generate_series(1, n) AS i
LEFT JOIN LATERAL (SELECT i, 'fizz'::text AS fizz
FROM generate_series(0, n, 3) AS i) fizz USING(i)
LEFT JOIN LATERAL (SELECT i, 'buzz'::text AS buzz
FROM generate_series(0, n, 5) AS i) buzz USING(i);
@jhorstmann
jhorstmann / TestConversions.java
Created October 28, 2016 20:02
Investigating memory usage of exchange rate conversions in moneta
import org.javamoney.moneta.Money;
import javax.money.MonetaryAmount;
import javax.money.convert.CurrencyConversion;
import javax.money.convert.MonetaryConversions;
import java.math.BigDecimal;
public class TestConversions {
public static void main(String[] args) throws InterruptedException {
@jhorstmann
jhorstmann / exporter.patch
Created December 1, 2011 12:53
Patch for Primefaces 3.0.M4 to use native excel formats instead of converting everything to strings
diff --git a/src/main/java/org/primefaces/component/export/CSVExporter.java b/src/main/java/org/primefaces/component/export/CSVExporter.java
index e5e8992..db0e981 100644
--- a/src/main/java/org/primefaces/component/export/CSVExporter.java
+++ b/src/main/java/org/primefaces/component/export/CSVExporter.java
@@ -20,6 +20,7 @@ import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.lang.reflect.Array;
+import java.util.ArrayList;
import java.util.Iterator;
@jhorstmann
jhorstmann / CartesianProduct.java
Last active May 22, 2018 21:02
Lazy Cartesian Product
package org.zuchini.examples.parallel;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.Spliterator;
// https://en.wikipedia.org/wiki/Double_dabble
public class DoubleDabble {
static int bcd(int value, int bits) {
int res = value << (16 - bits);
res <<= 3;
for (int i = 3; i < bits; i++) {
// find bcd digits > 4 and incremented them 3
// either bit 4 is set or bit 3 and at least on of bits 2 or 1 is set
// bit 1 in mask ist set if digit > 4
// to get the add operand or the mask with mask<<1