Skip to content

Instantly share code, notes, and snippets.

@abreslav
Created September 4, 2012 17:42
Show Gist options
  • Save abreslav/3623991 to your computer and use it in GitHub Desktop.
Save abreslav/3623991 to your computer and use it in GitHub Desktop.
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