Created
September 4, 2012 17:42
-
-
Save abreslav/3623991 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
interface Comparator<T> { | |
boolean lt(T left, T right); | |
} | |
static Object NULL = new Object(); | |
static class LtInf implements Comparator<Object> { | |
@Override | |
public boolean lt(Object left, Object right) { | |
if (left != NULL) { | |
if (right != NULL) { | |
return ((Integer) left) < ((Integer) right); // These casts are provably safe in Kotlin | |
} else | |
return true; | |
} else | |
return false; | |
} | |
} | |
static <T> Object max(Iterable<? extends T> ts, Comparator<? super T> lt) { | |
Object max = NULL; | |
for (T t : ts) { | |
Object m = max; | |
if (m != NULL) { | |
if (lt.lt((T) m, t)) // This cast is provably safe in Kotlin | |
max = t; | |
} else { | |
max = t; | |
} | |
} | |
return max; | |
} | |
public static void main(String[] args) { | |
Iterable<Object> ts = Lists.newArrayList(1, NULL, 5); | |
System.out.println(max(ts, new LtInf())); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment