Include whitespace at the end of each CSS line
[rhynodge.git] / src / main / java / net / pterodactylus / rhynodge / filters / BlacklistFilter.java
1 /*
2  * rhynodge - BlacklistFilter.java - Copyright © 2013 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 3 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, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.rhynodge.filters;
19
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;
23
24 import java.util.List;
25
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;
31
32 import com.google.common.base.Predicate;
33
34 /**
35  * Filter for {@link TorrentState}s that removes all {@link TorrentFile}s whose
36  * names match a list of bad words.
37  *
38  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
39  */
40 public class BlacklistFilter implements Filter {
41
42         private final Iterable<String> filterWords;
43
44         public BlacklistFilter(List<String> filterWords) {
45                 this.filterWords = filterWords;
46         }
47
48         @Override
49         public State filter(State state) {
50                 if (!state.success()) {
51                         return FailedState.from(state);
52                 }
53                 checkState(state instanceof TorrentState, "state is not a TorrentState but a %s!", state.getClass());
54
55                 TorrentState torrentState = (TorrentState) state;
56                 return new TorrentState(from(torrentState.torrentFiles()).filter(new Predicate<TorrentFile>() {
57                         @Override
58                         public boolean apply(TorrentFile torrentFile) {
59                                 return (torrentFile != null) && nameDoesNotMatchAFilterWord(torrentFile.name());
60                         }
61
62                         private boolean nameDoesNotMatchAFilterWord(final String name) {
63                                 return !from(filterWords).anyMatch(new Predicate<String>() {
64                                         @Override
65                                         public boolean apply(String word) {
66                                                 return name.toLowerCase().contains(word.toLowerCase());
67                                         }
68                                 });
69                         }
70                 }).toList());
71         }
72
73         public static BlacklistFilter createDefaultBlacklistFilter() {
74                 return new BlacklistFilter(asList(
75             "[G2G]",
76             "-3LT0N",
77             "-ADTRG",
78             "-AMIABLE",
79             "-AN0NYM0US",
80             "-ARROW",
81             "-AQOS",
82             "-AXED",
83             "-BeStDivX",
84             "-BiDA",
85             "-CM",
86             "-COCAIN",
87             "-DASH",
88             "-DEPRiVED",
89             "-DiAMOND",
90             "-DiRTYMARY",
91             "-DoNE",
92             "-EDAW",
93             "-EVO",
94             "-EVOLVE",
95             "-EwDp",
96             "-Felony",
97             "-FooKaS",
98             "-Haggebulle",
99             "-HELLRAZ0R",
100             "-iJUGGA",
101             "-IMAGiNE",
102             "-iMBT",
103             "-iND",
104             "-juggs",
105             "-KILLERS",
106             "-Larceny",
107             "-LEGi0N",
108             "-MAX",
109             "-MiLLENiUM",
110             "-NeDiVx",
111             "-NO1KNOWS",
112             "-NoGRP",
113             "-NOiR",
114             "-NYDIC",
115             "-P2P",
116             "-PLAYNOW",
117             "-PSYPHER",
118             "-PUKKA",
119             "-PrisM",
120             "-RARBG",
121             "-S4A",
122             "-SANTi",
123             "-SHODAN",
124             "-SPARKS",
125             "-SUFFiCE",
126             "-TAMILROCKERS",
127             "-TARGET",
128             "-TASTE",
129             "-TiTAN",
130             "-THC",
131             "-UnKnOwN",
132             "-UNiQUE",
133             "-W00D"
134         ));
135         }
136
137 }