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