Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (C) 2009 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.util.Collections.sort;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;
import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.annotations.J2ktIncompatible;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import junit.framework.Assert;
import junit.framework.AssertionFailedError;
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 List copyToList(Iterable extends E> elements) {
List list = new ArrayList<>();
addAll(list, elements);
return list;
}
public static List copyToList(E[] elements) {
return copyToList(Arrays.asList(elements));
}
// Clone of Sets.newLinkedHashSet
public static Set copyToSet(Iterable extends E> elements) {
Set set = new LinkedHashSet<>();
addAll(set, elements);
return set;
}
public static Set copyToSet(E[] elements) {
return copyToSet(Arrays.asList(elements));
}
// Would use Maps.immutableEntry
public static Entry mapEntry(
K key, V value) {
return Collections.singletonMap(key, value).entrySet().iterator().next();
}
private static boolean isEmpty(Iterable> iterable) {
return iterable instanceof Collection
? ((Collection>) iterable).isEmpty()
: !iterable.iterator().hasNext();
}
public static void assertEmpty(Iterable> iterable) {
if (!isEmpty(iterable)) {
Assert.fail("Not true that " + iterable + " is empty");
}
}
public static void assertEmpty(Map, ?> map) {
if (!map.isEmpty()) {
Assert.fail("Not true that " + map + " is empty");
}
}
public static void assertEqualInOrder(Iterable> expected, Iterable> actual) {
Iterator> expectedIter = expected.iterator();
Iterator> actualIter = actual.iterator();
while (expectedIter.hasNext() && actualIter.hasNext()) {
if (!equal(expectedIter.next(), actualIter.next())) {
Assert.fail(
"contents were not equal and in the same order: "
+ "expected = "
+ expected
+ ", actual = "
+ actual);
}
}
if (expectedIter.hasNext() || actualIter.hasNext()) {
// actual either had too few or too many elements
Assert.fail(
"contents were not equal and in the same order: "
+ "expected = "
+ expected
+ ", actual = "
+ actual);
}
}
public static void assertContentsInOrder(Iterable> actual, Object... expected) {
assertEqualInOrder(Arrays.asList(expected), actual);
}
public static void assertEqualIgnoringOrder(Iterable> expected, Iterable> actual) {
List> exp = copyToList(expected);
List> act = copyToList(actual);
String actString = act.toString();
// Of course we could take pains to give the complete description of the
// problem on any failure.
// Yeah it's n^2.
for (Object object : exp) {
if (!act.remove(object)) {
Assert.fail(
"did not contain expected element "
+ object
+ ", "
+ "expected = "
+ exp
+ ", actual = "
+ actString);
}
}
assertTrue("unexpected elements: " + act, act.isEmpty());
}
public static void assertContentsAnyOrder(Iterable> actual, Object... expected) {
assertEqualIgnoringOrder(Arrays.asList(expected), actual);
}
public static void assertContains(Iterable> actual, Object expected) {
boolean contained = false;
if (actual instanceof Collection) {
contained = ((Collection>) actual).contains(expected);
} else {
for (Object o : actual) {
if (equal(o, expected)) {
contained = true;
break;
}
}
}
if (!contained) {
Assert.fail("Not true that " + actual + " contains " + expected);
}
}
public static void assertContainsAllOf(Iterable> actual, Object... expected) {
List