Skip to content

Commit

Permalink
Split comparator into it's own class.
Browse files Browse the repository at this point in the history
RELNOTES=n/a
PiperOrigin-RevId: 532480406
  • Loading branch information
MarkCMann authored and Google Java Core Libraries committed May 16, 2023
1 parent 4aaebca commit 9f476d8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -253,17 +253,25 @@ static void fail(Throwable cause, Object message) {
throw assertionFailedError;
}

public static <K, V> Comparator<Entry<K, V>> entryComparator(
@Nullable Comparator<? super K> keyComparator) {
return new Comparator<Entry<K, V>>() {
@Override
@SuppressWarnings("unchecked") // no less safe than putting it in the map!
public int compare(Entry<K, V> a, Entry<K, V> b) {
private static class EntryComparator<K, V> implements Comparator<Entry<K, V>> {
final @Nullable Comparator<? super K> keyComparator;

public EntryComparator(@Nullable Comparator<? super K> keyComparator) {
this.keyComparator = keyComparator;
}

@Override
@SuppressWarnings("unchecked") // no less safe than putting it in the map!
public int compare(Entry<K, V> a, Entry<K, V> b) {
return (keyComparator == null)
? ((Comparable) a.getKey()).compareTo(b.getKey())
: keyComparator.compare(a.getKey(), b.getKey());
}
};
}
}

public static <K, V> Comparator<Entry<K, V>> entryComparator(
@Nullable Comparator<? super K> keyComparator) {
return new EntryComparator<K, V>(keyComparator);
}

/**
Expand Down
24 changes: 16 additions & 8 deletions guava-testlib/src/com/google/common/collect/testing/Helpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -253,17 +253,25 @@ static void fail(Throwable cause, Object message) {
throw assertionFailedError;
}

public static <K, V> Comparator<Entry<K, V>> entryComparator(
@Nullable Comparator<? super K> keyComparator) {
return new Comparator<Entry<K, V>>() {
@Override
@SuppressWarnings("unchecked") // no less safe than putting it in the map!
public int compare(Entry<K, V> a, Entry<K, V> b) {
private static class EntryComparator<K, V> implements Comparator<Entry<K, V>> {
final @Nullable Comparator<? super K> keyComparator;

public EntryComparator(@Nullable Comparator<? super K> keyComparator) {
this.keyComparator = keyComparator;
}

@Override
@SuppressWarnings("unchecked") // no less safe than putting it in the map!
public int compare(Entry<K, V> a, Entry<K, V> b) {
return (keyComparator == null)
? ((Comparable) a.getKey()).compareTo(b.getKey())
: keyComparator.compare(a.getKey(), b.getKey());
}
};
}
}

public static <K, V> Comparator<Entry<K, V>> entryComparator(
@Nullable Comparator<? super K> keyComparator) {
return new EntryComparator<K, V>(keyComparator);
}

/**
Expand Down

0 comments on commit 9f476d8

Please sign in to comment.