From 767728e169b4dcf5b07cecf1d3d3ca3ab8589ac5 Mon Sep 17 00:00:00 2001 From: Jonathan Bluett-Duncan Date: Tue, 5 Sep 2017 14:26:12 +0200 Subject: Polishing This is a follow-on to https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!msg/bazel-dev/Q2owiR-e86s/ugrVUhn7AwAJ to introduce more usages of Java 8 idioms and other "cleanups", with the intention of making the code base easier to maintain. Closes #3623. PiperOrigin-RevId: 167566256 --- .../build/lib/collect/nestedset/NestedSet.java | 53 ++++++++++------------ 1 file changed, 23 insertions(+), 30 deletions(-) (limited to 'src/main/java/com/google/devtools/build/lib/collect/nestedset/NestedSet.java') diff --git a/src/main/java/com/google/devtools/build/lib/collect/nestedset/NestedSet.java b/src/main/java/com/google/devtools/build/lib/collect/nestedset/NestedSet.java index 74e01eeeca..522d7e9b2e 100644 --- a/src/main/java/com/google/devtools/build/lib/collect/nestedset/NestedSet.java +++ b/src/main/java/com/google/devtools/build/lib/collect/nestedset/NestedSet.java @@ -26,7 +26,6 @@ import java.util.Iterator; import java.util.List; import java.util.Objects; import java.util.Set; -import java.util.stream.Stream; import javax.annotation.Nullable; /** @@ -111,7 +110,7 @@ public final class NestedSet implements Iterable { children[n++] = a; leaf = false; } else { - if (!alreadyInserted.contains((E) c) && hoisted.add((E) c)) { + if (!alreadyInserted.contains(c) && hoisted.add((E) c)) { children[n++] = c; } } @@ -231,11 +230,9 @@ public final class NestedSet implements Iterable { * different from both standard Java objects and collection-like objects. */ public int shallowHashCode() { - if (isSingleton()) { - return Objects.hash(order, children); - } else { - return Objects.hash(order, Arrays.hashCode((Object[]) children)); - } + return isSingleton() + ? Objects.hash(order, children) + : Objects.hash(order, Arrays.hashCode((Object[]) children)); } @Override @@ -246,20 +243,20 @@ public final class NestedSet implements Iterable { // TODO: this leaves LINK_ORDER backwards private static String childrenToString(Object children) { if (children instanceof Object[]) { - return "{" - + Stream.of((Object[]) children).map(Stringer.INSTANCE).collect(joining(", ")) - + "}"; + return Arrays.stream((Object[]) children) + .map(NestedSet::childrenToString) + .collect(joining(", ", "{", "}")); } else { return children.toString(); } } - private static enum Stringer implements Function { + private enum Stringer implements Function { INSTANCE; @Override public String apply(Object o) { return childrenToString(o); } - }; + } @Override public Iterator iterator() { @@ -275,7 +272,7 @@ public final class NestedSet implements Iterable { private ImmutableList expand() { // This value is only set in the constructor, so safe to test here with no lock. if (memo == LEAF_MEMO) { - return ImmutableList.copyOf(new ArraySharingCollection((Object[]) children)); + return ImmutableList.copyOf(new ArraySharingCollection<>((Object[]) children)); } CompactHashSet members = lockedExpand(); if (members != null) { @@ -338,19 +335,17 @@ public final class NestedSet implements Iterable { */ private int walk(CompactHashSet sets, CompactHashSet members, Object[] children, int pos) { - int n = children.length; - for (int i = 0; i < n; ++i) { - if ((pos>>3) >= memo.length) { + for (Object child : children) { + if ((pos >> 3) >= memo.length) { memo = Arrays.copyOf(memo, memo.length * 2); } - Object c = children[i]; - if (c instanceof Object[]) { - if (sets.add(c)) { + if (child instanceof Object[]) { + if (sets.add(child)) { int prepos = pos; int presize = members.size(); - pos = walk(sets, members, (Object[]) c, pos + 1); + pos = walk(sets, members, (Object[]) child, pos + 1); if (presize < members.size()) { - memo[prepos>>3] |= 1<<(prepos&7); + memo[prepos >> 3] |= 1 << (prepos & 7); } else { // We didn't find any new nodes, so don't mark this branch as taken. // Rewind pos. The rest of the array is still zeros because no one @@ -361,8 +356,8 @@ public final class NestedSet implements Iterable { ++pos; } } else { - if (members.add((E) c)) { - memo[pos>>3] |= 1<<(pos&7); + if (members.add((E) child)) { + memo[pos >> 3] |= 1 << (pos & 7); } ++pos; } @@ -376,14 +371,12 @@ public final class NestedSet implements Iterable { */ private static int replay(ImmutableList.Builder output, Object[] children, byte[] memo, int pos) { - int n = children.length; - for (int i = 0; i < n; ++i) { - Object c = children[i]; - if ((memo[pos>>3] & (1<<(pos&7))) != 0) { - if (c instanceof Object[]) { - pos = replay(output, (Object[]) c, memo, pos + 1); + for (Object child : children) { + if ((memo[pos >> 3] & (1 << (pos & 7))) != 0) { + if (child instanceof Object[]) { + pos = replay(output, (Object[]) child, memo, pos + 1); } else { - output.add((E) c); + output.add((E) child); ++pos; } } else { -- cgit v1.2.3