Example usage for org.apache.commons.functor UnaryFunction evaluate

List of usage examples for org.apache.commons.functor UnaryFunction evaluate

Introduction

In this page you can find the example usage for org.apache.commons.functor UnaryFunction evaluate.

Prototype

T evaluate(A obj);

Source Link

Document

Evaluate this function.

Usage

From source file:com.aw.support.collection.ListUtils.java

public static void apply(Collection list, UnaryPredicate predicate, UnaryFunction function) {
    for (Object o : list) {
        if (predicate == null || predicate.test(o)) {
            function.evaluate(o);
        }/*from  w w  w .j a  va2  s.  c  om*/
    }
}

From source file:com.aw.support.collection.ListUtils.java

/**
 * Return a new list that contains the result to apply the unaryFunction to all the elements of the list
 *
 * @param list/*from  ww w. ja v  a2s.c o m*/
 * @param unaryFunction
 * @return
 */
public static List<Object> getList(Collection list, UnaryFunction unaryFunction) {
    List<Object> newList = new ArrayList<Object>();
    for (Iterator iterator = list.iterator(); iterator.hasNext();) {
        Object obj = iterator.next();
        newList.add(unaryFunction.evaluate(obj));

    }
    return newList;
}

From source file:org.mili.core.cache.DefaultCache.java

/**
 * Creates a cache from a collection./*from  ww  w.  j  ava  2 s. co m*/
 *
 * @param collection the collection
 * @param idFunction the id function
 * @return the created default cache
 */
public final static <A, B> DefaultCache<A, B> create(Collection<B> collection, UnaryFunction<B, A> idFunction) {
    DefaultCache<A, B> cache = new DefaultCache<A, B>();
    for (Iterator<B> i = collection.iterator(); i.hasNext();) {
        B b = i.next();
        cache.put(idFunction.evaluate(b), b);
    }
    return cache;
}

From source file:org.mili.core.collection.MapUtil.java

/**
 * Transforms a list with a transformation function to a map.
 *
 * @param l list with objects.//from  w  ww.  ja  v  a  2 s  .  c  o  m
 * @param f transformation function to get the key from an object.
 * @return transformed map based upon the list.
 * @throws ArrayStoreException is overwrite is false and duplicate keys occurs.
 * @throws IllegalStateException if error while using the transformation function occurs.
 */
public static <T, I> Map<I, T> listAsMap(boolean overwrite, List<T> l, UnaryFunction<T, I> f) {
    Validate.notNull(l, "list cannot be null!");
    Validate.notNull(f, "function cannot be null!");
    Map<I, T> m = new Hashtable<I, T>();
    try {
        for (int i = 0, n = l.size(); i < n; i++) {
            T o = l.get(i);
            I k = f.evaluate(o);
            if (m.containsKey(k) && !overwrite) {
                throw new ArrayStoreException("key " + k + " already exists in map!");
            }
            m.put(k, o);
        }
    } catch (ArrayStoreException e) {
        throw e;
    } catch (Exception e) {
        throw new IllegalStateException("exception while execute function occured!", e);
    }
    return m;
}