Skip to content

Commit

Permalink
Fixed min()/max() behavior regarding null values
Browse files Browse the repository at this point in the history
  • Loading branch information
danieldietrich committed Nov 19, 2016
1 parent 7db4c34 commit 5326c88
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions javaslang/src/main/java/javaslang/collection/Traversable.java
Original file line number Diff line number Diff line change
Expand Up @@ -619,15 +619,16 @@ default Option<T> lastOption() {
<U> Traversable<U> map(Function<? super T, ? extends U> mapper);

/**
* Calculates the maximum of this elements according to the underlying element {@code Comparator} if exists,
* or the natural order of elements.
* Calculates the maximum of this elements according to their natural order.
*
* @return {@code Some(maximum)} of this elements or {@code None} if this is empty or no {@code Comparator} is applicable
* @return {@code Some(maximum)} of this elements or {@code None} if this is empty
* @throws NullPointerException if an element is null
* @throws ClassCastException if the elements do not have a natural order, i.e. they do not implement Comparable
*/
@SuppressWarnings("unchecked")
default Option<T> max() {
final Traversable<T> ts = isTraversableAgain() ? this : toStream();
if (isEmpty() || !(ts.head() instanceof Comparable)) {
if (isEmpty()) {
return Option.none();
} else {
return ts.maxBy((o1, o2) -> ((Comparable<T>) o1).compareTo(o2));
Expand Down Expand Up @@ -680,15 +681,16 @@ default <U extends Comparable<? super U>> Option<T> maxBy(Function<? super T, ?
}

/**
* Calculates the minimum of this elements according to the underlying element {@code Comparator} if exists,
* or the natural order of elements.
* Calculates the minimum of this elements according to their natural order.
*
* @return {@code Some(minimum)} of this elements or {@code None} if this is empty or no {@code Comparator} is applicable
* @return {@code Some(minimum)} of this elements or {@code None} if this is empty
* @throws NullPointerException if an element is null
* @throws ClassCastException if the elements do not have a natural order, i.e. they do not implement Comparable
*/
@SuppressWarnings("unchecked")
default Option<T> min() {
final Traversable<T> ts = isTraversableAgain() ? this : toStream();
if (isEmpty() || !(ts.head() instanceof Comparable)) {
if (isEmpty()) {
return Option.none();
} else {
return ts.minBy((o1, o2) -> ((Comparable<T>) o1).compareTo(o2));
Expand Down

0 comments on commit 5326c88

Please sign in to comment.