Skip to content

Instantly share code, notes, and snippets.

@kabutz
kabutz / gist:13ee4e22fc796a469d06
Created May 5, 2014 08:03
How to validate a Greek Tax Number (AFM)
// From http://www.gsis.gr/gsis/info/gsis_site/Services/Polites/sources.html
// gr.gsis.e7.E7File class
// Apache 2.0 license
public static boolean afmIsValid(String afm) {
int sum = 0;
if (afm.length() != 9)
return false;
for (int i = 0; i < 8; i++)
import java.util.*;
public class JavaChampionTest {
public static void main(String... args) {
Map<JavaChampion, String> urls = new HashMap<>();
urls.put(new JavaChampion("Jack"), "fasterj.com");
urls.put(new JavaChampion("Kirk"), "kodewerk.com");
urls.put(new JavaChampion("Heinz"), "javaspecialists.eu");
urls.forEach((p,u) -> System.out.println(u)); // Java 8
import java.lang.reflect.*;
public class SillyPuzzle {
public static void main(String... args) {
prepareForSillyPuzzle();
show(6 + 4);
show(9 + 2);
show(8 + 5);
show(5 + 2);
show(9 + 8);
@kabutz
kabutz / LuckyDraw.java
Created February 1, 2016 07:27
LuckyDraw used to decide who gets a place at JCrete® 2016
import java.io.*;
import java.nio.file.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.stream.*;
public class LuckyDraw {
public static void main(String... args) throws IOException {
List<Entry> list = Files.lines(Paths.get("luckydraw.csv"))
.map(Entry::new)
import java.math.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
/**
* Seems to cause livelock in Java 8 ConcurrentHashMap transfer()
* method and ConcurrentModificationException in Java 9 HashMap.
*/
public class WeirdComputeIfAbsentBehaviour {
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadMXBean;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
@kabutz
kabutz / Fib.txt
Last active October 26, 2016 15:22
A simple exponential time shootout between C, Java and Swift
/*
Algorithm is exponential - very bad. It is just meant to exercise the CPU a bit and
to see what happens with different languages. Not representative of real code.
Interesting that Java beats C, thanks to HotSpot. I expected Swift to be as fast as C
at least. I also expected the compiled Swift to be faster than when run as a script.
A bit disappointed, but more time needed to see where the bottlenecks are. Plus I
need to run some real code for benchmarking.
Update: swiftc -O improved the speed quite a bit to be about the same speed as Java
@kabutz
kabutz / virtualproxy.swift
Created October 12, 2016 15:49
Example of how easily we can build a "virtual proxy" in Swift 3.0
/*
All it takes is the "lazy" keyword in front of the var definition and the object is instantiated when it is first used. Brilliant.
*/
class MoralFibre {
init() {
print("***Creating MoralFibre***")
}
func actSociallyResponsibly() -> Int {
print("supporting local soup kitchen")
@kabutz
kabutz / HardPlace.java
Last active October 26, 2016 18:23
It is possible to compile this code with JDK 8, but not with JDK 9. What can happen when you run it? Vote here: https://twitter.com/heinzkabutz/status/791220763059781632
abstract class Brick {
public Brick() {
greet();
}
public abstract void greet();
}
public class HardPlace {
public class Between extends Brick {
@kabutz
kabutz / One.java
Created November 4, 2016 20:55
Strange class that compiled with JDK 9, but not 1.6, 1.7 nor 1.8.
/**
* As seen on https://javaspecialists.slack.com/messages/general
*
* This class fails to compile with JDK 1.6, 1.7 and 1.8:
*
* class Two extends Observable { // Nothing special about the choice of Observable here
* ^
* symbol: class Observable
* 1 error
*