2 * rhynodge - BlacklistFilter.java - Copyright © 2013 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 3 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, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.rhynodge.filters;
20 import static com.google.common.base.Preconditions.checkState;
21 import static com.google.common.collect.FluentIterable.from;
22 import static java.util.Arrays.asList;
24 import java.util.List;
26 import net.pterodactylus.rhynodge.Filter;
27 import net.pterodactylus.rhynodge.State;
28 import net.pterodactylus.rhynodge.states.FailedState;
29 import net.pterodactylus.rhynodge.states.TorrentState;
30 import net.pterodactylus.rhynodge.states.TorrentState.TorrentFile;
32 import com.google.common.base.Predicate;
35 * Filter for {@link TorrentState}s that removes all {@link TorrentFile}s whose
36 * names match a list of bad words.
38 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40 public class BlacklistFilter implements Filter {
42 private final Iterable<String> filterWords;
44 public BlacklistFilter(List<String> filterWords) {
45 this.filterWords = filterWords;
49 public State filter(State state) {
50 if (!state.success()) {
51 return FailedState.from(state);
53 checkState(state instanceof TorrentState, "state is not a TorrentState but a %s!", state.getClass());
55 TorrentState torrentState = (TorrentState) state;
56 return new TorrentState(from(torrentState.torrentFiles()).filter(new Predicate<TorrentFile>() {
58 public boolean apply(TorrentFile torrentFile) {
59 return (torrentFile != null) && nameDoesNotMatchAFilterWord(torrentFile.name());
62 private boolean nameDoesNotMatchAFilterWord(final String name) {
63 return !from(filterWords).anyMatch(new Predicate<String>() {
65 public boolean apply(String word) {
66 return name.toLowerCase().contains(word.toLowerCase());
73 public static BlacklistFilter createDefaultBlacklistFilter() {
74 return new BlacklistFilter(asList(