public static <X, Y> Collection<Y> transmogrify( Iterator<X> iterator, Collection<Y> target, Function<X, Y> function ) { FunctionToMapper<X, Y> mapper = new FunctionToMapper<>(function); transmogrify(iterator, target, mapper); return target; } /**
public static <T extends Comparable<? super T>> int compareLists( List<? extends T> list1, List<? extends T> list2 ) { Comparator<T> comparator = new Comparator<T>() { @Override public int compare(T o1, T o2) { return o1.compareTo(o2); } }; return compareLists(list1, list2, comparator); }
public static <T extends Comparable<? super T>> int compareArrays( T[] array1, T[] array2 ) { Comparator<T> comparator = new Comparator<T>() { @Override public int compare(T o1, T o2) { return o1.compareTo(o2); } }; return compareArrays( array1, array2, comparator); }
@Override public Y next() { X input = iterator.next(); return mapper.map(input); }
@BeforeMethod(alwaysRun = true) public void setUp() throws Exception { hasValue = new ReferenceCourier<>("fuu"); empty1 = new ReferenceCourier<>(null); empty2 = ReferenceCourier.empty(); }
public static <T2> ReferenceCourier<T2> empty() { return new ReferenceCourier<>(null); }
/** * real basic, just make the iterator into a list * * @param iterator * @param <T> * @return */ public static <T> List<T> copyOf(Iterator<T> iterator) { List<T> result = new ArrayList<T>(); copyOf(iterator, result); return result; }
@Test(groups = {"fast", "local"}, expectedExceptions = NullPointerException.class) public void testempty2Get() throws Exception { empty2.get(); } }
@Test(groups = {"fast", "local"}) public void testempty2IsSet() throws Exception { Assert.assertFalse(empty2.isSet()); }
/** * sorts in ocmparator order, usually ascending * * @param array1 * @param array2 * @param <T> * @return */ public static <T extends Comparable<? super T>> int compareArrays(T[] array1, T[] array2) { Comparator<T> comparator = new Comparator<T>() { @Override public int compare(T o1, T o2) { return o1.compareTo(o2); } }; return compareArrays(array1, array2, comparator); }
/** * creates a list of type Y from an iterator of type X * * @param iterator * @param mapper * @param <X> * @param <Y> * @return */ public static <X, Y> List<Y> transmogrify(Iterator<X> iterator, Mapper<X, Y> mapper) { List<Y> result = new ArrayList<>(); transmogrify(iterator, result, mapper); return result; }
/** * * @param source input collection * @param target collection containing filtered items * @param filter * @param <T> item type * @param <C> collection type * @param <E> * @return target * @throws E */ public static <T, C extends Collection<T>, E extends Throwable> C filter(Collection<T> source, C target, Filter<T,E> filter) throws E { for (T item : source) { if (filter.execute(item)) { target.add(item); } } return target; } }
private int hashValueOf(Long aLong) { return Math.abs(hashFunction.map(aLong)) % values.length; }
@Test(groups = "fast") public void testSanity() throws Exception { Assert.assertEquals(Lists.compareLists(list1, list1), 0); Assert.assertEquals(Lists.compareLists(list1, list2), -1); Assert.assertEquals(Lists.compareLists(list1, list3), -1); Assert.assertEquals(Lists.compareLists(list2, list1), 1); Assert.assertEquals(Lists.compareLists(list2, list2), 0); Assert.assertEquals(Lists.compareLists(list2, list3), 1); Assert.assertEquals(Lists.compareLists(list3, list1), 1); Assert.assertEquals(Lists.compareLists(list3, list2), -1); Assert.assertEquals(Lists.compareLists(list3, list3), 0); } }
/** * works with guava's Function interface * * @param iterator * @param function * @param <X> * @param <Y> * @return */ public static <X, Y> List<Y> transmogrify(Iterator<X> iterator, Function<X, Y> function) { com.facebook.collectionsbase.Mapper<X, Y> mapper = new com.facebook.collectionsbase.FunctionToMapper<X, Y>(function); return transmogrify(iterator, mapper); }
@Test(groups = {"fast", "local"}, expectedExceptions = NullPointerException.class) public void testEmpty1Get() throws Exception { empty1.get(); }
@Test(groups = {"fast", "local"}) public void testEmpty1IsSet() throws Exception { Assert.assertFalse(empty1.isSet()); }
public static <X,Y> List<Y> map(List<X> list, Mapper<X, Y> mapper) { List<Y> result = new ArrayList<Y>(); for (X item : list) { result.add(mapper.map(item)); } return result; } }
/** * allows caller to provide a Collection to place the iterator into * @param iterator * @param target * @param <T> * @return */ public static <X, Y> Collection<Y> transmogrify( Iterator<X> iterator, Collection<Y> target, Mapper<X, Y> mapper ) { while (iterator.hasNext()) { target.add(mapper.map(iterator.next())); } return target; }