diff options
author | Jonathan Bluett-Duncan <jbluettduncan@gmail.com> | 2017-09-05 14:26:12 +0200 |
---|---|---|
committer | Yun Peng <pcloudy@google.com> | 2017-09-05 14:58:40 +0200 |
commit | 767728e169b4dcf5b07cecf1d3d3ca3ab8589ac5 (patch) | |
tree | ac66d34ff3aa1bb63ab8b19033f069de1baee866 /src/main/java/com/google/devtools/build/lib/collect/nestedset/NestedSet.java | |
parent | ebf335cec37df673cf497038c13a1cec426c9d56 (diff) |
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
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/collect/nestedset/NestedSet.java')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/collect/nestedset/NestedSet.java | 53 |
1 files changed, 23 insertions, 30 deletions
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<E> implements Iterable<E> { 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<E> implements Iterable<E> { * 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<E> implements Iterable<E> { // 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<Object, String> { + private enum Stringer implements Function<Object, String> { INSTANCE; @Override public String apply(Object o) { return childrenToString(o); } - }; + } @Override public Iterator<E> iterator() { @@ -275,7 +272,7 @@ public final class NestedSet<E> implements Iterable<E> { private ImmutableList<E> expand() { // This value is only set in the constructor, so safe to test here with no lock. if (memo == LEAF_MEMO) { - return ImmutableList.<E>copyOf(new ArraySharingCollection<E>((Object[]) children)); + return ImmutableList.copyOf(new ArraySharingCollection<>((Object[]) children)); } CompactHashSet<E> members = lockedExpand(); if (members != null) { @@ -338,19 +335,17 @@ public final class NestedSet<E> implements Iterable<E> { */ private int walk(CompactHashSet<Object> sets, CompactHashSet<E> 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<E> implements Iterable<E> { ++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<E> implements Iterable<E> { */ private static <E> int replay(ImmutableList.Builder<E> 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 { |