Skip to content

Instantly share code, notes, and snippets.

@autermann
Last active August 29, 2015 14:07
Show Gist options
  • Save autermann/44a26a76d390cc5ad128 to your computer and use it in GitHub Desktop.
Save autermann/44a26a76d390cc5ad128 to your computer and use it in GitHub Desktop.
public class Clown {}
// run as 'java -Djava.security.manager Main'
public class Main {
private static class RecursiveClown extends Clown {
private final Volkswagen vw;
private final int amount;
public RecursiveClown(int amount, Volkswagen vw) {
this.vw = vw;
this.amount = amount;
}
@Override
public int hashCode() {
if (amount > 1) {
vw.add(new RecursiveClown(amount - 1, vw));
}
return super.hashCode();
}
}
public static void main(String[] args) {
Volkswagen vw = new Volkswagen();
vw.add(new RecursiveClown(20, vw)).done();
}
}
import java.util.HashSet;
import java.util.Set;
public final class Volkswagen {
private static final int CAPACITY = 5;
private final Set<Clown> clowns = new HashSet<>();
public Volkswagen add(Clown clown) {
synchronized(this) {
if (clowns.size() >= CAPACITY) {
throw new IllegalStateException("I'm full");
} else {
clowns.add(clown);
}
return this;
}
}
public Volkswagen done() {
synchronized(this) {
if (clowns.size() == 20) {
System.out.println("I'm a Volkswagen with 20 clowns!");
} else {
System.out.println("Way to much space in here!");
}
return this;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment