Skip to content

Commit

Permalink
Add emptiesFirst/Last to Comparators for easily comparing java.util.O…
Browse files Browse the repository at this point in the history
…ptional

values.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=146801689
  • Loading branch information
kevinb authored and cpovirk committed Feb 7, 2017
1 parent 994a379 commit a1a83d4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
11 changes: 11 additions & 0 deletions guava-tests/test/com/google/common/collect/ComparatorsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
package com.google.common.collect;

import static java.util.Arrays.asList;
import static java.util.Comparator.comparing;

import com.google.common.collect.testing.Helpers;
import com.google.common.testing.EqualsTester;
import java.util.Collections;
import java.util.Comparator;
import java.util.Optional;
import junit.framework.TestCase;

/**
Expand Down Expand Up @@ -69,4 +71,13 @@ public void testIsInStrictOrder() {
assertTrue(Comparators.isInStrictOrder(Collections.singleton(1), Ordering.natural()));
assertTrue(Comparators.isInStrictOrder(Collections.<Integer>emptyList(), Ordering.natural()));
}

public void testEmptiesFirst() {
Optional<String> empty = Optional.empty();
Optional<String> abc = Optional.of("abc");
Optional<String> z = Optional.of("z");

Comparator<Optional<String>> comparator = Comparators.emptiesFirst(comparing(String::length));
Helpers.testComparator(comparator, empty, z, abc);
}
}
27 changes: 27 additions & 0 deletions guava/src/com/google/common/collect/Comparators.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.google.common.annotations.GwtCompatible;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Optional;

/**
* Provides static methods for working with {@link Comparator} instances. For many other helpful
Expand Down Expand Up @@ -104,4 +105,30 @@ public static <T> boolean isInStrictOrder(
}
return true;
}

/**
* Returns a comparator of {@link Optional} values which treats {@link Optional#empty} as less
* than all other values, and orders the rest using {@code valueComparator} on the contained
* value.
*
* @since 22.0
*/
@Beta
public static <T> Comparator<Optional<T>> emptiesFirst(Comparator<T> valueComparator) {
checkNotNull(valueComparator);
return Comparator.comparing(o -> o.orElse(null), Comparator.nullsFirst(valueComparator));
}

/**
* Returns a comparator of {@link Optional} values which treats {@link Optional#empty} as greater
* than all other values, and orders the rest using {@code valueComparator} on the contained
* value.
*
* @since 22.0
*/
@Beta
public static <T> Comparator<Optional<T>> emptiesLast(Comparator<T> valueComparator) {
checkNotNull(valueComparator);
return Comparator.comparing(o -> o.orElse(null), Comparator.nullsLast(valueComparator));
}
}

0 comments on commit a1a83d4

Please sign in to comment.