Major cleanup of the interface and library in general

This commit is contained in:
Ben Gruver
2012-11-17 17:51:36 -08:00
parent bea9627ed7
commit 22c3185bb7
170 changed files with 2987 additions and 1974 deletions

View File

@ -0,0 +1,137 @@
/*
* Copyright 2012, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.jf.util;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.Ordering;
import com.google.common.primitives.Ints;
import javax.annotation.Nonnull;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.SortedSet;
public class CollectionUtils {
public static <T extends Comparable<? super T>> int compareAsList(@Nonnull Collection<? extends T> list1,
@Nonnull Collection<? extends T> list2) {
int res = Ints.compare(list1.size(), list2.size());
if (res != 0) return res;
Iterator<? extends T> elements2 = list2.iterator();
for (T element1: list1) {
res = element1.compareTo(elements2.next());
if (res != 0) return res;
}
return 0;
}
public static <T> int compareAsList(@Nonnull Comparator<? super T> elementComparator,
@Nonnull Collection<? extends T> list1,
@Nonnull Collection<? extends T> list2) {
int res = Ints.compare(list1.size(), list2.size());
if (res != 0) return res;
Iterator<? extends T> elements2 = list2.iterator();
for (T element1: list1) {
res = elementComparator.compare(element1, elements2.next());
if (res != 0) return res;
}
return 0;
}
public static <T> Comparator<Collection<? extends T>> listComparator(
@Nonnull final Comparator<? super T> elementComparator) {
return new Comparator<Collection<? extends T>>() {
@Override
public int compare(Collection<? extends T> list1, Collection<? extends T> list2) {
return compareAsList(elementComparator, list1, list2);
}
};
}
@Nonnull
private static <T> SortedSet<? extends T> toNaturalSortedSet(@Nonnull Collection<? extends T> collection) {
if (collection instanceof SortedSet) {
SortedSet<? extends T> sortedSet = (SortedSet<? extends T>)collection;
Comparator<?> comparator = sortedSet.comparator();
if (comparator == null || comparator.equals(Ordering.natural())) {
return sortedSet;
}
}
return ImmutableSortedSet.copyOf(collection);
}
@Nonnull
private static <T> SortedSet<? extends T> toSortedSet(@Nonnull Comparator<? super T> elementComparator,
@Nonnull Collection<? extends T> collection) {
if (collection instanceof SortedSet) {
SortedSet<? extends T> sortedSet = (SortedSet<? extends T>)collection;
Comparator<?> comparator = sortedSet.comparator();
if (comparator != null && comparator.equals(elementComparator)) {
return sortedSet;
}
}
return ImmutableSortedSet.copyOf(elementComparator, collection);
}
public static <T extends Comparable<T>> int compareAsSet(@Nonnull Collection<? extends T> set1,
@Nonnull Collection<? extends T> set2) {
int res = Ints.compare(set1.size(), set2.size());
if (res != 0) return res;
SortedSet<? extends T> sortedSet1 = toNaturalSortedSet(set1);
SortedSet<? extends T> sortedSet2 = toNaturalSortedSet(set2);
Iterator<? extends T> elements2 = set2.iterator();
for (T element1: set1) {
res = element1.compareTo(elements2.next());
if (res != 0) return res;
}
return 0;
}
public static <T> int compareAsSet(@Nonnull Comparator<? super T> elementComparator,
@Nonnull Collection<? extends T> list1,
@Nonnull Collection<? extends T> list2) {
int res = Ints.compare(list1.size(), list2.size());
if (res != 0) return res;
SortedSet<? extends T> set1 = toSortedSet(elementComparator, list1);
SortedSet<? extends T> set2 = toSortedSet(elementComparator, list2);
Iterator<? extends T> elements2 = set2.iterator();
for (T element1: set1) {
res = elementComparator.compare(element1, elements2.next());
if (res != 0) return res;
}
return 0;
}
}

View File

@ -31,6 +31,8 @@
package org.jf.util;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet;
import javax.annotation.Nonnull;
@ -39,11 +41,75 @@ import java.util.Comparator;
import java.util.Iterator;
import java.util.SortedSet;
public abstract class ImmutableSortedSetConverter<ImmutableItem, Item> {
public abstract class ImmutableConverter<ImmutableItem, Item> {
protected abstract boolean isImmutable(@Nonnull Item item);
@Nonnull protected abstract ImmutableItem makeImmutable(@Nonnull Item item);
@Nonnull
public ImmutableSortedSet<ImmutableItem> convert(@Nonnull Comparator<? super ImmutableItem> comparator,
@Nullable final Iterable<? extends Item> iterable) {
public ImmutableList<ImmutableItem> toList(@Nullable final Iterable<? extends Item> iterable) {
if (iterable == null) {
return ImmutableList.of();
}
boolean needsCopy = false;
if (iterable instanceof ImmutableList) {
for (Item element: iterable) {
if (!isImmutable(element)) {
needsCopy = true;
break;
}
}
} else {
needsCopy = true;
}
if (!needsCopy) {
return (ImmutableList<ImmutableItem>)iterable;
}
final Iterator<? extends Item> iter = iterable.iterator();
return ImmutableList.copyOf(new Iterator<ImmutableItem>() {
@Override public boolean hasNext() { return iter.hasNext(); }
@Override public ImmutableItem next() { return makeImmutable(iter.next()); }
@Override public void remove() { iter.remove(); }
});
}
@Nonnull
public ImmutableSet<ImmutableItem> toSet(@Nullable final Iterable<? extends Item> iterable) {
if (iterable == null) {
return ImmutableSet.of();
}
boolean needsCopy = false;
if (iterable instanceof ImmutableSet) {
for (Item element: iterable) {
if (!isImmutable(element)) {
needsCopy = true;
break;
}
}
} else {
needsCopy = true;
}
if (!needsCopy) {
return (ImmutableSet<ImmutableItem>)iterable;
}
final Iterator<? extends Item> iter = iterable.iterator();
return ImmutableSet.copyOf(new Iterator<ImmutableItem>() {
@Override public boolean hasNext() { return iter.hasNext(); }
@Override public ImmutableItem next() { return makeImmutable(iter.next()); }
@Override public void remove() { iter.remove(); }
});
}
@Nonnull
public ImmutableSortedSet<ImmutableItem> toSortedSet(@Nonnull Comparator<? super ImmutableItem> comparator,
@Nullable final Iterable<? extends Item> iterable) {
if (iterable == null) {
return ImmutableSortedSet.of();
}
@ -76,8 +142,8 @@ public abstract class ImmutableSortedSetConverter<ImmutableItem, Item> {
}
@Nonnull
public SortedSet<ImmutableItem> convert(@Nonnull Comparator<? super ImmutableItem> comparator,
@Nullable final SortedSet<? extends Item> sortedSet) {
public SortedSet<ImmutableItem> toSortedSet(@Nonnull Comparator<? super ImmutableItem> comparator,
@Nullable final SortedSet<? extends Item> sortedSet) {
if (sortedSet == null || sortedSet.size() == 0) {
return ImmutableSortedSet.of();
}
@ -91,7 +157,4 @@ public abstract class ImmutableSortedSetConverter<ImmutableItem, Item> {
return ArraySortedSet.of(comparator, newItems);
}
protected abstract boolean isImmutable(@Nonnull Item item);
@Nonnull protected abstract ImmutableItem makeImmutable(@Nonnull Item item);
}

View File

@ -1,89 +0,0 @@
/*
* Copyright 2012, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.jf.util;
import com.google.common.collect.ImmutableList;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Iterator;
/**
* This class converts a list of items to an immutable list of immutable items
*
* @param <ImmutableItem> The immutable version of the element
* @param <Item> The normal version of the element
*/
public abstract class ImmutableListConverter<ImmutableItem, Item> {
/**
* Converts a {@code List} of {@code Item}s to an {@code ImmutableList} of {@code ImmutableItem}s.
*
* If the provided list is already an ImmutableList of ImmutableItems, then the list is not copied and is returned
* as-is. If the list is null, an empty ImmutableList will be returned
*
* @param iterable The iterable of items to convert.
* @return An ImmutableList of ImmutableItem. If list is null, an empty list will be returned.
*/
@Nonnull
public ImmutableList<ImmutableItem> convert(@Nullable final Iterable<? extends Item> iterable) {
if (iterable == null) {
return ImmutableList.of();
}
boolean needsCopy = false;
if (iterable instanceof ImmutableList) {
for (Item element: iterable) {
if (!isImmutable(element)) {
needsCopy = true;
break;
}
}
} else {
needsCopy = true;
}
if (!needsCopy) {
return (ImmutableList<ImmutableItem>)iterable;
}
final Iterator<? extends Item> iter = iterable.iterator();
return ImmutableList.copyOf(new Iterator<ImmutableItem>() {
@Override public boolean hasNext() { return iter.hasNext(); }
@Override public ImmutableItem next() { return makeImmutable(iter.next()); }
@Override public void remove() { iter.remove(); }
});
}
protected abstract boolean isImmutable(@Nonnull Item item);
@Nonnull protected abstract ImmutableItem makeImmutable(@Nonnull Item item);
}

View File

@ -32,6 +32,7 @@
package org.jf.util;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet;
import javax.annotation.Nonnull;
@ -45,6 +46,13 @@ public class ImmutableUtils {
return list;
}
@Nonnull public static <T> ImmutableSet<T> nullToEmptySet(@Nullable ImmutableSet<T> set) {
if (set == null) {
return ImmutableSet.of();
}
return set;
}
@Nonnull public static <T> ImmutableSortedSet<T> nullToEmptySortedSet(@Nullable ImmutableSortedSet<T> set) {
if (set == null) {
return ImmutableSortedSet.of();