Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.google.common.io.ByteStreams.newDataInput;
import static com.google.common.io.ByteStreams.newDataOutput;
import static com.google.common.truth.Truth.assertThat;
import static java.lang.Math.min;
import static java.lang.System.arraycopy;
import static java.nio.charset.StandardCharsets.US_ASCII;
import static java.nio.charset.StandardCharsets.UTF_16;
Expand Down Expand Up @@ -469,7 +470,7 @@ private static class SlowSkipper extends FilterInputStream {

@Override
public long skip(long n) throws IOException {
return super.skip(Math.min(max, n));
return super.skip(min(max, n));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static com.google.common.base.Strings.repeat;
import static com.google.common.truth.Truth.assertThat;
import static java.lang.Math.max;
import static org.junit.Assert.assertThrows;

import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -295,7 +296,7 @@ public int read(char[] cbuf, int off, int len) throws IOException {
}
// read fewer than the max number of chars to read
// shouldn't be a problem unless the buffer is shrinking each call
return in.read(cbuf, off, Math.max(len - 1024, 0));
return in.read(cbuf, off, max(len - 1024, 0));
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.common.annotations.GwtIncompatible;
import com.google.common.annotations.J2ktIncompatible;
import java.io.Serializable;
import java.util.Objects;
import org.jspecify.annotations.Nullable;

/**
Expand Down Expand Up @@ -66,7 +67,7 @@ public boolean equals(@Nullable Object obj) {

@Override
public int hashCode() {
return Objects.hashCode(function, resultEquivalence);
return Objects.hash(function, resultEquivalence);
}

@Override
Expand Down
5 changes: 3 additions & 2 deletions android/guava/src/com/google/common/base/Predicate.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package com.google.common.base;

import com.google.common.annotations.GwtCompatible;
import java.util.Objects;
import org.jspecify.annotations.Nullable;

/**
Expand Down Expand Up @@ -52,8 +53,8 @@ public interface Predicate<T extends @Nullable Object> {
*
* <ul>
* <li>Its execution does not cause any observable side effects.
* <li>The computation is <i>consistent with equals</i>; that is, {@link Objects#equal
* Objects.equal}{@code (a, b)} implies that {@code predicate.apply(a) ==
* <li>The computation is <i>consistent with equals</i>; that is, {@link Objects#equals
* Objects.equals}{@code (a, b)} implies that {@code predicate.apply(a) ==
* predicate.apply(b))}.
* </ul>
*
Expand Down
30 changes: 11 additions & 19 deletions android/guava/src/com/google/common/base/Predicates.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
package com.google.common.base;

import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.Arrays.asList;

import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.annotations.J2ktIncompatible;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -133,7 +133,7 @@ public final class Predicates {
*/
public static <T extends @Nullable Object> Predicate<T> and(
Predicate<? super T> first, Predicate<? super T> second) {
return new AndPredicate<>(Predicates.asList(checkNotNull(first), checkNotNull(second)));
return new AndPredicate<>(asList(checkNotNull(first), checkNotNull(second)));
}

/**
Expand Down Expand Up @@ -173,7 +173,7 @@ public final class Predicates {
*/
public static <T extends @Nullable Object> Predicate<T> or(
Predicate<? super T> first, Predicate<? super T> second) {
return new OrPredicate<>(Predicates.asList(checkNotNull(first), checkNotNull(second)));
return new OrPredicate<>(asList(checkNotNull(first), checkNotNull(second)));
}

/**
Expand All @@ -185,9 +185,7 @@ public final class Predicates {
* serializable.
*/
public static <T extends @Nullable Object> Predicate<T> equalTo(@ParametricNullness T target) {
return (target == null)
? Predicates.isNull()
: new IsEqualToPredicate(target).withNarrowedType();
return target == null ? isNull() : new IsEqualToPredicate(target).withNarrowedType();
}

/**
Expand Down Expand Up @@ -413,9 +411,9 @@ public String toString() {
*/
private static final class AndPredicate<T extends @Nullable Object>
implements Predicate<T>, Serializable {
private final List<? extends Predicate<? super T>> components;
private final List<Predicate<? super T>> components;

private AndPredicate(List<? extends Predicate<? super T>> components) {
private AndPredicate(List<Predicate<? super T>> components) {
this.components = components;
}

Expand Down Expand Up @@ -458,9 +456,9 @@ public String toString() {
*/
private static final class OrPredicate<T extends @Nullable Object>
implements Predicate<T>, Serializable {
private final List<? extends Predicate<? super T>> components;
private final List<Predicate<? super T>> components;

private OrPredicate(List<? extends Predicate<? super T>> components) {
private OrPredicate(List<Predicate<? super T>> components) {
this.components = components;
}

Expand Down Expand Up @@ -789,17 +787,11 @@ public String toString() {
@GwtIncompatible @J2ktIncompatible private static final long serialVersionUID = 0;
}

private static <T extends @Nullable Object> List<Predicate<? super T>> asList(
Predicate<? super T> first, Predicate<? super T> second) {
// TODO(kevinb): understand why we still get a warning despite @SafeVarargs!
return Arrays.asList(first, second);
}

private static <T> List<T> defensiveCopy(T... array) {
return defensiveCopy(Arrays.asList(array));
private static <T> List<T> defensiveCopy(T[] array) {
return defensiveCopy(asList(array));
}

static <T> List<T> defensiveCopy(Iterable<T> iterable) {
private static <T> List<T> defensiveCopy(Iterable<? extends T> iterable) {
ArrayList<T> list = new ArrayList<>();
for (T element : iterable) {
list.add(checkNotNull(element));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.CollectPreconditions.checkNonnegative;
import static com.google.common.collect.CollectPreconditions.checkRemove;

import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
Expand Down Expand Up @@ -197,7 +198,7 @@ public T next() {
@Override
public void remove() {
checkForConcurrentModification();
CollectPreconditions.checkRemove(toRemove != -1);
checkRemove(toRemove != -1);
size -= backingMap.removeEntry(toRemove);
entryIndex = backingMap.nextIndexAfterRemove(entryIndex, toRemove);
toRemove = -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import com.google.common.annotations.GwtCompatible;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.util.Iterator;
import org.jspecify.annotations.Nullable;

/** Precondition checks useful in collection implementations. */
Expand Down Expand Up @@ -50,14 +51,23 @@ static long checkNonnegative(long value, String name) {
return value;
}

/** A variant of {@link #checkNonnegative} that throws {@link IndexOutOfBoundsException}. */
@CanIgnoreReturnValue
static int checkNonnegativeIndex(int value, String name) {
if (value < 0) {
throw new IndexOutOfBoundsException(name + " cannot be negative but was: " + value);
}
return value;
}

static void checkPositive(int value, String name) {
if (value <= 0) {
throw new IllegalArgumentException(name + " must be positive but was: " + value);
}
}

/**
* Precondition tester for {@code Iterator.remove()} that throws an exception with a consistent
* Precondition tester for {@link Iterator#remove} that throws an exception with a consistent
* error message.
*/
static void checkRemove(boolean canRemove) {
Expand Down
3 changes: 2 additions & 1 deletion android/guava/src/com/google/common/collect/HashBiMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.CollectPreconditions.checkNonnegative;
import static com.google.common.collect.CollectPreconditions.checkRemove;
import static com.google.common.collect.Hashing.smearedHash;
import static com.google.common.collect.NullnessCasts.uncheckedCastNullableTToT;
import static com.google.common.collect.NullnessCasts.unsafeNull;
Expand Down Expand Up @@ -730,7 +731,7 @@ public T next() {
@Override
public void remove() {
checkForComodification();
CollectPreconditions.checkRemove(indexToRemove != ABSENT);
checkRemove(indexToRemove != ABSENT);
biMap.removeEntry(indexToRemove);
if (index == biMap.size) {
index = indexToRemove;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.CollectPreconditions.checkNonnegative;
import static com.google.common.collect.Lists.newArrayListWithCapacity;

import com.google.common.annotations.GwtIncompatible;
Expand Down Expand Up @@ -595,7 +596,7 @@ public Builder<E> add(E... elements) {
@Override
public Builder<E> addCopies(E element, int occurrences) {
checkNotNull(element);
CollectPreconditions.checkNonnegative(occurrences, "occurrences");
checkNonnegative(occurrences, "occurrences");
if (occurrences == 0) {
return this;
}
Expand All @@ -620,7 +621,7 @@ public Builder<E> addCopies(E element, int occurrences) {
@Override
public Builder<E> setCount(E element, int count) {
checkNotNull(element);
CollectPreconditions.checkNonnegative(count, "count");
checkNonnegative(count, "count");
maintenance();
elements[length] = element;
counts[length] = ~count;
Expand Down
4 changes: 2 additions & 2 deletions android/guava/src/com/google/common/collect/Iterables.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Predicates.instanceOf;
import static com.google.common.collect.CollectPreconditions.checkNonnegativeIndex;
import static com.google.common.collect.CollectPreconditions.checkRemove;
import static com.google.common.collect.Collections2.safeContains;
import static com.google.common.collect.Iterators.advance;
import static com.google.common.collect.Iterators.checkNonnegative;
import static com.google.common.collect.Iterators.consumingIterator;
import static com.google.common.collect.Iterators.getNext;
import static com.google.common.collect.Iterators.unmodifiableIterator;
Expand Down Expand Up @@ -807,7 +807,7 @@ public Iterator<T> iterator() {
public static <T extends @Nullable Object> T get(
Iterable<? extends T> iterable, int position, @ParametricNullness T defaultValue) {
checkNotNull(iterable);
checkNonnegative(position);
checkNonnegativeIndex(position, "position");
if (iterable instanceof List) {
List<? extends T> list = (List<? extends T>) iterable;
return (position < list.size()) ? list.get(position) : defaultValue;
Expand Down
11 changes: 3 additions & 8 deletions android/guava/src/com/google/common/collect/Iterators.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.base.Predicates.instanceOf;
import static com.google.common.collect.CollectPreconditions.checkNonnegativeIndex;
import static com.google.common.collect.CollectPreconditions.checkRemove;
import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.NullnessCasts.uncheckedCastNullableTToT;
Expand Down Expand Up @@ -842,7 +843,7 @@ T transform(@ParametricNullness F from) {
*/
@ParametricNullness
public static <T extends @Nullable Object> T get(Iterator<T> iterator, int position) {
checkNonnegative(position);
checkNonnegativeIndex(position, "position");
int skipped = advance(iterator, position);
if (!iterator.hasNext()) {
throw new IndexOutOfBoundsException(
Expand Down Expand Up @@ -870,17 +871,11 @@ T transform(@ParametricNullness F from) {
@ParametricNullness
public static <T extends @Nullable Object> T get(
Iterator<? extends T> iterator, int position, @ParametricNullness T defaultValue) {
checkNonnegative(position);
checkNonnegativeIndex(position, "position");
advance(iterator, position);
return getNext(iterator, defaultValue);
}

static void checkNonnegative(int position) {
if (position < 0) {
throw new IndexOutOfBoundsException("position (" + position + ") must not be negative");
}
}

/**
* Returns the next element in {@code iterator} or {@code defaultValue} if the iterator is empty.
* The {@link Iterables} analog to this method is {@link Iterables#getFirst}.
Expand Down
4 changes: 0 additions & 4 deletions android/guava/src/com/google/common/primitives/Ints.java
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,6 @@ public static int max(int... array) {
* @throws IllegalArgumentException if {@code min > max}
* @since 21.0
*/
// A call to bare "min" or "max" would resolve to our varargs method, not to any static import.
@SuppressWarnings("StaticImportPreferred")
public static int constrainToRange(int value, int min, int max) {
checkArgument(min <= max, "min (%s) must be less than or equal to max (%s)", min, max);
return Math.min(Math.max(value, min), max);
Expand Down Expand Up @@ -462,8 +460,6 @@ private enum LexicographicalComparator implements Comparator<int[]> {
INSTANCE;

@Override
// A call to bare "min" or "max" would resolve to our varargs method, not to any static import.
@SuppressWarnings("StaticImportPreferred")
public int compare(int[] left, int[] right) {
int minLength = Math.min(left.length, right.length);
for (int i = 0; i < minLength; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,6 @@ enum LexicographicalComparator implements Comparator<int[]> {
INSTANCE;

@Override
// A call to bare "min" or "max" would resolve to our varargs method, not to any static import.
@SuppressWarnings("StaticImportPreferred")
public int compare(int[] left, int[] right) {
int minLength = Math.min(left.length, right.length);
for (int i = 0; i < minLength; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

package com.google.thirdparty.publicsuffix;

import static java.lang.Math.min;

import com.google.common.annotations.GwtCompatible;
import com.google.common.collect.ImmutableList;
import java.util.List;
Expand Down Expand Up @@ -209,7 +211,7 @@ private int findChild(int firstChild, int numChildren, String label) {
private int compareLabel(String label, int offset) {
int labelLen = label.length();
int nodeLabelLen = stringPool.charAt(offset);
int minLen = Math.min(nodeLabelLen, labelLen);
int minLen = min(nodeLabelLen, labelLen);
for (int i = 0; i < minLen; i++) {
char c1 = label.charAt(i);
char c2 = stringPool.charAt(offset + 1 + i);
Expand Down
3 changes: 2 additions & 1 deletion guava-tests/test/com/google/common/io/ByteStreamsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.google.common.io.ByteStreams.newDataInput;
import static com.google.common.io.ByteStreams.newDataOutput;
import static com.google.common.truth.Truth.assertThat;
import static java.lang.Math.min;
import static java.lang.System.arraycopy;
import static java.nio.charset.StandardCharsets.US_ASCII;
import static java.nio.charset.StandardCharsets.UTF_16;
Expand Down Expand Up @@ -469,7 +470,7 @@ private static class SlowSkipper extends FilterInputStream {

@Override
public long skip(long n) throws IOException {
return super.skip(Math.min(max, n));
return super.skip(min(max, n));
}
}

Expand Down
3 changes: 2 additions & 1 deletion guava-tests/test/com/google/common/io/CharStreamsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static com.google.common.base.Strings.repeat;
import static com.google.common.truth.Truth.assertThat;
import static java.lang.Math.max;
import static org.junit.Assert.assertThrows;

import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -295,7 +296,7 @@ public int read(char[] cbuf, int off, int len) throws IOException {
}
// read fewer than the max number of chars to read
// shouldn't be a problem unless the buffer is shrinking each call
return in.read(cbuf, off, Math.max(len - 1024, 0));
return in.read(cbuf, off, max(len - 1024, 0));
}
};
}
Expand Down
Loading
Loading