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 | |
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')
5 files changed, 38 insertions, 53 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 { diff --git a/src/main/java/com/google/devtools/build/lib/collect/nestedset/NestedSetBuilder.java b/src/main/java/com/google/devtools/build/lib/collect/nestedset/NestedSetBuilder.java index 633572f7fb..0a96139a1b 100644 --- a/src/main/java/com/google/devtools/build/lib/collect/nestedset/NestedSetBuilder.java +++ b/src/main/java/com/google/devtools/build/lib/collect/nestedset/NestedSetBuilder.java @@ -148,7 +148,7 @@ public final class NestedSetBuilder<E> { return candidate; } } - return new NestedSet<E>(order, items, transitiveSetsCast); + return new NestedSet<>(order, items, transitiveSetsCast); } private static final ConcurrentMap<ImmutableList<?>, NestedSet<?>> immutableListCache = @@ -234,9 +234,7 @@ public final class NestedSetBuilder<E> { return stableOrder(); } NestedSetBuilder<E> result = new NestedSetBuilder<>(firstSet.getOrder()); - for (NestedSet<E> set : sets) { - result.addTransitive(set); - } + sets.forEach(result::addTransitive); return result; } } diff --git a/src/main/java/com/google/devtools/build/lib/collect/nestedset/NestedSetView.java b/src/main/java/com/google/devtools/build/lib/collect/nestedset/NestedSetView.java index 3defa64d44..b1f778b3fe 100644 --- a/src/main/java/com/google/devtools/build/lib/collect/nestedset/NestedSetView.java +++ b/src/main/java/com/google/devtools/build/lib/collect/nestedset/NestedSetView.java @@ -13,7 +13,10 @@ // limitations under the License. package com.google.devtools.build.lib.collect.nestedset; +import static com.google.common.collect.ImmutableSet.toImmutableSet; + import com.google.common.collect.ImmutableSet; +import java.util.Arrays; import java.util.Set; /** @@ -66,15 +69,12 @@ public class NestedSetView<E> { */ public Set<NestedSetView<E>> transitives() { if (!(set instanceof Object[])) { - return ImmutableSet.<NestedSetView<E>>of(); - } - ImmutableSet.Builder<NestedSetView<E>> transitives = new ImmutableSet.Builder<>(); - for (Object c : (Object[]) set) { - if (c instanceof Object[]) { - transitives.add(new NestedSetView<E>(c)); - } + return ImmutableSet.of(); } - return transitives.build(); + return Arrays.stream((Object[]) set) + .filter(c -> c instanceof Object[]) + .map(c -> new NestedSetView<E>(c)) + .collect(toImmutableSet()); } /** @@ -86,14 +86,11 @@ public class NestedSetView<E> { @SuppressWarnings("unchecked") public Set<E> directs() { if (!(set instanceof Object[])) { - return ImmutableSet.<E>of((E) set); - } - ImmutableSet.Builder<E> children = new ImmutableSet.Builder<E>(); - for (Object c : (Object[]) set) { - if (!(c instanceof Object[])) { - children.add((E) c); - } + return ImmutableSet.of((E) set); } - return children.build(); + return Arrays.stream((Object[]) set) + .filter(c -> !(c instanceof Object[])) + .map(c -> (E) c) + .collect(toImmutableSet()); } } diff --git a/src/main/java/com/google/devtools/build/lib/collect/nestedset/NestedSetVisitor.java b/src/main/java/com/google/devtools/build/lib/collect/nestedset/NestedSetVisitor.java index 686991cfab..9ced74139b 100644 --- a/src/main/java/com/google/devtools/build/lib/collect/nestedset/NestedSetVisitor.java +++ b/src/main/java/com/google/devtools/build/lib/collect/nestedset/NestedSetVisitor.java @@ -15,7 +15,6 @@ package com.google.devtools.build.lib.collect.nestedset; import com.google.common.collect.Sets; import com.google.devtools.build.lib.util.Preconditions; - import java.util.Set; /** @@ -54,7 +53,6 @@ public final class NestedSetVisitor<E> { * Transitively visit a nested set. * * @param nestedSet the nested set to visit transitively. - * */ public void visit(NestedSet<E> nestedSet) { Preconditions.checkArgument(nestedSet.getOrder() == Order.STABLE_ORDER); diff --git a/src/main/java/com/google/devtools/build/lib/collect/nestedset/Order.java b/src/main/java/com/google/devtools/build/lib/collect/nestedset/Order.java index d4ae2d6054..05cc7a1b1a 100644 --- a/src/main/java/com/google/devtools/build/lib/collect/nestedset/Order.java +++ b/src/main/java/com/google/devtools/build/lib/collect/nestedset/Order.java @@ -15,7 +15,6 @@ package com.google.devtools.build.lib.collect.nestedset; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Maps; - import java.util.HashMap; /** |