All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.googlecode.common.client.util.CollectionsUtil Maven / Gradle / Ivy

There is a newer version: 1.0.5
Show newest version

package com.googlecode.common.client.util;

import java.util.ArrayList;
import java.util.List;


public final class CollectionsUtil {

    public interface Predicate

{ public boolean predicate(P object); } public interface Function { public R exec(T object); } private CollectionsUtil() { } public static List addToList(List list, T data) { if (list == null) { list = new ArrayList(); } list.add(data); return list; } public static List addAllToList(List list, List data) { if (list == null) { list = new ArrayList(); } list.addAll(data); return list; } /** * Selects first element which satisfies predicate */ public static T first(List list, Predicate

predicate) { for (T e : list) { if (predicate.predicate(e)) { return e; } } return null; } /** * Tests whether a predicate holds for some of the elements of the list */ public static boolean exists(List list, Predicate

predicate) { return first(list, predicate) != null; } /** * Selects all elements which satisfy a predicate * * @return new {@code List} containing filtered elements */ public static List filter(List list, Predicate

predicate) { List filteredList = new ArrayList(); for (T e : list) { if (predicate.predicate(e)) { filteredList.add(e); } } return filteredList; } /** * Builds a new List by applying a function to all elements of this list * @return new {@code List} containing elements returned by {@code func} */ public static List map(List list, Function func) { List result = new ArrayList(); for (T e : list) { result.add(func.exec(e)); } return result; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy