
com.facebook.collectionsbase.Piles Maven / Gradle / Ivy
/*
* Copyright (C) 2012 Facebook, Inc.
*
* 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.facebook.collectionsbase;
import com.google.common.base.Function;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
/**
* utility methods for working with Piles (collections) of elements.
*/
public class Piles {
private Piles() {
throw new AssertionError();
}
/**
* works with guava's Function interface
*
* @param iterator
* @param function
* @param
* @param
* @return
*/
public static List transmogrify(Iterator iterator, Function function) {
com.facebook.collectionsbase.Mapper mapper = new com.facebook.collectionsbase.FunctionToMapper(function);
return transmogrify(iterator, mapper);
}
/**
* creates a list of type Y from an iterator of type X
*
* @param iterator
* @param mapper
* @param
* @param
* @return
*/
public static List transmogrify(Iterator iterator, Mapper mapper) {
List result = new ArrayList<>();
transmogrify(iterator, result, mapper);
return result;
}
/**
* real basic, just make the iterator into a list
*
* @param iterator
* @param
* @return
*/
public static List copyOf(Iterator iterator) {
List result = new ArrayList();
copyOf(iterator, result);
return result;
}
public static Collection transmogrify(
Iterator iterator, Collection target, Function function
) {
FunctionToMapper mapper = new FunctionToMapper<>(function);
transmogrify(iterator, target, mapper);
return target;
}
/**
* allows caller to provide a Collection to place the iterator into
* @param iterator
* @param target
* @param
* @return
*/
public static Collection transmogrify(
Iterator iterator, Collection target, Mapper mapper
) {
while (iterator.hasNext()) {
target.add(mapper.map(iterator.next()));
}
return target;
}
/**
* allows caller to provide a Collection to place the iterator into
* @param iterator
* @param target
* @param
* @return
*/
public static Collection copyOf(Iterator iterator, Collection target) {
while (iterator.hasNext()) {
target.add(iterator.next());
}
return target;
}
/**
*
* @param source input collection
* @param target collection containing filtered items
* @param filter
* @param item type
* @param collection type
* @param
* @return target
* @throws E
*/
public static , E extends Throwable> C filter(Collection source, C target, Filter filter)
throws E {
for (T item : source) {
if (filter.execute(item)) {
target.add(item);
}
}
return target;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy