2 * jFCPlib - Filters.java - Copyright © 2009 David Roden
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 package net.pterodactylus.util.filter;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.HashMap;
23 import java.util.HashSet;
24 import java.util.Iterator;
25 import java.util.List;
27 import java.util.NoSuchElementException;
29 import java.util.Map.Entry;
32 * Defines various methods to filter {@link Collection}s.
34 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
36 public class Filters {
39 * Returns a list that contains only the elements from the given list that
40 * match the given filter.
43 * The type of the list elements
48 * @return The filtered list
50 public static <E> List<E> filteredList(List<E> list, Filter<E> listFilter) {
51 List<E> filteredList = new ArrayList<E>();
52 for (E element : list) {
53 if (listFilter.filterObject(element)) {
54 filteredList.add(element);
61 * Returns a set that contains only the elements from the given set that
62 * match the given filter.
65 * The type of the set elements
70 * @return The filtered set
72 public static <E> Set<E> filteredSet(Set<E> set, Filter<E> setFilter) {
73 Set<E> filteredSet = new HashSet<E>();
74 for (E element : set) {
75 if (setFilter.filterObject(element)) {
76 filteredSet.add(element);
83 * Returns a map that contains only the elements from the given map that
84 * match the given filter.
87 * The type of the map keys
89 * The type of the map values
94 * @return The filtered map
96 public static <K, V> Map<K, V> filteredMap(Map<K, V> map, Filter<Entry<K, V>> mapFilter) {
97 Map<K, V> filteredMap = new HashMap<K, V>();
98 for (Entry<K, V> element : map.entrySet()) {
99 if (mapFilter.filterObject(element)) {
100 filteredMap.put(element.getKey(), element.getValue());
107 * Returns a collection that contains only the elements from the given
108 * collection that match the given filter.
111 * The type of the collection values
113 * The collection to filter
114 * @param collectionFilter
115 * The collection filter
116 * @return The filtered collection
118 public static <K> Collection<K> filteredCollection(Collection<K> collection, Filter<K> collectionFilter) {
119 return filteredList(new ArrayList<K>(collection), collectionFilter);
123 * Returns an iterator that contains only the elements from the given
124 * iterator that match the given filter.
127 * The type of the iterator elements
129 * The iterator to filter
130 * @param iteratorFilter
131 * The iterator filter
132 * @return The filtered iterator
134 public static <E> Iterator<E> filteredIterator(final Iterator<E> iterator, final Filter<E> iteratorFilter) {
135 return new Iterator<E>() {
137 private boolean gotNextElement = false;
139 private E nextElement;
141 private void getNextElement() {
142 if (gotNextElement) {
145 while (iterator.hasNext()) {
146 nextElement = iterator.next();
147 if (iteratorFilter.filterObject(nextElement)) {
148 gotNextElement = true;
157 * @see java.util.Iterator#hasNext()
159 public boolean hasNext() {
161 return gotNextElement;
167 * @see java.util.Iterator#next()
171 if (!gotNextElement) {
172 throw new NoSuchElementException("no more elements in iteration");
174 gotNextElement = false;
181 * @see java.util.Iterator#remove()
183 public void remove() {
184 throw new UnsupportedOperationException("remove() not supported on this iteration");