1
20 package org.crosswire.jsword.book;
21
22 import java.util.ArrayList;
23 import java.util.List;
24
25
31 public final class BookFilters {
32
35 private BookFilters() {
36 }
37
38
43 public static BookFilter getAll() {
44 return new AllBookFilter();
45 }
46
47
53 public static BookFilter getBibles() {
54 if (commentariesWithBibles) {
55 return either(new BookCategoryFilter(BookCategory.BIBLE), new BookCategoryFilter(BookCategory.COMMENTARY));
56 }
57 return new BookCategoryFilter(BookCategory.BIBLE);
58 }
59
60
65 public static BookFilter getOnlyBibles() {
66 return new BookCategoryFilter(BookCategory.BIBLE);
67 }
68
69
75 public static BookFilter getNonBibles() {
76 if (commentariesWithBibles) {
77 return both(new NotBookCategoryFilter(BookCategory.BIBLE), new NotBookCategoryFilter(BookCategory.COMMENTARY));
78 }
79 return new NotBookCategoryFilter(BookCategory.BIBLE);
80 }
81
82
87 public static BookFilter getDictionaries() {
88 return new BookCategoryFilter(BookCategory.DICTIONARY);
89 }
90
91
96 public static BookFilter getGlossaries() {
97 return new BookCategoryFilter(BookCategory.GLOSSARY);
98 }
99
100
105 public static BookFilter getDailyDevotionals() {
106 return new BookCategoryFilter(BookCategory.DAILY_DEVOTIONS);
107 }
108
109
114 public static BookFilter getCommentaries() {
115 return new BookCategoryFilter(BookCategory.COMMENTARY);
116 }
117
118
123 public static BookFilter getGeneralBooks() {
124 return new BookCategoryFilter(BookCategory.GENERAL_BOOK);
125 }
126
127
132 public static BookFilter getMaps() {
133 return new BookCategoryFilter(BookCategory.MAPS);
134 }
135
136
141 public static BookFilter getGreekDefinitions() {
142 return new BookFeatureFilter(FeatureType.GREEK_DEFINITIONS);
143 }
144
145
151 public static BookFilter getGreekParse() {
152 return new BookFeatureFilter(FeatureType.GREEK_PARSE);
153 }
154
155
160 public static BookFilter getHebrewDefinitions() {
161 return new BookFeatureFilter(FeatureType.HEBREW_DEFINITIONS);
162 }
163
164
170 public static BookFilter getHebrewParse() {
171 return new BookFeatureFilter(FeatureType.HEBREW_PARSE);
172 }
173
174
180 public static boolean isCommentariesWithBibles() {
181 return commentariesWithBibles;
182 }
183
184
190 public static void setCommentariesWithBibles(boolean current) {
191 commentariesWithBibles = current;
192 }
193
194
197 private static boolean commentariesWithBibles;
198
199
202 static class AllBookFilter implements BookFilter {
203
206 public boolean test(Book book) {
207 return true;
208 }
209 }
210
211
214 static class BookCategoryFilter implements BookFilter {
215 BookCategoryFilter(BookCategory category) {
216 this.category = category;
217 }
218
219
222 public boolean test(Book book) {
223 return book.getBookCategory().equals(category) && !book.isLocked();
224 }
225
226 private BookCategory category;
227 }
228
229
232 static class NotBookCategoryFilter implements BookFilter {
233 NotBookCategoryFilter(BookCategory category) {
234 this.category = category;
235 }
236
237
240 public boolean test(Book book) {
241 return !book.getBookCategory().equals(category) && !book.isLocked();
242 }
243
244 private BookCategory category;
245 }
246
247
250 public static class BookFeatureFilter implements BookFilter {
251 public BookFeatureFilter(FeatureType feature) {
252 this.feature = feature;
253 }
254
255
258 public boolean test(Book book) {
259 return book.hasFeature(feature) && !book.isLocked();
260 }
261
262 private FeatureType feature;
263 }
264
265
272 public static BookFilter both(final BookFilter b1, final BookFilter b2) {
273 return new BookFilter() {
274 public boolean test(Book book) {
275 return b1.test(book) && b2.test(book);
276 }
277 };
278 }
279
280
287 public static BookFilter either(final BookFilter b1, final BookFilter b2) {
288 return new BookFilter() {
289 public boolean test(Book book) {
290 return b1.test(book) || b2.test(book);
291 }
292 };
293 }
294
295
301 public static BookFilter getBooksByDriver(final BookDriver driver) {
302 return new BookFilter() {
303 public boolean test(Book book) {
304 return book.getDriver() == driver;
305 }
306 };
307 }
308
309
321 public static BookFilter getCustom(String match) {
322 return new CustomBookFilter(match);
323 }
324
325
328 static class CustomBookFilter implements BookFilter {
329
336 CustomBookFilter(String match) {
337 List<Test> cache = new ArrayList<Test>();
338 String[] filters = match.split(";");
339 for (int i = 0; i < filters.length; i++) {
340 cache.add(new Test(filters[i]));
341 }
342
343 tests = cache.toArray(new Test[cache.size()]);
344 }
345
346
349 public boolean test(Book book) {
350 for (int i = 0; i < tests.length; i++) {
351 Test test = tests[i];
352 if ("initials".equalsIgnoreCase(test.property)) {
353 if (!test.result.equals(book.getInitials())) {
354 return false;
355 }
356 continue;
357 }
358 String result = book.getProperty(test.property);
359 if (result == null || !test.result.equals(result)) {
360 return false;
361 }
362 }
363
364 return true;
365 }
366
367 private Test[] tests;
368
369
372 static class Test {
373 protected Test(String filter) {
374 String[] parts = filter.split("=");
375 if (parts.length != 2 || parts[0].length() == 0 || parts[1].length() == 0) {
376 throw new IllegalArgumentException("Filter format is 'property=value', given: " + filter);
377 }
378 this.property = parts[0];
379 this.result = parts[1];
380
381 }
382 protected Test(String property, String result) {
383 this.property = property;
384 this.result = result;
385 }
386 protected String property;
387 protected String result;
388 }
389 }
390 }
391