Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove uses of Ordering and ComparisonChain and just use Comparator. #363

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion jimfs/src/main/java/com/google/common/jimfs/Directory.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public void unlink(Name name) {
*/
public ImmutableSortedSet<Name> snapshot() {
ImmutableSortedSet.Builder<Name> builder =
new ImmutableSortedSet.Builder<>(Name.displayOrdering());
new ImmutableSortedSet.Builder<>(Name.displayComparator());

for (DirectoryEntry entry : this) {
if (!isReserved(entry.name())) {
Expand Down
2 changes: 1 addition & 1 deletion jimfs/src/main/java/com/google/common/jimfs/FileTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ final class FileTree {

/** Creates a new file tree with the given root directories. */
FileTree(Map<Name, Directory> roots) {
this.roots = ImmutableSortedMap.copyOf(roots, Name.canonicalOrdering());
this.roots = ImmutableSortedMap.copyOf(roots, Name.canonicalComparator());
}

/** Returns the names of the root directories in this tree. */
Expand Down
10 changes: 5 additions & 5 deletions jimfs/src/main/java/com/google/common/jimfs/JimfsPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.collect.ComparisonChain;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import java.io.File;
Expand All @@ -37,6 +36,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Deque;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -409,10 +409,10 @@ public int size() {
public int compareTo(Path other) {
// documented to throw CCE if other is associated with a different FileSystemProvider
JimfsPath otherPath = (JimfsPath) other;
return ComparisonChain.start()
.compare(getJimfsFileSystem().getUri(), ((JimfsPath) other).getJimfsFileSystem().getUri())
.compare(this, otherPath, pathService)
.result();
Comparator<JimfsPath> comparator =
Comparator.comparing((JimfsPath p) -> p.getJimfsFileSystem().getUri())
.thenComparing(pathService);
return comparator.compare(this, otherPath);
}

@Override
Expand Down
39 changes: 12 additions & 27 deletions jimfs/src/main/java/com/google/common/jimfs/Name.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import com.google.common.collect.Ordering;
import java.util.Comparator;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
Expand Down Expand Up @@ -95,33 +94,19 @@ public String toString() {
return display;
}

/** Returns an ordering that orders names by their display representation. */
public static Ordering<Name> displayOrdering() {
return DISPLAY_ORDERING;
/** Returns a comparator that orders names by their display representation. */
static Comparator<Name> displayComparator() {
return DISPLAY_COMPARATOR;
}

/** Returns an ordering that orders names by their canonical representation. */
public static Ordering<Name> canonicalOrdering() {
return CANONICAL_ORDERING;
/** Returns a comparator that orders names by their canonical representation. */
static Comparator<Name> canonicalComparator() {
return CANONICAL_COMPARATOR;
}

private static final Ordering<Name> DISPLAY_ORDERING =
Ordering.natural()
.onResultOf(
new Function<Name, String>() {
@Override
public String apply(Name name) {
return name.display;
}
});

private static final Ordering<Name> CANONICAL_ORDERING =
Ordering.natural()
.onResultOf(
new Function<Name, String>() {
@Override
public String apply(Name name) {
return name.canonical;
}
});
private static final Comparator<Name> DISPLAY_COMPARATOR =
Comparator.comparing((Name n) -> n.display);

private static final Comparator<Name> CANONICAL_COMPARATOR =
Comparator.comparing((Name n) -> n.canonical);
}
40 changes: 21 additions & 19 deletions jimfs/src/main/java/com/google/common/jimfs/PathService.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static java.nio.file.LinkOption.NOFOLLOW_LINKS;
import static java.util.Comparator.nullsLast;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Functions;
import com.google.common.base.Predicate;
import com.google.common.collect.ComparisonChain;
import com.google.common.collect.Comparators;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Ordering;
import com.google.common.jimfs.PathType.ParseResult;
import java.net.URI;
import java.nio.file.FileSystem;
Expand All @@ -47,23 +47,24 @@
*/
final class PathService implements Comparator<JimfsPath> {

private static final Ordering<Name> DISPLAY_ROOT_ORDERING = Name.displayOrdering().nullsLast();
private static final Ordering<Iterable<Name>> DISPLAY_NAMES_ORDERING =
Name.displayOrdering().lexicographical();
private static final Comparator<Name> DISPLAY_ROOT_COMPARATOR =
nullsLast(Name.displayComparator());
private static final Comparator<Iterable<Name>> DISPLAY_NAMES_COMPARATOR =
Comparators.lexicographical(Name.displayComparator());

private static final Ordering<Name> CANONICAL_ROOT_ORDERING =
Name.canonicalOrdering().nullsLast();
private static final Ordering<Iterable<Name>> CANONICAL_NAMES_ORDERING =
Name.canonicalOrdering().lexicographical();
private static final Comparator<Name> CANONICAL_ROOT_COMPARATOR =
nullsLast(Name.canonicalComparator());
private static final Comparator<Iterable<Name>> CANONICAL_NAMES_COMPARATOR =
Comparators.lexicographical(Name.canonicalComparator());

private final PathType type;

private final ImmutableSet<PathNormalization> displayNormalizations;
private final ImmutableSet<PathNormalization> canonicalNormalizations;
private final boolean equalityUsesCanonicalForm;

private final Ordering<Name> rootOrdering;
private final Ordering<Iterable<Name>> namesOrdering;
private final Comparator<Name> rootComparator;
private final Comparator<Iterable<Name>> namesComparator;

private volatile FileSystem fileSystem;
private volatile JimfsPath emptyPath;
Expand All @@ -86,9 +87,10 @@ final class PathService implements Comparator<JimfsPath> {
this.canonicalNormalizations = ImmutableSet.copyOf(canonicalNormalizations);
this.equalityUsesCanonicalForm = equalityUsesCanonicalForm;

this.rootOrdering = equalityUsesCanonicalForm ? CANONICAL_ROOT_ORDERING : DISPLAY_ROOT_ORDERING;
this.namesOrdering =
equalityUsesCanonicalForm ? CANONICAL_NAMES_ORDERING : DISPLAY_NAMES_ORDERING;
this.rootComparator =
equalityUsesCanonicalForm ? CANONICAL_ROOT_COMPARATOR : DISPLAY_ROOT_COMPARATOR;
this.namesComparator =
equalityUsesCanonicalForm ? CANONICAL_NAMES_COMPARATOR : DISPLAY_NAMES_COMPARATOR;
}

/** Sets the file system to use for created paths. */
Expand Down Expand Up @@ -200,7 +202,7 @@ public String toString(JimfsPath path) {
/** Creates a hash code for the given path. */
public int hash(JimfsPath path) {
// Note: JimfsPath.equals() is implemented using the compare() method below;
// equalityUsesCanonicalForm is taken into account there via the namesOrdering, which is set
// equalityUsesCanonicalForm is taken into account there via the namesComparator, which is set
// at construction time.
int hash = 31;
hash = 31 * hash + getFileSystem().hashCode();
Expand All @@ -226,10 +228,10 @@ public int hash(JimfsPath path) {

@Override
public int compare(JimfsPath a, JimfsPath b) {
return ComparisonChain.start()
.compare(a.root(), b.root(), rootOrdering)
.compare(a.names(), b.names(), namesOrdering)
.result();
Comparator<JimfsPath> comparator =
Comparator.comparing(JimfsPath::root, rootComparator)
.thenComparing(JimfsPath::names, namesComparator);
return comparator.compare(a, b);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Iterators;
import com.google.common.collect.Ordering;
import com.google.common.io.ByteStreams;
import com.google.common.io.CharStreams;
import com.google.common.primitives.Bytes;
Expand Down Expand Up @@ -87,7 +87,6 @@
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.nio.file.attribute.UserPrincipal;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Set;
import java.util.regex.PatternSyntaxException;
Expand Down Expand Up @@ -188,8 +187,7 @@ public void testPaths_areSortedCaseSensitive() {
Path p3 = path("c");
Path p4 = path("D");

assertThat(Ordering.natural().immutableSortedCopy(Arrays.asList(p3, p4, p1, p2)))
.isEqualTo(ImmutableList.of(p2, p4, p1, p3));
assertThat(ImmutableSortedSet.of(p3, p4, p1, p2)).containsExactly(p2, p4, p1, p3).inOrder();

// would be p1, p2, p3, p4 if sorting were case insensitive
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,15 @@
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import static org.junit.Assert.fail;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Ordering;
import com.google.common.collect.ImmutableSortedSet;
import java.io.IOException;
import java.net.URI;
import java.nio.file.FileSystem;
import java.nio.file.FileSystemException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.regex.PatternSyntaxException;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -93,8 +91,7 @@ public void testPaths_areSortedCaseInsensitive() {
Path p3 = path("c");
Path p4 = path("D");

assertThat(Ordering.natural().immutableSortedCopy(Arrays.asList(p3, p4, p1, p2)))
.isEqualTo(ImmutableList.of(p1, p2, p3, p4));
assertThat(ImmutableSortedSet.of(p3, p4, p1, p2)).containsExactly(p1, p2, p3, p4).inOrder();

// would be p2, p4, p1, p3 if sorting were case sensitive
}
Expand Down
Loading