aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools
diff options
context:
space:
mode:
authorGravatar Jonathan Bluett-Duncan <jbluettduncan@gmail.com>2017-09-05 14:26:12 +0200
committerGravatar Yun Peng <pcloudy@google.com>2017-09-05 14:58:40 +0200
commit767728e169b4dcf5b07cecf1d3d3ca3ab8589ac5 (patch)
treeac66d34ff3aa1bb63ab8b19033f069de1baee866 /src/main/java/com/google/devtools
parentebf335cec37df673cf497038c13a1cec426c9d56 (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')
-rw-r--r--src/main/java/com/google/devtools/build/lib/collect/CollectionUtils.java43
-rw-r--r--src/main/java/com/google/devtools/build/lib/collect/CompactHashSet.java6
-rw-r--r--src/main/java/com/google/devtools/build/lib/collect/EquivalenceRelation.java67
-rw-r--r--src/main/java/com/google/devtools/build/lib/collect/ImmutableSortedKeyListMultimap.java32
-rw-r--r--src/main/java/com/google/devtools/build/lib/collect/ImmutableSortedKeyMap.java49
-rw-r--r--src/main/java/com/google/devtools/build/lib/collect/IterablesChain.java4
-rw-r--r--src/main/java/com/google/devtools/build/lib/collect/nestedset/NestedSet.java53
-rw-r--r--src/main/java/com/google/devtools/build/lib/collect/nestedset/NestedSetBuilder.java6
-rw-r--r--src/main/java/com/google/devtools/build/lib/collect/nestedset/NestedSetView.java29
-rw-r--r--src/main/java/com/google/devtools/build/lib/collect/nestedset/NestedSetVisitor.java2
-rw-r--r--src/main/java/com/google/devtools/build/lib/collect/nestedset/Order.java1
11 files changed, 103 insertions, 189 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/collect/CollectionUtils.java b/src/main/java/com/google/devtools/build/lib/collect/CollectionUtils.java
index 341e750b87..dc33350d6e 100644
--- a/src/main/java/com/google/devtools/build/lib/collect/CollectionUtils.java
+++ b/src/main/java/com/google/devtools/build/lib/collect/CollectionUtils.java
@@ -13,14 +13,17 @@
// limitations under the License.
package com.google.devtools.build.lib.collect;
-import static com.google.common.collect.Sets.newEnumSet;
+import static com.google.common.collect.ImmutableList.toImmutableList;
+import static java.util.stream.Collectors.toCollection;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Maps;
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.util.Preconditions;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.EnumSet;
@@ -28,6 +31,7 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
+import java.util.Objects;
import java.util.Set;
/**
@@ -106,13 +110,7 @@ public final class CollectionUtils {
*/
@SuppressWarnings("unchecked")
public static <T> ImmutableList<T> asListWithoutNulls(T... elements) {
- ImmutableList.Builder<T> builder = ImmutableList.builder();
- for (T element : elements) {
- if (element != null) {
- builder.add(element);
- }
- }
- return builder.build();
+ return Arrays.stream(elements).filter(Objects::nonNull).collect(toImmutableList());
}
/**
@@ -139,11 +137,7 @@ public final class CollectionUtils {
* Given an iterable, returns an immutable iterable with the same contents.
*/
public static <T> Iterable<T> makeImmutable(Iterable<T> iterable) {
- if (isImmutable(iterable)) {
- return iterable;
- } else {
- return ImmutableList.copyOf(iterable);
- }
+ return isImmutable(iterable) ? iterable : ImmutableList.copyOf(iterable);
}
/**
@@ -178,14 +172,9 @@ public final class CollectionUtils {
public static <T extends Enum<T>> EnumSet<T> fromBits(int value, Class<T> clazz) {
T[] elements = clazz.getEnumConstants();
Preconditions.checkArgument(elements.length <= 32);
- ArrayList<T> result = new ArrayList<>();
- for (T element : elements) {
- if ((value & (1 << element.ordinal())) != 0) {
- result.add(element);
- }
- }
-
- return newEnumSet(result, clazz);
+ return Arrays.stream(elements)
+ .filter(element -> (value & (1 << element.ordinal())) != 0)
+ .collect(toCollection(() -> EnumSet.noneOf(clazz)));
}
/**
@@ -200,11 +189,7 @@ public final class CollectionUtils {
*/
public static <KEY_1, KEY_2, VALUE> ImmutableMap<KEY_1, ImmutableMap<KEY_2, VALUE>> toImmutable(
Map<KEY_1, Map<KEY_2, VALUE>> map) {
- ImmutableMap.Builder<KEY_1, ImmutableMap<KEY_2, VALUE>> builder = ImmutableMap.builder();
- for (Map.Entry<KEY_1, Map<KEY_2, VALUE>> entry : map.entrySet()) {
- builder.put(entry.getKey(), ImmutableMap.copyOf(entry.getValue()));
- }
- return builder.build();
+ return ImmutableMap.copyOf(Maps.transformValues(map, ImmutableMap::copyOf));
}
/**
@@ -212,10 +197,6 @@ public final class CollectionUtils {
*/
public static <KEY_1, KEY_2, VALUE> Map<KEY_1, Map<KEY_2, VALUE>> copyOf(
Map<KEY_1, ? extends Map<KEY_2, VALUE>> map) {
- Map<KEY_1, Map<KEY_2, VALUE>> result = new HashMap<>();
- for (Map.Entry<KEY_1, ? extends Map<KEY_2, VALUE>> entry : map.entrySet()) {
- result.put(entry.getKey(), new HashMap<>(entry.getValue()));
- }
- return result;
+ return new HashMap<>(Maps.transformValues(map, HashMap::new));
}
}
diff --git a/src/main/java/com/google/devtools/build/lib/collect/CompactHashSet.java b/src/main/java/com/google/devtools/build/lib/collect/CompactHashSet.java
index 88bfc84a81..6a81f2675f 100644
--- a/src/main/java/com/google/devtools/build/lib/collect/CompactHashSet.java
+++ b/src/main/java/com/google/devtools/build/lib/collect/CompactHashSet.java
@@ -31,7 +31,6 @@ package com.google.devtools.build.lib.collect;
import com.google.common.primitives.Ints;
import com.google.devtools.build.lib.util.Preconditions;
-
import java.io.IOException;
import java.io.InvalidObjectException;
import java.io.ObjectInputStream;
@@ -46,7 +45,6 @@ import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Objects;
-
import javax.annotation.Nullable;
/**
@@ -113,7 +111,7 @@ public class CompactHashSet<E> extends AbstractSet<E> implements Serializable {
* Creates an empty {@code CompactHashSet} instance.
*/
public static <E> CompactHashSet<E> create() {
- return new CompactHashSet<E>();
+ return new CompactHashSet<>();
}
/**
@@ -153,7 +151,7 @@ public class CompactHashSet<E> extends AbstractSet<E> implements Serializable {
* @throws IllegalArgumentException if {@code expectedSize} is negative
*/
public static <E> CompactHashSet<E> createWithExpectedSize(int expectedSize) {
- return new CompactHashSet<E>(expectedSize);
+ return new CompactHashSet<>(expectedSize);
}
private static final int MAXIMUM_CAPACITY = 1 << 30;
diff --git a/src/main/java/com/google/devtools/build/lib/collect/EquivalenceRelation.java b/src/main/java/com/google/devtools/build/lib/collect/EquivalenceRelation.java
index 7a7517794c..af379d02c3 100644
--- a/src/main/java/com/google/devtools/build/lib/collect/EquivalenceRelation.java
+++ b/src/main/java/com/google/devtools/build/lib/collect/EquivalenceRelation.java
@@ -15,29 +15,28 @@
package com.google.devtools.build.lib.collect;
/**
- * A comparison function, which imposes an equivalence relation on some
- * collection of objects.
+ * A comparison function, which imposes an equivalence relation on some collection of objects.
*
- * <p>The ordering imposed by an EquivalenceRelation <tt>e</tt> on a set of
- * elements <tt>S</tt> is said to be <i>consistent with equals</i> if and only
- * if <tt>(compare((Object)e1, (Object)e2)==0)</tt> has the same boolean value
- * as <tt>e1.equals((Object)e2)</tt> for every <tt>e1</tt> and <tt>e2</tt> in
- * <tt>S</tt>.<p>
+ * <p>The ordering imposed by an EquivalenceRelation <tt>e</tt> on a set of elements <tt>S</tt> is
+ * said to be <i>consistent with equals</i> if and only if <tt>(compare((Object)e1,
+ * (Object)e2)==0)</tt> has the same boolean value as <tt>e1.equals((Object)e2)</tt> for every
+ * <tt>e1</tt> and <tt>e2</tt> in <tt>S</tt>.
*
- * <p>Unlike {@link java.util.Comparator}, whose implementations are often
- * consistent with equals, the applications for which EquivalenceRelation
- * instances are used means that its implementations rarely are. They may are
- * usually more or less discriminative than the default equivalence relation
- * for the type.
+ * <p>
*
- * <p>For example, consider possible equivalence relations for {@link
- * java.lang.Integer}: the default equivalence defined by Integer.equals() is
- * based on the integer value is represents, but two alternative equivalences
- * would be {@link EquivalenceRelation#IDENTITY} (object identity&mdash;a more
- * discriminative relation) or <i>parity</i> (under which all even numbers, odd
- * numbers are considered equivalent to each other&mdash;a less discriminative
- * relation).
+ * <p>Unlike {@link java.util.Comparator}, whose implementations are often consistent with equals,
+ * the applications for which EquivalenceRelation instances are used means that its implementations
+ * rarely are. They may are usually more or less discriminative than the default equivalence
+ * relation for the type.
+ *
+ * <p>For example, consider possible equivalence relations for {@link java.lang.Integer}: the
+ * default equivalence defined by Integer.equals() is based on the integer value is represents, but
+ * two alternative equivalences would be {@link EquivalenceRelation#IDENTITY} (object
+ * identity&mdash;a more discriminative relation) or <i>parity</i> (under which all even numbers,
+ * odd numbers are considered equivalent to each other&mdash;a less discriminative relation).
*/
+// TODO: Consider phasing out this interface in favour of com.google.common.base.Equivalence
+@FunctionalInterface
public interface EquivalenceRelation<T> {
// This should be a superinterface of Comparator.
@@ -64,30 +63,16 @@ public interface EquivalenceRelation<T> {
int compare(T o1, T o2);
/**
- * The object-identity equivalence relation. This is the strictest possible
- * equivalence relation for objects, and considers two values equal iff they
- * are references to the same object instance.
+ * The object-identity equivalence relation. This is the strictest possible equivalence relation
+ * for objects, and considers two values equal iff they are references to the same object
+ * instance.
*/
- public static final EquivalenceRelation<?> IDENTITY =
- new EquivalenceRelation<Object>() {
- @Override
- public int compare(Object o1, Object o2) {
- return o1 == o2 ? 0 : -1;
- }
- };
+ EquivalenceRelation<?> IDENTITY = (EquivalenceRelation<Object>) (o1, o2) -> (o1 == o2) ? 0 : -1;
/**
- * The default equivalence relation for type T, using T.equals(). This
- * relation considers two values equivalent if either they are both null, or
- * o1.equals(o2).
+ * The default equivalence relation for type T, using T.equals(). This relation considers two
+ * values equivalent if either they are both null, or o1.equals(o2).
*/
- public static final EquivalenceRelation<?> DEFAULT =
- new EquivalenceRelation<Object>() {
- @Override
- public int compare(Object o1, Object o2) {
- return (o1 == null ? o2 == null : o1.equals(o2))
- ? 0
- : -1;
- }
- };
+ EquivalenceRelation<?> DEFAULT =
+ (EquivalenceRelation<Object>) (o1, o2) -> (o1 == null ? o2 == null : o1.equals(o2)) ? 0 : -1;
}
diff --git a/src/main/java/com/google/devtools/build/lib/collect/ImmutableSortedKeyListMultimap.java b/src/main/java/com/google/devtools/build/lib/collect/ImmutableSortedKeyListMultimap.java
index 1e164891f1..0fc427bb7f 100644
--- a/src/main/java/com/google/devtools/build/lib/collect/ImmutableSortedKeyListMultimap.java
+++ b/src/main/java/com/google/devtools/build/lib/collect/ImmutableSortedKeyListMultimap.java
@@ -21,8 +21,8 @@ import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ListMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multiset;
+import com.google.common.primitives.Ints;
import com.google.devtools.build.lib.util.Preconditions;
-
import java.util.AbstractCollection;
import java.util.AbstractMap;
import java.util.AbstractMap.SimpleImmutableEntry;
@@ -34,7 +34,6 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
-
import javax.annotation.Nullable;
/**
@@ -122,10 +121,7 @@ public final class ImmutableSortedKeyListMultimap<K extends Comparable<K>, V>
}
public Builder<K, V> putAll(Multimap<? extends K, ? extends V> multimap) {
- for (Map.Entry<? extends K, ? extends Collection<? extends V>> entry
- : multimap.asMap().entrySet()) {
- putAll(entry.getKey(), entry.getValue());
- }
+ multimap.asMap().forEach((key, collectionValue) -> putAll(key, collectionValue));
return this;
}
}
@@ -172,7 +168,7 @@ public final class ImmutableSortedKeyListMultimap<K extends Comparable<K>, V>
public Set<Entry<K, Collection<V>>> entrySet() {
ImmutableSet.Builder<Entry<K, Collection<V>>> builder = ImmutableSet.builder();
for (int i = 0; i < sortedKeys.length; i++) {
- builder.add(new SimpleImmutableEntry<K, Collection<V>>(sortedKeys[i], values[i]));
+ builder.add(new SimpleImmutableEntry<>(sortedKeys[i], values[i]));
}
return builder.build();
}
@@ -255,11 +251,7 @@ public final class ImmutableSortedKeyListMultimap<K extends Comparable<K>, V>
@Override
public int size() {
- int result = 0;
- for (List<V> list : values) {
- result += list.size();
- }
- return result;
+ return Ints.saturatedCast(Arrays.stream(values).mapToLong(List::size).sum());
}
@Override
@@ -275,21 +267,13 @@ public final class ImmutableSortedKeyListMultimap<K extends Comparable<K>, V>
@Override
public boolean containsValue(Object value) {
- for (List<V> list : values) {
- if (list.contains(value)) {
- return true;
- }
- }
- return false;
+ return Arrays.stream(values).anyMatch(list -> list.contains(value));
}
@Override
public boolean containsEntry(Object key, Object value) {
int index = Arrays.binarySearch(sortedKeys, key);
- if (index >= 0) {
- return values[index].contains(value);
- }
- return false;
+ return index >= 0 && values[index].contains(value);
}
@Override
@@ -330,7 +314,7 @@ public final class ImmutableSortedKeyListMultimap<K extends Comparable<K>, V>
@Override
public List<V> get(K key) {
int index = Arrays.binarySearch(sortedKeys, key);
- return index >= 0 ? values[index] : ImmutableList.<V>of();
+ return index >= 0 ? values[index] : ImmutableList.of();
}
@Override
@@ -353,7 +337,7 @@ public final class ImmutableSortedKeyListMultimap<K extends Comparable<K>, V>
ImmutableList.Builder<Entry<K, V>> builder = ImmutableList.builder();
for (int i = 0; i < sortedKeys.length; i++) {
for (V value : values[i]) {
- builder.add(new SimpleImmutableEntry<K, V>(sortedKeys[i], value));
+ builder.add(new SimpleImmutableEntry<>(sortedKeys[i], value));
}
}
return builder.build();
diff --git a/src/main/java/com/google/devtools/build/lib/collect/ImmutableSortedKeyMap.java b/src/main/java/com/google/devtools/build/lib/collect/ImmutableSortedKeyMap.java
index 535f4b3552..d3b42c258f 100644
--- a/src/main/java/com/google/devtools/build/lib/collect/ImmutableSortedKeyMap.java
+++ b/src/main/java/com/google/devtools/build/lib/collect/ImmutableSortedKeyMap.java
@@ -13,10 +13,13 @@
// limitations under the License.
package com.google.devtools.build.lib.collect;
+import static com.google.common.collect.ImmutableSet.toImmutableSet;
+import static java.util.stream.Collectors.joining;
+
import com.google.common.collect.AbstractIterator;
import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Streams;
import com.google.devtools.build.lib.util.Preconditions;
-
import java.util.AbstractCollection;
import java.util.AbstractMap.SimpleImmutableEntry;
import java.util.Arrays;
@@ -26,7 +29,7 @@ import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
-
+import java.util.stream.Stream;
import javax.annotation.Nullable;
/**
@@ -120,9 +123,7 @@ public final class ImmutableSortedKeyMap<K extends Comparable<K>, V> implements
}
public Builder<K, V> putAll(Map<? extends K, ? extends V> map) {
- for (Map.Entry<? extends K, ? extends V> entry : map.entrySet()) {
- put(entry.getKey(), entry.getValue());
- }
+ map.forEach((key, value) -> put(key, value));
return this;
}
}
@@ -215,15 +216,7 @@ public final class ImmutableSortedKeyMap<K extends Comparable<K>, V> implements
@Override
public boolean containsValue(@Nullable Object value) {
- if (value == null) {
- return false;
- }
- for (V v : values) {
- if (v.equals(value)) {
- return true;
- }
- }
- return false;
+ return value != null && Arrays.stream(values).anyMatch(v -> v.equals(value));
}
@Override
@@ -267,34 +260,18 @@ public final class ImmutableSortedKeyMap<K extends Comparable<K>, V> implements
@Override
public Set<Entry<K, V>> entrySet() {
- ImmutableSet.Builder<Entry<K, V>> builder = ImmutableSet.builder();
- for (int i = 0; i < sortedKeys.length; i++) {
- builder.add(new SimpleImmutableEntry<K, V>(sortedKeys[i], values[i]));
- }
- return builder.build();
+ return entryStream().collect(toImmutableSet());
}
@Override
public String toString() {
- StringBuilder result = new StringBuilder();
- result.append('{');
- for (int i = 0; i < sortedKeys.length; i++) {
- if (i != 0) {
- result.append(", ");
- }
- result.append(sortedKeys[i]).append('=').append(values[i]);
- }
- result.append('}');
- return result.toString();
+ return Streams.zip(Arrays.stream(sortedKeys), Arrays.stream(values), (k, v) -> k + "=" + v)
+ .collect(joining(", ", "{", "}"));
}
@Override
public int hashCode() {
- int h = 0;
- for (Entry<K, V> entry : entrySet()) {
- h += entry.hashCode();
- }
- return h;
+ return entryStream().mapToInt(Entry::hashCode).sum();
}
@Override
@@ -307,4 +284,8 @@ public final class ImmutableSortedKeyMap<K extends Comparable<K>, V> implements
}
return false;
}
+
+ private Stream<Entry<K, V>> entryStream() {
+ return Streams.zip(Arrays.stream(sortedKeys), Arrays.stream(values), SimpleImmutableEntry::new);
+ }
}
diff --git a/src/main/java/com/google/devtools/build/lib/collect/IterablesChain.java b/src/main/java/com/google/devtools/build/lib/collect/IterablesChain.java
index 0286798e2a..5fa4f8621e 100644
--- a/src/main/java/com/google/devtools/build/lib/collect/IterablesChain.java
+++ b/src/main/java/com/google/devtools/build/lib/collect/IterablesChain.java
@@ -116,7 +116,7 @@ public final class IterablesChain<T> implements Iterable<T> {
*/
public IterablesChain<T> build() {
if (isEmpty()) {
- return new IterablesChain<>(ImmutableList.<T>of());
+ return new IterablesChain<>(ImmutableList.of());
}
Iterable<T> concat = Iterables.concat(ImmutableList.copyOf(iterables));
return new IterablesChain<>(deduplicate ? new Deduper<>(concat) : concat);
@@ -136,7 +136,7 @@ public final class IterablesChain<T> implements Iterable<T> {
@Override
public Iterator<T> iterator() {
- return new DedupingIterator<T>(iterable.iterator());
+ return new DedupingIterator<>(iterable.iterator());
}
}
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;
/**