Created
August 21, 2015 09:28
-
-
Save michaelamaura/aa0d4502c45cbc15a316 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
package com.rwe.st.cao.gas.attila.util; | |
import java.util.Comparator; | |
import java.util.Optional; | |
/** | |
* Comparator utils for {@see Optional} types. | |
*/ | |
public final class OptionalComparator { | |
private OptionalComparator() { | |
} | |
public static <T extends Comparable<T>> Comparator<Optional<T>> absentFirst() { | |
return absentFirst(Comparator.<T>naturalOrder()); | |
} | |
public static <T extends Comparable<T>> Comparator<Optional<T>> absentFirst(Comparator<T> comparator) { | |
return Comparator.comparing(x -> x.orElse(null), Comparator.nullsFirst(comparator)); | |
} | |
public static <T extends Comparable<T>> Comparator<Optional<T>> absentLast() { | |
return absentLast(Comparator.<T>naturalOrder()); | |
} | |
public static <T extends Comparable<T>> Comparator<Optional<T>> absentLast(Comparator<T> comparator) { | |
return Comparator.comparing(x -> x.orElse(null), Comparator.nullsLast(comparator)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment