fe3f12fdb169cf23384fdbedcf75518ff02ef715
[jFCPlib.git] / src / net / pterodactylus / util / filter / Filters.java
1 /*
2  * jFCPlib - Filters.java -
3  * Copyright © 2009 David Roden
4  *
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.
9  *
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.
14  *
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.
18  */
19 package net.pterodactylus.util.filter;
20
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;
27 import java.util.Map;
28 import java.util.NoSuchElementException;
29 import java.util.Set;
30 import java.util.Map.Entry;
31
32 /**
33  * Defines various methods to filter {@link Collection}s.
34  *
35  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
36  */
37 public class Filters {
38
39         /**
40          * Returns a list that contains only the elements from the given list that
41          * match the given filter.
42          *
43          * @param <E>
44          *            The type of the list elements
45          * @param list
46          *            The list to filter
47          * @param listFilter
48          *            The list filter
49          * @return The filtered list
50          */
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);
56                         }
57                 }
58                 return filteredList;
59         }
60
61         /**
62          * Returns a set that contains only the elements from the given set that
63          * match the given filter.
64          *
65          * @param <E>
66          *            The type of the set elements
67          * @param set
68          *            The set to filter
69          * @param setFilter
70          *            The set filter
71          * @return The filtered set
72          */
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);
78                         }
79                 }
80                 return filteredSet;
81         }
82
83         /**
84          * Returns a map that contains only the elements from the given map that
85          * match the given filter.
86          *
87          * @param <K>
88          *            The type of the map keys
89          * @param <V>
90          *            The type of the map values
91          * @param map
92          *            The map to filter
93          * @param mapFilter
94          *            The map filter
95          * @return The filtered map
96          */
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());
102                         }
103                 }
104                 return filteredMap;
105         }
106
107         /**
108          * Returns a collection that contains only the elements from the given
109          * collection that match the given filter.
110          *
111          * @param <K>
112          *            The type of the collection values
113          * @param collection
114          *            The collection to filter
115          * @param collectionFilter
116          *            The collection filter
117          * @return The filtered collection
118          */
119         public static <K> Collection<K> filteredCollection(Collection<K> collection, Filter<K> collectionFilter) {
120                 return filteredList(new ArrayList<K>(collection), collectionFilter);
121         }
122
123         /**
124          * Returns an iterator that contains only the elements from the given
125          * iterator that match the given filter.
126          *
127          * @param <E>
128          *            The type of the iterator elements
129          * @param iterator
130          *            The iterator to filter
131          * @param iteratorFilter
132          *            The iterator filter
133          * @return The filtered iterator
134          */
135         public static <E> Iterator<E> filteredIterator(final Iterator<E> iterator, final Filter<E> iteratorFilter) {
136                 return new Iterator<E>() {
137
138                         private boolean gotNextElement = false;
139
140                         private E nextElement;
141
142                         private void getNextElement() {
143                                 if (gotNextElement) {
144                                         return;
145                                 }
146                                 while (iterator.hasNext()) {
147                                         nextElement = iterator.next();
148                                         if (iteratorFilter.filterObject(nextElement)) {
149                                                 gotNextElement = true;
150                                                 break;
151                                         }
152                                 }
153                         }
154
155                         /**
156                          * {@inheritDoc}
157                          *
158                          * @see java.util.Iterator#hasNext()
159                          */
160                         public boolean hasNext() {
161                                 getNextElement();
162                                 return gotNextElement;
163                         }
164
165                         /**
166                          * {@inheritDoc}
167                          *
168                          * @see java.util.Iterator#next()
169                          */
170                         public E next() {
171                                 getNextElement();
172                                 if (!gotNextElement) {
173                                         throw new NoSuchElementException("no more elements in iteration");
174                                 }
175                                 gotNextElement = false;
176                                 return nextElement;
177                         }
178
179                         /**
180                          * {@inheritDoc}
181                          *
182                          * @see java.util.Iterator#remove()
183                          */
184                         public void remove() {
185                                 throw new UnsupportedOperationException("remove() not supported on this iteration");
186                         }
187
188                 };
189         }
190
191 }