Skip to content

Commit 816eaa1

Browse files
cpovirkGoogle Java Core Libraries
authored andcommitted
Use Preconditions in more places.
...by running the forthcoming https://errorprone.info/bugpattern/PreferPreconditions. RELNOTES=n/a PiperOrigin-RevId: 888270599
1 parent a99a4fc commit 816eaa1

File tree

22 files changed

+90
-96
lines changed

22 files changed

+90
-96
lines changed

android/guava-testlib/src/com/google/common/collect/testing/AbstractIteratorTester.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.common.collect.testing;
1818

19+
import static com.google.common.base.Preconditions.checkArgument;
1920
import static com.google.common.collect.testing.Helpers.copyToList;
2021
import static com.google.common.collect.testing.Helpers.copyToSet;
2122
import static java.lang.System.arraycopy;
@@ -287,9 +288,7 @@ public enum KnownOrder {
287288
// periodically we should manually try (steps * 3 / 2) here; all tests but
288289
// one should still pass (testVerifyGetsCalled()).
289290
stimuli = (Stimulus<E, ? super I>[]) new Stimulus<?, ?>[steps];
290-
if (!elementsToInsertIterable.iterator().hasNext()) {
291-
throw new IllegalArgumentException();
292-
}
291+
checkArgument(elementsToInsertIterable.iterator().hasNext());
293292
elementsToInsert = Helpers.cycle(elementsToInsertIterable);
294293
this.features = copyToSet(features);
295294
this.expectedElements = copyToList(expectedElements);

android/guava-testlib/src/com/google/common/collect/testing/FeatureSpecificTestSuiteBuilder.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.common.collect.testing;
1818

19+
import static com.google.common.base.Preconditions.checkArgument;
1920
import static com.google.common.collect.testing.Helpers.copyToSet;
2021
import static com.google.common.collect.testing.Helpers.getMethod;
2122
import static com.google.common.collect.testing.features.FeatureUtil.addImpliedFeatures;
@@ -134,11 +135,10 @@ public Set<Feature<?>> getFeatures() {
134135
/** Configures this builder produce a TestSuite with the given name. */
135136
@CanIgnoreReturnValue
136137
public B named(String name) {
137-
if (name.contains("(")) {
138-
throw new IllegalArgumentException(
139-
"Eclipse hides all characters after "
140-
+ "'('; please use '[]' or other characters instead of parentheses");
141-
}
138+
checkArgument(
139+
!name.contains("("),
140+
"Eclipse hides all characters after "
141+
+ "'('; please use '[]' or other characters instead of parentheses");
142142
this.name = name;
143143
return self();
144144
}

android/guava-testlib/src/com/google/common/collect/testing/Helpers.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.common.collect.testing;
1818

19+
import static com.google.common.base.Preconditions.checkNotNull;
1920
import static java.lang.Math.max;
2021
import static java.util.Arrays.asList;
2122
import static java.util.Collections.singletonMap;
@@ -480,11 +481,7 @@ public abstract static class NullsBefore implements Comparator<@Nullable String>
480481
private final String justAfterNull;
481482

482483
protected NullsBefore(String justAfterNull) {
483-
if (justAfterNull == null) {
484-
throw new NullPointerException();
485-
}
486-
487-
this.justAfterNull = justAfterNull;
484+
this.justAfterNull = checkNotNull(justAfterNull);
488485
}
489486

490487
@Override

android/guava-testlib/src/com/google/common/collect/testing/MinimalCollection.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.common.collect.testing;
1818

19+
import static com.google.common.base.Preconditions.checkNotNull;
1920
import static java.lang.System.arraycopy;
2021
import static java.util.Arrays.asList;
2122

@@ -77,9 +78,7 @@ public int size() {
7778
public boolean contains(@Nullable Object object) {
7879
if (!allowNulls) {
7980
// behave badly
80-
if (object == null) {
81-
throw new NullPointerException();
82-
}
81+
checkNotNull(object);
8382
}
8483
Platform.checkCast(type, object); // behave badly
8584
return asList(contents).contains(object);

android/guava-testlib/src/com/google/common/collect/testing/SafeTreeMap.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.google.common.collect.testing;
1818

19+
import static com.google.common.base.Preconditions.checkNotNull;
20+
1921
import com.google.common.annotations.GwtIncompatible;
2022
import com.google.errorprone.annotations.CanIgnoreReturnValue;
2123
import java.io.Serializable;
@@ -62,10 +64,7 @@ public SafeTreeMap(SortedMap<K, ? extends V> map) {
6264
}
6365

6466
private SafeTreeMap(NavigableMap<K, V> delegate) {
65-
this.delegate = delegate;
66-
if (delegate == null) {
67-
throw new NullPointerException();
68-
}
67+
this.delegate = checkNotNull(delegate);
6968
for (K k : keySet()) {
7069
checkValid(k);
7170
}

android/guava-tests/benchmark/com/google/common/util/concurrent/MonitorBasedArrayBlockingQueue.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
package com.google.common.util.concurrent;
1818

19+
import static com.google.common.base.Preconditions.checkArgument;
20+
import static com.google.common.base.Preconditions.checkNotNull;
21+
1922
import com.google.common.annotations.GwtIncompatible;
2023
import com.google.common.annotations.J2ktIncompatible;
2124
import com.google.common.collect.ObjectArrays;
@@ -168,7 +171,7 @@ public MonitorBasedArrayBlockingQueue(int capacity) {
168171
* @throws IllegalArgumentException if {@code capacity} is less than 1
169172
*/
170173
public MonitorBasedArrayBlockingQueue(int capacity, boolean fair) {
171-
if (capacity <= 0) throw new IllegalArgumentException();
174+
checkArgument(capacity > 0);
172175
this.items = newEArray(capacity);
173176
monitor = new Monitor(fair);
174177
notEmpty =
@@ -202,7 +205,7 @@ public boolean isSatisfied() {
202205
*/
203206
public MonitorBasedArrayBlockingQueue(int capacity, boolean fair, Collection<? extends E> c) {
204207
this(capacity, fair);
205-
if (capacity < c.size()) throw new IllegalArgumentException();
208+
checkArgument(capacity >= c.size());
206209

207210
for (E e : c) add(e);
208211
}
@@ -239,7 +242,7 @@ public boolean add(E e) {
239242
@CanIgnoreReturnValue
240243
@Override
241244
public boolean offer(E e) {
242-
if (e == null) throw new NullPointerException();
245+
checkNotNull(e);
243246
Monitor monitor = this.monitor;
244247
if (monitor.enterIf(notFull)) {
245248
try {
@@ -263,8 +266,7 @@ public boolean offer(E e) {
263266
@CanIgnoreReturnValue
264267
@Override
265268
public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException {
266-
267-
if (e == null) throw new NullPointerException();
269+
checkNotNull(e);
268270
Monitor monitor = this.monitor;
269271
if (monitor.enterWhen(notFull, timeout, unit)) {
270272
try {
@@ -287,7 +289,7 @@ public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedExcepti
287289
*/
288290
@Override
289291
public void put(E e) throws InterruptedException {
290-
if (e == null) throw new NullPointerException();
292+
checkNotNull(e);
291293
Monitor monitor = this.monitor;
292294
monitor.enterWhen(notFull);
293295
try {
@@ -589,8 +591,8 @@ public void clear() {
589591
@CanIgnoreReturnValue
590592
@Override
591593
public int drainTo(Collection<? super E> c) {
592-
if (c == null) throw new NullPointerException();
593-
if (c == this) throw new IllegalArgumentException();
594+
checkNotNull(c);
595+
checkArgument(c != this);
594596
E[] items = this.items;
595597
Monitor monitor = this.monitor;
596598
monitor.enter();
@@ -624,8 +626,8 @@ public int drainTo(Collection<? super E> c) {
624626
@CanIgnoreReturnValue
625627
@Override
626628
public int drainTo(Collection<? super E> c, int maxElements) {
627-
if (c == null) throw new NullPointerException();
628-
if (c == this) throw new IllegalArgumentException();
629+
checkNotNull(c);
630+
checkArgument(c != this);
629631
if (maxElements <= 0) return 0;
630632
E[] items = this.items;
631633
Monitor monitor = this.monitor;

android/guava-tests/benchmark/com/google/common/util/concurrent/MonitorBasedPriorityBlockingQueue.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.common.util.concurrent;
1818

19+
import static com.google.common.base.Preconditions.checkArgument;
1920
import static com.google.common.base.Preconditions.checkNotNull;
2021

2122
import com.google.common.annotations.GwtIncompatible;
@@ -442,8 +443,8 @@ public String toString() {
442443
@CanIgnoreReturnValue // pushed down from class to method
443444
@Override
444445
public int drainTo(Collection<? super E> c) {
445-
if (c == null) throw new NullPointerException();
446-
if (c == this) throw new IllegalArgumentException();
446+
checkNotNull(c);
447+
checkArgument(c != this);
447448
Monitor monitor = this.monitor;
448449
monitor.enter();
449450
try {
@@ -468,8 +469,8 @@ public int drainTo(Collection<? super E> c) {
468469
@CanIgnoreReturnValue // pushed down from class to method
469470
@Override
470471
public int drainTo(Collection<? super E> c, int maxElements) {
471-
if (c == null) throw new NullPointerException();
472-
if (c == this) throw new IllegalArgumentException();
472+
checkNotNull(c);
473+
checkArgument(c != this);
473474
if (maxElements <= 0) return 0;
474475
Monitor monitor = this.monitor;
475476
monitor.enter();

android/guava/src/com/google/common/collect/MapMakerInternalMap.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
package com.google.common.collect;
1616

17+
import static com.google.common.base.Preconditions.checkArgument;
1718
import static com.google.common.base.Preconditions.checkNotNull;
1819
import static com.google.common.collect.CollectPreconditions.checkRemove;
1920
import static java.lang.Math.min;
@@ -243,9 +244,8 @@ private MapMakerInternalMap(MapMaker builder, InternalEntryHelper<K, V, E, S> en
243244
&& builder.getValueStrength() == Strength.STRONG) {
244245
return new MapMakerInternalMap<>(builder, WeakKeyDummyValueEntry.Helper.instance());
245246
}
246-
if (builder.getValueStrength() == Strength.WEAK) {
247-
throw new IllegalArgumentException("Map cannot have both weak and dummy values");
248-
}
247+
checkArgument(
248+
builder.getValueStrength() != Strength.WEAK, "Map cannot have both weak and dummy values");
249249
throw new AssertionError();
250250
}
251251

android/guava/src/com/google/common/io/ByteStreams.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -493,9 +493,7 @@ public static ByteArrayDataOutput newDataOutput() {
493493
public static ByteArrayDataOutput newDataOutput(int size) {
494494
// When called at high frequency, boxing size generates too much garbage,
495495
// so avoid doing that if we can.
496-
if (size < 0) {
497-
throw new IllegalArgumentException(String.format("Invalid size: %s", size));
498-
}
496+
checkArgument(size >= 0, "Invalid size: %s", size);
499497
return newDataOutput(new ByteArrayOutputStream(size));
500498
}
501499

android/guava/src/com/google/common/math/Quantiles.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -489,10 +489,10 @@ private static double interpolate(double lower, double upper, double remainder,
489489
}
490490

491491
private static void checkIndex(int index, int scale) {
492-
if (index < 0 || index > scale) {
493-
throw new IllegalArgumentException(
494-
"Quantile indexes must be between 0 and the scale, which is " + scale);
495-
}
492+
checkArgument(
493+
index >= 0 && index <= scale,
494+
"Quantile indexes must be between 0 and the scale, which is %s",
495+
scale);
496496
}
497497

498498
private static double[] longsToDoubles(long[] longs) {

0 commit comments

Comments
 (0)