Skip to content

Instantly share code, notes, and snippets.

@nurkiewicz
Last active December 14, 2015 00:09
Show Gist options
  • Save nurkiewicz/4997229 to your computer and use it in GitHub Desktop.
Save nurkiewicz/4997229 to your computer and use it in GitHub Desktop.
import java.util.HashSet;
import java.util.Set;
public class MagicalLand {
public static void main(String[] args) {
for (int i = 0; i < (Math.random() * 500) + 2; i++) {
if (Unicorn.pat()) {
System.out.println("UNICORN #1: PAT THIS UNICORN ONCE");
}
}
for (int i = 0; i < (Math.random() * 500) + 2; i++) {
if (Unicorn.pat()) {
System.out.println("UNICORN #2: PAT THIS UNICORN ONCE");
}
}
System.out.println("END OF PROGRAM");
}
}
class Unicorn {
private static Set<Integer> sourceLinesVisited = new HashSet<>();
public static boolean pat() {
final int callerLineNumber = Thread.currentThread().getStackTrace()[2].getLineNumber();
return sourceLinesVisited.add(callerLineNumber);
}
}
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.HashSet;
import java.util.Set;
public class MagicalLand {
public static void main(String[] args) {
for (int i = 0; i < (Math.random() * 500) + 2; i++) {
if (Unicorn.pat()) {
System.out.println("UNICORN #1: PAT THIS UNICORN ONCE");
}
}
for (int i = 0; i < (Math.random() * 500) + 2; i++) {
if (Unicorn.pat()) {
System.out.println("UNICORN #2: PAT THIS UNICORN ONCE");
}
}
System.out.println("END OF PROGRAM");
}
}
class Unicorn {
static {
System.setOut(new NoDuplicateOutputStream(System.out));
}
public static boolean pat() {
return true;
}
}
class NoDuplicateOutputStream extends PrintStream {
private final Set<String> printedMessages = new HashSet<>();
public NoDuplicateOutputStream(OutputStream out) {
super(out);
}
@Override
public void println(String x) {
if (printedMessages.add(x)) {
super.println(x);
}
}
}
public class MagicalLand {
public static void main(String[] args) {
for (int i = 0; i < (Math.random() * 500) + 2; i++) {
if (Unicorn.pat()) {
System.out.println("UNICORN #1: PAT THIS UNICORN ONCE");
}
}
for (int i = 0; i < (Math.random() * 500) + 2; i++) {
if (Unicorn.pat()) {
System.out.println("UNICORN #2: PAT THIS UNICORN ONCE");
}
}
System.out.println("END OF PROGRAM");
}
}
class Unicorn {
public static boolean pat() {
return true;
}
}
class Math {
public static double random() {
return -1.0 / 500;
}
}
public class MagicalLand {
public static void main(String[] args) {
for (int i = 0; i < (Math.random() * 500) + 2; i++) {
if (Unicorn.pat()) {
System.out.println("UNICORN #1: PAT THIS UNICORN ONCE");
}
}
for (int i = 0; i < (Math.random() * 500) + 2; i++) {
if (Unicorn.pat()) {
System.out.println("UNICORN #2: PAT THIS UNICORN ONCE");
}
}
System.out.println("END OF PROGRAM");
}
}
class Unicorn {
static {
System.out.println("UNICORN #1: PAT THIS UNICORN ONCE");
System.out.println("UNICORN #2: PAT THIS UNICORN ONCE");
}
public static boolean pat() {
return false;
}
}
import java.util.concurrent.atomic.AtomicBoolean;
public class MagicalLand {
public static void main(String[] args) {
for (int i = 0; i < (Math.random() * 500) + 2; i++) {
if (Unicorn.pat()) {
System.out.println("UNICORN #1: PAT THIS UNICORN ONCE");
}
}
for (int i = 0; i < (Math.random() * 500) + 2; i++) {
if (Unicorn.pat()) {
System.out.println("UNICORN #2: PAT THIS UNICORN ONCE");
}
}
System.out.println("END OF PROGRAM");
}
}
class Unicorn {
private static AtomicBoolean printed = new AtomicBoolean();
public static boolean pat() {
if (!printed.getAndSet(true)) {
System.out.println("UNICORN #1: PAT THIS UNICORN ONCE");
System.out.println("UNICORN #2: PAT THIS UNICORN ONCE");
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment