Last active
August 29, 2015 14:07
-
-
Save autermann/44a26a76d390cc5ad128 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Clown {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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