2 * jFCPlib - Filters.java -
3 * Copyright © 2009 David Roden
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 package net.pterodactylus.util.filter;
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.Iterator;
26 import java.util.List;
28 import java.util.NoSuchElementException;
30 import java.util.Map.Entry;
33 * Defines various methods to filter {@link Collection}s.
35 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
37 public class Filters {
40 * Returns a list that contains only the elements from the given list that
41 * match the given filter.
44 * The type of the list elements
49 * @return The filtered list
51 public static <E> List<E> filteredList(List<E> list, Filter<E> listFilter) {
52 List<E> filteredList = new ArrayList<E>();
53 for (E element : list) {
54 if (listFilter.filterObject(element)) {
55 filteredList.add(element);
62 * Returns a set that contains only the elements from the given set that
63 * match the given filter.
66 * The type of the set elements
71 * @return The filtered set
73 public static <E> Set<E> filteredSet(Set<E> set, Filter<E> setFilter) {
74 Set<E> filteredSet = new HashSet<E>();
75 for (E element : set) {
76 if (setFilter.filterObject(element)) {
77 filteredSet.add(element);
84 * Returns a map that contains only the elements from the given map that
85 * match the given filter.
88 * The type of the map keys
90 * The type of the map values
95 * @return The filtered map
97 public static <K, V> Map<K, V> filteredMap(Map<K, V> map, Filter<Entry<K, V>> mapFilter) {
98 Map<K, V> filteredMap = new HashMap<K, V>();
99 for (Entry<K, V> element : map.entrySet()) {
100 if (mapFilter.filterObject(element)) {
101 filteredMap.put(element.getKey(), element.getValue());
108 * Returns a collection that contains only the elements from the given
109 * collection that match the given filter.
112 * The type of the collection values
114 * The collection to filter
115 * @param collectionFilter
116 * The collection filter
117 * @return The filtered collection
119 public static <K> Collection<K> filteredCollection(Collection<K> collection, Filter<K> collectionFilter) {
120 return filteredList(new ArrayList<K>(collection), collectionFilter);
124 * Returns an iterator that contains only the elements from the given
125 * iterator that match the given filter.
128 * The type of the iterator elements
130 * The iterator to filter
131 * @param iteratorFilter
132 * The iterator filter
133 * @return The filtered iterator
135 public static <E> Iterator<E> filteredIterator(final Iterator<E> iterator, final Filter<E> iteratorFilter) {
136 return new Iterator<E>() {
138 private boolean gotNextElement = false;
140 private E nextElement;
142 private void getNextElement() {
143 if (gotNextElement) {
146 while (iterator.hasNext()) {
147 nextElement = iterator.next();
148 if (iteratorFilter.filterObject(nextElement)) {
149 gotNextElement = true;
158 * @see java.util.Iterator#hasNext()
160 public boolean hasNext() {
162 return gotNextElement;
168 * @see java.util.Iterator#next()
172 if (!gotNextElement) {
173 throw new NoSuchElementException("no more elements in iteration");
175 gotNextElement = false;
182 * @see java.util.Iterator#remove()
184 public void remove() {
185 throw new UnsupportedOperationException("remove() not supported on this iteration");