diff --git a/.github/workflows/scorecard.yml b/.github/workflows/scorecard.yml index 8cfeb49d183f..83bf9d44c868 100644 --- a/.github/workflows/scorecard.yml +++ b/.github/workflows/scorecard.yml @@ -67,6 +67,6 @@ jobs: # Upload the results to GitHub's code scanning dashboard. - name: "Upload to code-scanning" - uses: github/codeql-action/upload-sarif@aa578102511db1f4524ed59b8cc2bae4f6e88195 # v3.27.6 + uses: github/codeql-action/upload-sarif@f09c1c0a94de965c15400f5634aa42fac8fb8f88 # v3.27.5 with: sarif_file: results.sarif diff --git a/android/guava-tests/test/com/google/common/base/JoinerTest.java b/android/guava-tests/test/com/google/common/base/JoinerTest.java index c02127e9d55d..36cba68cf32a 100644 --- a/android/guava-tests/test/com/google/common/base/JoinerTest.java +++ b/android/guava-tests/test/com/google/common/base/JoinerTest.java @@ -17,24 +17,22 @@ package com.google.common.base; import static com.google.common.base.ReflectionFreeAssertThrows.assertThrows; -import static com.google.common.collect.Lists.newArrayList; -import static java.util.Collections.unmodifiableList; import com.google.common.annotations.GwtCompatible; import com.google.common.annotations.GwtIncompatible; import com.google.common.annotations.J2ktIncompatible; import com.google.common.base.Joiner.MapJoiner; -import com.google.common.collect.ForwardingList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMultimap; +import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.google.common.testing.NullPointerTester; import java.io.IOException; import java.util.Arrays; -import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; +import junit.framework.AssertionFailedError; import junit.framework.TestCase; import org.checkerframework.checker.nullness.qual.Nullable; @@ -62,56 +60,12 @@ public class JoinerTest extends TestCase { private static final Iterable<@Nullable Integer> ITERABLE_FOUR_NULLS = Arrays.asList((Integer) null, null, null, null); - /* - * Both of these fields *are* immutable/constant. They don't use the type ImmutableList because - * they need to behave slightly differently. - */ - @SuppressWarnings("ConstantCaseForConstants") - private static final List UNDERREPORTING_SIZE_LIST; - - @SuppressWarnings("ConstantCaseForConstants") - private static final List OVERREPORTING_SIZE_LIST; - - static { - List collection123 = Arrays.asList(1, 2, 3); - UNDERREPORTING_SIZE_LIST = unmodifiableList(new MisleadingSizeList<>(collection123, -1)); - OVERREPORTING_SIZE_LIST = unmodifiableList(new MisleadingSizeList<>(collection123, 1)); - } - - /* - * c.g.c.collect.testing.Helpers.misleadingSizeList has a broken Iterator, so we can't use it. (I - * mean, ideally we'd fix it....) Also, we specifically need a List so that we trigger the fast - * path in join(Iterable). - */ - private static final class MisleadingSizeList - extends ForwardingList { - final List delegate; - final int delta; - - MisleadingSizeList(List delegate, int delta) { - this.delegate = delegate; - this.delta = delta; - } - - @Override - protected List delegate() { - return delegate; - } - - @Override - public int size() { - return delegate.size() + delta; - } - } - @SuppressWarnings("JoinIterableIterator") // explicitly testing iterator overload, too public void testNoSpecialNullBehavior() { checkNoOutput(J, ITERABLE_); checkResult(J, ITERABLE_1, "1"); checkResult(J, ITERABLE_12, "1-2"); checkResult(J, ITERABLE_123, "1-2-3"); - checkResult(J, UNDERREPORTING_SIZE_LIST, "1-2-3"); - checkResult(J, OVERREPORTING_SIZE_LIST, "1-2-3"); assertThrows(NullPointerException.class, () -> J.join(ITERABLE_NULL)); assertThrows(NullPointerException.class, () -> J.join(ITERABLE_1_NULL_2)); @@ -126,8 +80,6 @@ public void testOnCharOverride() { checkResult(onChar, ITERABLE_1, "1"); checkResult(onChar, ITERABLE_12, "1-2"); checkResult(onChar, ITERABLE_123, "1-2-3"); - checkResult(J, UNDERREPORTING_SIZE_LIST, "1-2-3"); - checkResult(J, OVERREPORTING_SIZE_LIST, "1-2-3"); } public void testSkipNulls() { @@ -139,8 +91,6 @@ public void testSkipNulls() { checkResult(skipNulls, ITERABLE_1, "1"); checkResult(skipNulls, ITERABLE_12, "1-2"); checkResult(skipNulls, ITERABLE_123, "1-2-3"); - checkResult(J, UNDERREPORTING_SIZE_LIST, "1-2-3"); - checkResult(J, OVERREPORTING_SIZE_LIST, "1-2-3"); checkResult(skipNulls, ITERABLE_NULL_1, "1"); checkResult(skipNulls, ITERABLE_1_NULL, "1"); checkResult(skipNulls, ITERABLE_1_NULL_2, "1-2"); @@ -152,8 +102,6 @@ public void testUseForNull() { checkResult(zeroForNull, ITERABLE_1, "1"); checkResult(zeroForNull, ITERABLE_12, "1-2"); checkResult(zeroForNull, ITERABLE_123, "1-2-3"); - checkResult(J, UNDERREPORTING_SIZE_LIST, "1-2-3"); - checkResult(J, OVERREPORTING_SIZE_LIST, "1-2-3"); checkResult(zeroForNull, ITERABLE_NULL, "0"); checkResult(zeroForNull, ITERABLE_NULL_NULL, "0-0"); checkResult(zeroForNull, ITERABLE_NULL_1, "0-1"); @@ -166,7 +114,7 @@ private static void checkNoOutput(Joiner joiner, Iterable set) { assertEquals("", joiner.join(set)); assertEquals("", joiner.join(set.iterator())); - Object[] array = newArrayList(set).toArray(new Integer[0]); + Object[] array = Lists.newArrayList(set).toArray(new Integer[0]); assertEquals("", joiner.join(array)); StringBuilder sb1FromIterable = new StringBuilder(); @@ -231,8 +179,7 @@ private static void checkResult(Joiner joiner, Iterable parts, String e joiner.appendTo(sb1FromIterator, parts.iterator()); assertEquals("x" + expected, sb1FromIterator.toString()); - // The use of iterator() works around J2KT b/381065164. - Integer[] partsArray = newArrayList(parts.iterator()).toArray(new Integer[0]); + Integer[] partsArray = Lists.newArrayList(parts).toArray(new Integer[0]); assertEquals(expected, joiner.join(partsArray)); StringBuilder sb2 = new StringBuilder().append('x'); @@ -322,6 +269,36 @@ public void test_skipNulls_onMap() { assertThrows(UnsupportedOperationException.class, () -> j.withKeyValueSeparator("/")); } + private static class DontStringMeBro implements CharSequence { + @Override + public int length() { + return 3; + } + + @Override + public char charAt(int index) { + return "foo".charAt(index); + } + + @Override + public CharSequence subSequence(int start, int end) { + return "foo".subSequence(start, end); + } + + @Override + public String toString() { + throw new AssertionFailedError("shouldn't be invoked"); + } + } + + @GwtIncompatible // StringBuilder.append in GWT invokes Object.toString(), unlike the JRE version. + public void testDontConvertCharSequenceToString() { + assertEquals("foo,foo", Joiner.on(",").join(new DontStringMeBro(), new DontStringMeBro())); + assertEquals( + "foo,bar,foo", + Joiner.on(",").useForNull("bar").join(new DontStringMeBro(), null, new DontStringMeBro())); + } + @J2ktIncompatible @GwtIncompatible // NullPointerTester public void testNullPointers() { diff --git a/android/guava-tests/test/com/google/common/collect/ImmutableListTest.java b/android/guava-tests/test/com/google/common/collect/ImmutableListTest.java index bc45424d78ea..d8b4bbdf7425 100644 --- a/android/guava-tests/test/com/google/common/collect/ImmutableListTest.java +++ b/android/guava-tests/test/com/google/common/collect/ImmutableListTest.java @@ -627,8 +627,6 @@ public void testAddOverflowCollection() { assertThrows( IllegalArgumentException.class, () -> builder.addAll(nCopies(Integer.MAX_VALUE - 50, "a"))); - assertThat(expected) - .hasMessageThat() - .contains("cannot store more than Integer.MAX_VALUE elements"); + assertThat(expected).hasMessageThat().contains("cannot store more than MAX_VALUE elements"); } } diff --git a/android/guava-tests/test/com/google/common/collect/SynchronizedNavigableMapTest.java b/android/guava-tests/test/com/google/common/collect/SynchronizedNavigableMapTest.java index d92a94847711..a3ef13f10ade 100644 --- a/android/guava-tests/test/com/google/common/collect/SynchronizedNavigableMapTest.java +++ b/android/guava-tests/test/com/google/common/collect/SynchronizedNavigableMapTest.java @@ -261,7 +261,7 @@ public static TestSuite suite() { suite.addTest( NavigableMapTestSuiteBuilder.using( new TestStringSortedMapGenerator() { - private final Object mutex = new Object[0]; // something Serializable + private final Object mutex = new Integer(1); @Override protected SortedMap create(Entry[] entries) { diff --git a/android/guava-tests/test/com/google/common/math/DoubleMathTest.java b/android/guava-tests/test/com/google/common/math/DoubleMathTest.java index 70ae02c6e540..c9c12c8194d6 100644 --- a/android/guava-tests/test/com/google/common/math/DoubleMathTest.java +++ b/android/guava-tests/test/com/google/common/math/DoubleMathTest.java @@ -447,7 +447,6 @@ public void testLog2NaNInfinity() { } @GwtIncompatible // StrictMath - @SuppressWarnings("strictfp") // Guava still supports Java 8 private strictfp double trueLog2(double d) { double trueLog2 = StrictMath.log(d) / StrictMath.log(2); // increment until it's >= the true value diff --git a/android/guava/src/com/google/common/base/Joiner.java b/android/guava/src/com/google/common/base/Joiner.java index 2ba41d2ba16d..acb7d8d12fc8 100644 --- a/android/guava/src/com/google/common/base/Joiner.java +++ b/android/guava/src/com/google/common/base/Joiner.java @@ -205,20 +205,10 @@ public final StringBuilder appendTo( * Returns a string containing the string representation of each of {@code parts}, using the * previously configured separator between each. */ - public String join(Iterable parts) { - // We don't use the same optimization here as in the JRE flavor. - // TODO: b/381289911 - Evaluate the performance impact of doing so. + public final String join(Iterable parts) { return join(parts.iterator()); } - /* - * TODO: b/381289911 - Make the Iterator overload use StringJoiner (including Android or not)—or - * some other optimization, given that StringJoiner can over-allocate: - * https://bugs.openjdk.org/browse/JDK-8305774 - */ - - // TODO: b/381289911 - Optimize MapJoiner similarly to Joiner (including Android or not). - /** * Returns a string containing the string representation of each of {@code parts}, using the * previously configured separator between each. @@ -278,12 +268,6 @@ public Joiner skipNulls() { */ public Joiner skipNulls() { return new Joiner(this) { - @Override - @SuppressWarnings("JoinIterableIterator") // suggests infinite recursion - public String join(Iterable parts) { - return join(parts.iterator()); - } - @Override public A appendTo( A appendable, Iterator parts) throws IOException { @@ -486,7 +470,6 @@ public MapJoiner useForNull(String nullText) { } } - // TODO(cpovirk): Rename to "toCharSequence." CharSequence toString(@CheckForNull Object part) { /* * requireNonNull is not safe: Joiner.on(...).join(somethingThatContainsNull) will indeed throw. @@ -532,23 +515,4 @@ public Object get(int index) { } }; } - - // cloned from ImmutableCollection - private static int expandedCapacity(int oldCapacity, int minCapacity) { - if (minCapacity < 0) { - throw new IllegalArgumentException("cannot store more than Integer.MAX_VALUE elements"); - } else if (minCapacity <= oldCapacity) { - return oldCapacity; - } - // careful of overflow! - int newCapacity = oldCapacity + (oldCapacity >> 1) + 1; - if (newCapacity < minCapacity) { - newCapacity = Integer.highestOneBit(minCapacity - 1) << 1; - } - if (newCapacity < 0) { - newCapacity = Integer.MAX_VALUE; - // guaranteed to be >= newCapacity - } - return newCapacity; - } } diff --git a/android/guava/src/com/google/common/collect/ImmutableCollection.java b/android/guava/src/com/google/common/collect/ImmutableCollection.java index 0acdf9789754..191934da1007 100644 --- a/android/guava/src/com/google/common/collect/ImmutableCollection.java +++ b/android/guava/src/com/google/common/collect/ImmutableCollection.java @@ -404,7 +404,7 @@ public abstract static class Builder { static int expandedCapacity(int oldCapacity, int minCapacity) { if (minCapacity < 0) { - throw new IllegalArgumentException("cannot store more than Integer.MAX_VALUE elements"); + throw new IllegalArgumentException("cannot store more than MAX_VALUE elements"); } else if (minCapacity <= oldCapacity) { return oldCapacity; } diff --git a/guava-tests/test/com/google/common/base/JoinerTest.java b/guava-tests/test/com/google/common/base/JoinerTest.java index c02127e9d55d..36cba68cf32a 100644 --- a/guava-tests/test/com/google/common/base/JoinerTest.java +++ b/guava-tests/test/com/google/common/base/JoinerTest.java @@ -17,24 +17,22 @@ package com.google.common.base; import static com.google.common.base.ReflectionFreeAssertThrows.assertThrows; -import static com.google.common.collect.Lists.newArrayList; -import static java.util.Collections.unmodifiableList; import com.google.common.annotations.GwtCompatible; import com.google.common.annotations.GwtIncompatible; import com.google.common.annotations.J2ktIncompatible; import com.google.common.base.Joiner.MapJoiner; -import com.google.common.collect.ForwardingList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMultimap; +import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.google.common.testing.NullPointerTester; import java.io.IOException; import java.util.Arrays; -import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; +import junit.framework.AssertionFailedError; import junit.framework.TestCase; import org.checkerframework.checker.nullness.qual.Nullable; @@ -62,56 +60,12 @@ public class JoinerTest extends TestCase { private static final Iterable<@Nullable Integer> ITERABLE_FOUR_NULLS = Arrays.asList((Integer) null, null, null, null); - /* - * Both of these fields *are* immutable/constant. They don't use the type ImmutableList because - * they need to behave slightly differently. - */ - @SuppressWarnings("ConstantCaseForConstants") - private static final List UNDERREPORTING_SIZE_LIST; - - @SuppressWarnings("ConstantCaseForConstants") - private static final List OVERREPORTING_SIZE_LIST; - - static { - List collection123 = Arrays.asList(1, 2, 3); - UNDERREPORTING_SIZE_LIST = unmodifiableList(new MisleadingSizeList<>(collection123, -1)); - OVERREPORTING_SIZE_LIST = unmodifiableList(new MisleadingSizeList<>(collection123, 1)); - } - - /* - * c.g.c.collect.testing.Helpers.misleadingSizeList has a broken Iterator, so we can't use it. (I - * mean, ideally we'd fix it....) Also, we specifically need a List so that we trigger the fast - * path in join(Iterable). - */ - private static final class MisleadingSizeList - extends ForwardingList { - final List delegate; - final int delta; - - MisleadingSizeList(List delegate, int delta) { - this.delegate = delegate; - this.delta = delta; - } - - @Override - protected List delegate() { - return delegate; - } - - @Override - public int size() { - return delegate.size() + delta; - } - } - @SuppressWarnings("JoinIterableIterator") // explicitly testing iterator overload, too public void testNoSpecialNullBehavior() { checkNoOutput(J, ITERABLE_); checkResult(J, ITERABLE_1, "1"); checkResult(J, ITERABLE_12, "1-2"); checkResult(J, ITERABLE_123, "1-2-3"); - checkResult(J, UNDERREPORTING_SIZE_LIST, "1-2-3"); - checkResult(J, OVERREPORTING_SIZE_LIST, "1-2-3"); assertThrows(NullPointerException.class, () -> J.join(ITERABLE_NULL)); assertThrows(NullPointerException.class, () -> J.join(ITERABLE_1_NULL_2)); @@ -126,8 +80,6 @@ public void testOnCharOverride() { checkResult(onChar, ITERABLE_1, "1"); checkResult(onChar, ITERABLE_12, "1-2"); checkResult(onChar, ITERABLE_123, "1-2-3"); - checkResult(J, UNDERREPORTING_SIZE_LIST, "1-2-3"); - checkResult(J, OVERREPORTING_SIZE_LIST, "1-2-3"); } public void testSkipNulls() { @@ -139,8 +91,6 @@ public void testSkipNulls() { checkResult(skipNulls, ITERABLE_1, "1"); checkResult(skipNulls, ITERABLE_12, "1-2"); checkResult(skipNulls, ITERABLE_123, "1-2-3"); - checkResult(J, UNDERREPORTING_SIZE_LIST, "1-2-3"); - checkResult(J, OVERREPORTING_SIZE_LIST, "1-2-3"); checkResult(skipNulls, ITERABLE_NULL_1, "1"); checkResult(skipNulls, ITERABLE_1_NULL, "1"); checkResult(skipNulls, ITERABLE_1_NULL_2, "1-2"); @@ -152,8 +102,6 @@ public void testUseForNull() { checkResult(zeroForNull, ITERABLE_1, "1"); checkResult(zeroForNull, ITERABLE_12, "1-2"); checkResult(zeroForNull, ITERABLE_123, "1-2-3"); - checkResult(J, UNDERREPORTING_SIZE_LIST, "1-2-3"); - checkResult(J, OVERREPORTING_SIZE_LIST, "1-2-3"); checkResult(zeroForNull, ITERABLE_NULL, "0"); checkResult(zeroForNull, ITERABLE_NULL_NULL, "0-0"); checkResult(zeroForNull, ITERABLE_NULL_1, "0-1"); @@ -166,7 +114,7 @@ private static void checkNoOutput(Joiner joiner, Iterable set) { assertEquals("", joiner.join(set)); assertEquals("", joiner.join(set.iterator())); - Object[] array = newArrayList(set).toArray(new Integer[0]); + Object[] array = Lists.newArrayList(set).toArray(new Integer[0]); assertEquals("", joiner.join(array)); StringBuilder sb1FromIterable = new StringBuilder(); @@ -231,8 +179,7 @@ private static void checkResult(Joiner joiner, Iterable parts, String e joiner.appendTo(sb1FromIterator, parts.iterator()); assertEquals("x" + expected, sb1FromIterator.toString()); - // The use of iterator() works around J2KT b/381065164. - Integer[] partsArray = newArrayList(parts.iterator()).toArray(new Integer[0]); + Integer[] partsArray = Lists.newArrayList(parts).toArray(new Integer[0]); assertEquals(expected, joiner.join(partsArray)); StringBuilder sb2 = new StringBuilder().append('x'); @@ -322,6 +269,36 @@ public void test_skipNulls_onMap() { assertThrows(UnsupportedOperationException.class, () -> j.withKeyValueSeparator("/")); } + private static class DontStringMeBro implements CharSequence { + @Override + public int length() { + return 3; + } + + @Override + public char charAt(int index) { + return "foo".charAt(index); + } + + @Override + public CharSequence subSequence(int start, int end) { + return "foo".subSequence(start, end); + } + + @Override + public String toString() { + throw new AssertionFailedError("shouldn't be invoked"); + } + } + + @GwtIncompatible // StringBuilder.append in GWT invokes Object.toString(), unlike the JRE version. + public void testDontConvertCharSequenceToString() { + assertEquals("foo,foo", Joiner.on(",").join(new DontStringMeBro(), new DontStringMeBro())); + assertEquals( + "foo,bar,foo", + Joiner.on(",").useForNull("bar").join(new DontStringMeBro(), null, new DontStringMeBro())); + } + @J2ktIncompatible @GwtIncompatible // NullPointerTester public void testNullPointers() { diff --git a/guava-tests/test/com/google/common/collect/ImmutableListTest.java b/guava-tests/test/com/google/common/collect/ImmutableListTest.java index e9abcf1092b3..2f32546eba58 100644 --- a/guava-tests/test/com/google/common/collect/ImmutableListTest.java +++ b/guava-tests/test/com/google/common/collect/ImmutableListTest.java @@ -613,8 +613,6 @@ public void testAddOverflowCollection() { assertThrows( IllegalArgumentException.class, () -> builder.addAll(nCopies(Integer.MAX_VALUE - 50, "a"))); - assertThat(expected) - .hasMessageThat() - .contains("cannot store more than Integer.MAX_VALUE elements"); + assertThat(expected).hasMessageThat().contains("cannot store more than MAX_VALUE elements"); } } diff --git a/guava-tests/test/com/google/common/collect/SynchronizedNavigableMapTest.java b/guava-tests/test/com/google/common/collect/SynchronizedNavigableMapTest.java index d92a94847711..a3ef13f10ade 100644 --- a/guava-tests/test/com/google/common/collect/SynchronizedNavigableMapTest.java +++ b/guava-tests/test/com/google/common/collect/SynchronizedNavigableMapTest.java @@ -261,7 +261,7 @@ public static TestSuite suite() { suite.addTest( NavigableMapTestSuiteBuilder.using( new TestStringSortedMapGenerator() { - private final Object mutex = new Object[0]; // something Serializable + private final Object mutex = new Integer(1); @Override protected SortedMap create(Entry[] entries) { diff --git a/guava-tests/test/com/google/common/math/DoubleMathTest.java b/guava-tests/test/com/google/common/math/DoubleMathTest.java index 70ae02c6e540..c9c12c8194d6 100644 --- a/guava-tests/test/com/google/common/math/DoubleMathTest.java +++ b/guava-tests/test/com/google/common/math/DoubleMathTest.java @@ -447,7 +447,6 @@ public void testLog2NaNInfinity() { } @GwtIncompatible // StrictMath - @SuppressWarnings("strictfp") // Guava still supports Java 8 private strictfp double trueLog2(double d) { double trueLog2 = StrictMath.log(d) / StrictMath.log(2); // increment until it's >= the true value diff --git a/guava/src/com/google/common/base/Joiner.java b/guava/src/com/google/common/base/Joiner.java index b03a626b821f..acb7d8d12fc8 100644 --- a/guava/src/com/google/common/base/Joiner.java +++ b/guava/src/com/google/common/base/Joiner.java @@ -205,60 +205,10 @@ public final StringBuilder appendTo( * Returns a string containing the string representation of each of {@code parts}, using the * previously configured separator between each. */ - public String join(Iterable parts) { - /* - * If we can quickly determine how many elements there are likely to be, then we can use the - * fastest possible implementation, which delegates to the array overload of String.join. - * - * In theory, we can quickly determine the size of any Collection. However, thanks to - * regrettable implementations like our own Sets.filter, Collection.size() is sometimes a - * linear-time operation, and it can even have side effects. Thus, we limit the special case to - * List, which is _even more likely_ to have size() implemented to be fast and side-effect-free. - * - * We could consider recognizing specific other collections as safe (like ImmutableCollection, - * except ContiguousSet!) or as not worth this optimization (CopyOnWriteArrayList?). - */ - if (parts instanceof List) { - int size = ((List) parts).size(); - if (size == 0) { - return ""; - } - CharSequence[] toJoin = new CharSequence[size]; - int i = 0; - for (Object part : parts) { - if (i == toJoin.length) { - /* - * We first initialized toJoin to the size of the input collection. However, that size can - * go out of date (for a collection like CopyOnWriteArrayList, which may have been safely - * modified concurrently), or it might have been only an estimate to begin with (for a - * collection like ConcurrentHashMap, which sums up several counters that may not be in - * sync with one another). We accommodate that by resizing as necessary. - */ - toJoin = Arrays.copyOf(toJoin, expandedCapacity(toJoin.length, toJoin.length + 1)); - } - toJoin[i++] = toString(part); - } - // We might not have seen the expected number of elements, as discussed above. - if (i != toJoin.length) { - toJoin = Arrays.copyOf(toJoin, i); - } - // What we care about is Android, under which this method is always desugared: - // https://r8.googlesource.com/r8/+/05ba76883518bff06496d6d7df5f06b94a88fb00/src/main/java/com/android/tools/r8/ir/desugar/BackportedMethodRewriter.java#831 - @SuppressWarnings("Java7ApiChecker") - String result = String.join(separator, toJoin); - return result; - } + public final String join(Iterable parts) { return join(parts.iterator()); } - /* - * TODO: b/381289911 - Make the Iterator overload use StringJoiner (including Android or not)—or - * some other optimization, given that StringJoiner can over-allocate: - * https://bugs.openjdk.org/browse/JDK-8305774 - */ - - // TODO: b/381289911 - Optimize MapJoiner similarly to Joiner (including Android or not). - /** * Returns a string containing the string representation of each of {@code parts}, using the * previously configured separator between each. @@ -318,12 +268,6 @@ public Joiner skipNulls() { */ public Joiner skipNulls() { return new Joiner(this) { - @Override - @SuppressWarnings("JoinIterableIterator") // suggests infinite recursion - public String join(Iterable parts) { - return join(parts.iterator()); - } - @Override public A appendTo( A appendable, Iterator parts) throws IOException { @@ -526,7 +470,6 @@ public MapJoiner useForNull(String nullText) { } } - // TODO(cpovirk): Rename to "toCharSequence." CharSequence toString(@CheckForNull Object part) { /* * requireNonNull is not safe: Joiner.on(...).join(somethingThatContainsNull) will indeed throw. @@ -572,23 +515,4 @@ public Object get(int index) { } }; } - - // cloned from ImmutableCollection - private static int expandedCapacity(int oldCapacity, int minCapacity) { - if (minCapacity < 0) { - throw new IllegalArgumentException("cannot store more than Integer.MAX_VALUE elements"); - } else if (minCapacity <= oldCapacity) { - return oldCapacity; - } - // careful of overflow! - int newCapacity = oldCapacity + (oldCapacity >> 1) + 1; - if (newCapacity < minCapacity) { - newCapacity = Integer.highestOneBit(minCapacity - 1) << 1; - } - if (newCapacity < 0) { - newCapacity = Integer.MAX_VALUE; - // guaranteed to be >= newCapacity - } - return newCapacity; - } } diff --git a/guava/src/com/google/common/collect/ImmutableCollection.java b/guava/src/com/google/common/collect/ImmutableCollection.java index 7de929a232c0..dc0c46a9a560 100644 --- a/guava/src/com/google/common/collect/ImmutableCollection.java +++ b/guava/src/com/google/common/collect/ImmutableCollection.java @@ -415,7 +415,7 @@ public abstract static class Builder { static int expandedCapacity(int oldCapacity, int minCapacity) { if (minCapacity < 0) { - throw new IllegalArgumentException("cannot store more than Integer.MAX_VALUE elements"); + throw new IllegalArgumentException("cannot store more than MAX_VALUE elements"); } else if (minCapacity <= oldCapacity) { return oldCapacity; }