From 165e5c3a780a7a1616d3231c3cdfb3590999cd46 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 17 May 2009 22:02:52 +0200 Subject: [PATCH] Add some common collection filters. --- src/net/pterodactylus/util/filter/Filters.java | 191 +++++++++++++++++++++++++ 1 file changed, 191 insertions(+) create mode 100644 src/net/pterodactylus/util/filter/Filters.java diff --git a/src/net/pterodactylus/util/filter/Filters.java b/src/net/pterodactylus/util/filter/Filters.java new file mode 100644 index 0000000..fe3f12f --- /dev/null +++ b/src/net/pterodactylus/util/filter/Filters.java @@ -0,0 +1,191 @@ +/* + * jFCPlib - Filters.java - + * Copyright © 2009 David Roden + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ +package net.pterodactylus.util.filter; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.NoSuchElementException; +import java.util.Set; +import java.util.Map.Entry; + +/** + * Defines various methods to filter {@link Collection}s. + * + * @author David ‘Bombe’ Roden <bombe@freenetproject.org> + */ +public class Filters { + + /** + * Returns a list that contains only the elements from the given list that + * match the given filter. + * + * @param + * The type of the list elements + * @param list + * The list to filter + * @param listFilter + * The list filter + * @return The filtered list + */ + public static List filteredList(List list, Filter listFilter) { + List filteredList = new ArrayList(); + for (E element : list) { + if (listFilter.filterObject(element)) { + filteredList.add(element); + } + } + return filteredList; + } + + /** + * Returns a set that contains only the elements from the given set that + * match the given filter. + * + * @param + * The type of the set elements + * @param set + * The set to filter + * @param setFilter + * The set filter + * @return The filtered set + */ + public static Set filteredSet(Set set, Filter setFilter) { + Set filteredSet = new HashSet(); + for (E element : set) { + if (setFilter.filterObject(element)) { + filteredSet.add(element); + } + } + return filteredSet; + } + + /** + * Returns a map that contains only the elements from the given map that + * match the given filter. + * + * @param + * The type of the map keys + * @param + * The type of the map values + * @param map + * The map to filter + * @param mapFilter + * The map filter + * @return The filtered map + */ + public static Map filteredMap(Map map, Filter> mapFilter) { + Map filteredMap = new HashMap(); + for (Entry element : map.entrySet()) { + if (mapFilter.filterObject(element)) { + filteredMap.put(element.getKey(), element.getValue()); + } + } + return filteredMap; + } + + /** + * Returns a collection that contains only the elements from the given + * collection that match the given filter. + * + * @param + * The type of the collection values + * @param collection + * The collection to filter + * @param collectionFilter + * The collection filter + * @return The filtered collection + */ + public static Collection filteredCollection(Collection collection, Filter collectionFilter) { + return filteredList(new ArrayList(collection), collectionFilter); + } + + /** + * Returns an iterator that contains only the elements from the given + * iterator that match the given filter. + * + * @param + * The type of the iterator elements + * @param iterator + * The iterator to filter + * @param iteratorFilter + * The iterator filter + * @return The filtered iterator + */ + public static Iterator filteredIterator(final Iterator iterator, final Filter iteratorFilter) { + return new Iterator() { + + private boolean gotNextElement = false; + + private E nextElement; + + private void getNextElement() { + if (gotNextElement) { + return; + } + while (iterator.hasNext()) { + nextElement = iterator.next(); + if (iteratorFilter.filterObject(nextElement)) { + gotNextElement = true; + break; + } + } + } + + /** + * {@inheritDoc} + * + * @see java.util.Iterator#hasNext() + */ + public boolean hasNext() { + getNextElement(); + return gotNextElement; + } + + /** + * {@inheritDoc} + * + * @see java.util.Iterator#next() + */ + public E next() { + getNextElement(); + if (!gotNextElement) { + throw new NoSuchElementException("no more elements in iteration"); + } + gotNextElement = false; + return nextElement; + } + + /** + * {@inheritDoc} + * + * @see java.util.Iterator#remove() + */ + public void remove() { + throw new UnsupportedOperationException("remove() not supported on this iteration"); + } + + }; + } + +} -- 2.7.4