Skip to content

Commit

Permalink
Internal build setup change
Browse files Browse the repository at this point in the history
RELNOTES=n/a
PiperOrigin-RevId: 538188470
  • Loading branch information
stefanhaustein authored and Google Java Core Libraries committed Jun 6, 2023
1 parent c03136c commit 6135e9d
Show file tree
Hide file tree
Showing 27 changed files with 200 additions and 51 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (C) 2021 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.common.collect.testing;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import com.google.common.annotations.GwtCompatible;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import javax.annotation.Nonnull;
import javax.annotation.meta.TypeQualifierDefault;

/**
* Marks all "top-level" types as non-null in a way that is recognized by Kotlin. Note that this
* unfortunately includes type-variable usages, so we also provide {@link ParametricNullness} to
* "undo" it as best we can.
*/
@GwtCompatible
@Retention(RUNTIME)
@Target(TYPE)
@TypeQualifierDefault({FIELD, METHOD, PARAMETER})
@Nonnull
@interface ElementTypesAreNonnullByDefault {}
Original file line number Diff line number Diff line change
Expand Up @@ -45,36 +45,38 @@
import org.checkerframework.checker.nullness.qual.Nullable;

@GwtCompatible(emulated = true)
@ElementTypesAreNonnullByDefault
public class Helpers {
// Clone of Objects.equal
static boolean equal(@Nullable Object a, @Nullable Object b) {
return a == b || (a != null && a.equals(b));
}

// Clone of Lists.newArrayList
public static <E> List<E> copyToList(Iterable<? extends E> elements) {
public static <E extends @Nullable Object> List<E> copyToList(Iterable<? extends E> elements) {
List<E> list = new ArrayList<>();
addAll(list, elements);
return list;
}

public static <E> List<E> copyToList(E[] elements) {
public static <E extends @Nullable Object> List<E> copyToList(E[] elements) {
return copyToList(Arrays.asList(elements));
}

// Clone of Sets.newLinkedHashSet
public static <E> Set<E> copyToSet(Iterable<? extends E> elements) {
public static <E extends @Nullable Object> Set<E> copyToSet(Iterable<? extends E> elements) {
Set<E> set = new LinkedHashSet<>();
addAll(set, elements);
return set;
}

public static <E> Set<E> copyToSet(E[] elements) {
public static <E extends @Nullable Object> Set<E> copyToSet(E[] elements) {
return copyToSet(Arrays.asList(elements));
}

// Would use Maps.immutableEntry
public static <K, V> Entry<K, V> mapEntry(K key, V value) {
public static <K extends @Nullable Object, V extends @Nullable Object> Entry<K, V> mapEntry(
K key, V value) {
return Collections.singletonMap(key, value).entrySet().iterator().next();
}

Expand Down Expand Up @@ -185,7 +187,8 @@ public static void assertContainsAllOf(Iterable<?> actual, Object... expected) {
}

@CanIgnoreReturnValue
public static <E> boolean addAll(Collection<E> addTo, Iterable<? extends E> elementsToAdd) {
public static <E extends @Nullable Object> boolean addAll(
Collection<E> addTo, Iterable<? extends E> elementsToAdd) {
boolean modified = false;
for (E e : elementsToAdd) {
modified |= addTo.add(e);
Expand Down Expand Up @@ -255,7 +258,8 @@ static void fail(Throwable cause, Object message) {
throw assertionFailedError;
}

private static class EntryComparator<K, V> implements Comparator<Entry<K, V>> {
private static class EntryComparator<K extends @Nullable Object, V extends @Nullable Object>
implements Comparator<Entry<K, V>> {
final @Nullable Comparator<? super K> keyComparator;

public EntryComparator(@Nullable Comparator<? super K> keyComparator) {
Expand Down Expand Up @@ -303,7 +307,7 @@ public static <T> void testComparator(
* valuesInExpectedOrder.get(i)} and {@code tj = valuesInExpectedOrder.get(j)}.
* </ul>
*/
public static <T> void testComparator(
public static <T extends @Nullable Object> void testComparator(
Comparator<? super T> comparator, List<T> valuesInExpectedOrder) {
// This does an O(n^2) test of all pairs of values in both orders
for (int i = 0; i < valuesInExpectedOrder.size(); i++) {
Expand Down Expand Up @@ -358,7 +362,7 @@ public static <T extends Comparable<? super T>> void testCompareToAndEquals(
* @param delta the difference between the true size of the collection and the values returned by
* the size method
*/
public static <T> Collection<T> misleadingSizeCollection(int delta) {
public static <T extends @Nullable Object> Collection<T> misleadingSizeCollection(int delta) {
// It would be nice to be able to return a real concurrent
// collection like ConcurrentLinkedQueue, so that e.g. concurrent
// iteration would work, but that would not be GWT-compatible.
Expand Down Expand Up @@ -397,12 +401,7 @@ public T remove(int index) {
}

@Override
public <T> T[] toArray(T[] a) {
return data.toArray(a);
}

@Override
public Object[] toArray() {
public @Nullable Object[] toArray() {
return data.toArray();
}
};
Expand All @@ -414,7 +413,8 @@ public Object[] toArray() {
* equals. This is used for testing unmodifiable collections of map entries; for example, it
* should not be possible to access the raw (modifiable) map entry via a nefarious equals method.
*/
public static <K, V> Entry<K, V> nefariousMapEntry(K key, V value) {
public static <K extends @Nullable Object, V extends @Nullable Object>
Entry<K, V> nefariousMapEntry(K key, V value) {
return new Entry<K, V>() {
@Override
public K getKey() {
Expand Down Expand Up @@ -457,7 +457,7 @@ public String toString() {
};
}

static <E> List<E> castOrCopyToList(Iterable<E> iterable) {
static <E extends @Nullable Object> List<E> castOrCopyToList(Iterable<E> iterable) {
if (iterable instanceof List) {
return (List<E>) iterable;
}
Expand All @@ -477,6 +477,7 @@ public int compare(Comparable left, Comparable right) {
}
};

@J2ktIncompatible
public static <K extends Comparable, V> Iterable<Entry<K, V>> orderEntriesByKey(
List<Entry<K, V>> insertionOrder) {
sort(insertionOrder, Helpers.<K, V>entryComparator(NATURAL_ORDER));
Expand All @@ -495,7 +496,7 @@ public static <K extends Comparable, V> Iterable<Entry<K, V>> orderEntriesByKey(
* values, it lies outside the submap/submultiset ranges we test, and the variety of tests that
* exercise null handling fail on those subcollections.
*/
public abstract static class NullsBefore implements Comparator<String>, Serializable {
public abstract static class NullsBefore implements Comparator<@Nullable String>, Serializable {
/*
* We don't serialize this class in GWT, so we don't care about whether GWT will serialize this
* field.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@
import java.util.Comparator;
import java.util.List;
import junit.framework.TestCase;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
* Unit test for {@link Booleans}.
*
* @author Kevin Bourrillion
*/
@GwtCompatible(emulated = true)
@ElementTypesAreNonnullByDefault
public class BooleansTest extends TestCase {
private static final boolean[] EMPTY = {};
private static final boolean[] ARRAY_FALSE = {false};
Expand All @@ -47,6 +49,7 @@ public class BooleansTest extends TestCase {

private static final boolean[] VALUES = {false, true};

@J2ktIncompatible // TODO(b/285538920): Fix and enable.
public void testHashCode() {
assertThat(Booleans.hashCode(true)).isEqualTo(Boolean.TRUE.hashCode());
assertThat(Booleans.hashCode(false)).isEqualTo(Boolean.FALSE.hashCode());
Expand Down Expand Up @@ -478,7 +481,7 @@ public void testToArray_threadSafe() {
}

public void testToArray_withNull() {
List<Boolean> list = Arrays.asList(false, true, null);
List<@Nullable Boolean> list = Arrays.asList(false, true, null);
try {
Booleans.toArray(list);
fail();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@
import java.util.List;
import java.util.Locale;
import junit.framework.TestCase;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
* Unit test for {@link Chars}.
*
* @author Kevin Bourrillion
*/
@GwtCompatible(emulated = true)
@ElementTypesAreNonnullByDefault
@SuppressWarnings("cast") // redundant casts are intentional and harmless
public class CharsTest extends TestCase {
private static final char[] EMPTY = {};
Expand Down Expand Up @@ -87,6 +89,7 @@ private void assertCastFails(long value) {
}
}

@J2ktIncompatible // TODO(b/285538920): Fix and enable.
public void testCompare() {
for (char x : VALUES) {
for (char y : VALUES) {
Expand Down Expand Up @@ -625,14 +628,15 @@ public void testToArray_threadSafe() {
}

public void testToArray_withNull() {
List<Character> list = Arrays.asList((char) 0, (char) 1, null);
List<@Nullable Character> list = Arrays.asList((char) 0, (char) 1, null);
try {
Chars.toArray(list);
fail();
} catch (NullPointerException expected) {
}
}

@J2ktIncompatible // b/285319375
public void testAsList_isAView() {
char[] array = {(char) 0, (char) 1};
List<Character> list = Chars.asList(array);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
*
* @author Kevin Bourrillion
*/
@ElementTypesAreNonnullByDefault
@GwtCompatible(emulated = true)
public class DoublesTest extends TestCase {
private static final double[] EMPTY = {};
Expand Down Expand Up @@ -550,7 +551,7 @@ public void testToArray_threadSafe() {
}

public void testToArray_withNull() {
List<Double> list = Arrays.asList((double) 0, (double) 1, null);
List<@Nullable Double> list = Arrays.asList((double) 0, (double) 1, null);
try {
Doubles.toArray(list);
fail();
Expand All @@ -576,6 +577,7 @@ public void testToArray_withConversion() {
assertThat(Doubles.toArray(doubles)).isEqualTo(array);
}

@J2ktIncompatible // b/285319375
public void testAsList_isAView() {
double[] array = {(double) 0, (double) 1};
List<Double> list = Doubles.asList(array);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
*
* @author Kevin Bourrillion
*/
@ElementTypesAreNonnullByDefault
@GwtCompatible(emulated = true)
public class FloatsTest extends TestCase {
private static final float[] EMPTY = {};
Expand Down Expand Up @@ -529,7 +530,7 @@ public void testToArray_threadSafe() {
}

public void testToArray_withNull() {
List<Float> list = Arrays.asList((float) 0, (float) 1, null);
List<@Nullable Float> list = Arrays.asList((float) 0, (float) 1, null);
try {
Floats.toArray(list);
fail();
Expand All @@ -555,6 +556,7 @@ public void testToArray_withConversion() {
assertThat(Floats.toArray(doubles)).isEqualTo(array);
}

@J2ktIncompatible // b/285319375
public void testAsList_isAView() {
float[] array = {(float) 0, (float) 1};
List<Float> list = Floats.asList(array);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@
import junit.framework.TestCase;
import junit.framework.TestSuite;

/** @author Kevin Bourrillion */
/**
* @author Kevin Bourrillion
*/
@GwtCompatible(emulated = true)
@ElementTypesAreNonnullByDefault
public class ImmutableDoubleArrayTest extends TestCase {
// Test all creation paths very lazily: by assuming asList() works

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@
import java.util.List;
import java.util.Random;
import junit.framework.TestCase;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
* Unit test for {@link Ints}.
*
* @author Kevin Bourrillion
*/
@GwtCompatible(emulated = true)
@ElementTypesAreNonnullByDefault
@SuppressWarnings("cast") // redundant casts are intentional and harmless
public class IntsTest extends TestCase {
private static final int[] EMPTY = {};
Expand Down Expand Up @@ -511,7 +513,7 @@ public void testToArray_threadSafe() {
}

public void testToArray_withNull() {
List<Integer> list = Arrays.asList((int) 0, (int) 1, null);
List<@Nullable Integer> list = Arrays.asList((int) 0, (int) 1, null);
try {
Ints.toArray(list);
fail();
Expand All @@ -537,6 +539,7 @@ public void testToArray_withConversion() {
assertThat(Ints.toArray(doubles)).isEqualTo(array);
}

@J2ktIncompatible // b/285319375
public void testAsList_isAView() {
int[] array = {(int) 0, (int) 1};
List<Integer> list = Ints.asList(array);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@
import java.util.List;
import java.util.Random;
import junit.framework.TestCase;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
* Unit test for {@link Longs}.
*
* @author Kevin Bourrillion
*/
@ElementTypesAreNonnullByDefault
@GwtCompatible(emulated = true)
@SuppressWarnings("cast") // redundant casts are intentional and harmless
public class LongsTest extends TestCase {
Expand Down Expand Up @@ -522,7 +524,7 @@ public void testToArray_threadSafe() {
}

public void testToArray_withNull() {
List<Long> list = Arrays.asList((long) 0, (long) 1, null);
List<@Nullable Long> list = Arrays.asList((long) 0, (long) 1, null);
try {
Longs.toArray(list);
fail();
Expand All @@ -548,6 +550,7 @@ public void testToArray_withConversion() {
assertThat(Longs.toArray(doubles)).isEqualTo(array);
}

@J2ktIncompatible // b/285319375
public void testAsList_isAView() {
long[] array = {(long) 0, (long) 1};
List<Long> list = Longs.asList(array);
Expand Down
Loading

0 comments on commit 6135e9d

Please sign in to comment.