This example Java source code file (Helpers.java) is included in the alvinalexander.com
"Java Source Code
Warehouse" project. The intent of this project is to help you "Learn
Java by Example" TM.
/*
* 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 junit.framework.Assert;
import junit.framework.AssertionFailedError;
import java.io.Serializable;
import java.lang.reflect.Method;
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;
@GwtCompatible(emulated = true)
public class Helpers {
// Clone of Objects.equal
static boolean equal(Object a, Object b) {
return a == b || (a != null && a.equals(b));
}
// Clone of Lists.newArrayList
public static <E> List copyToList(Iterable extends E> elements) {
List<E> list = new ArrayList();
addAll(list, elements);
return list;
}
public static <E> List copyToList(E[] elements) {
return copyToList(Arrays.asList(elements));
}
// Clone of Sets.newLinkedHashSet
public static <E> Set copyToSet(Iterable extends E> elements) {
Set<E> set = new LinkedHashSet();
addAll(set, elements);
return set;
}
public static <E> Set copyToSet(E[] elements) {
return copyToSet(Arrays.asList(elements));
}
// Would use Maps.immutableEntry
public static <K, V> 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<Object> expectedList = new ArrayList
Other Java examples (source code examples)
Here is a short list of links related to this Java Helpers.java source code file: