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