X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fnet%2Fpterodactylus%2Futil%2Ffilter%2FFilters.java;fp=src%2Fnet%2Fpterodactylus%2Futil%2Ffilter%2FFilters.java;h=0000000000000000000000000000000000000000;hb=508624458578f01a393f8b44f32b98b40054fdc8;hp=d2bbb494d9d3c6f51357cbb6b157adbbb95c75dc;hpb=dd605aee444057a874fabf7fb0045b8448b0d1cd;p=jFCPlib.git diff --git a/src/net/pterodactylus/util/filter/Filters.java b/src/net/pterodactylus/util/filter/Filters.java deleted file mode 100644 index d2bbb49..0000000 --- a/src/net/pterodactylus/util/filter/Filters.java +++ /dev/null @@ -1,190 +0,0 @@ -/* - * 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"); - } - - }; - } - -}