| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| BookFilterIterator |
|
| 2.1666666666666665;2.167 |
| 1 | /** | |
| 2 | * Distribution License: | |
| 3 | * JSword is free software; you can redistribute it and/or modify it under | |
| 4 | * the terms of the GNU Lesser General Public License, version 2.1 or later | |
| 5 | * as published by the Free Software Foundation. This program is distributed | |
| 6 | * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even | |
| 7 | * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
| 8 | * See the GNU Lesser General Public License for more details. | |
| 9 | * | |
| 10 | * The License is available on the internet at: | |
| 11 | * http://www.gnu.org/copyleft/lgpl.html | |
| 12 | * or by writing to: | |
| 13 | * Free Software Foundation, Inc. | |
| 14 | * 59 Temple Place - Suite 330 | |
| 15 | * Boston, MA 02111-1307, USA | |
| 16 | * | |
| 17 | * © CrossWire Bible Society, 2005 - 2016 | |
| 18 | * | |
| 19 | */ | |
| 20 | package org.crosswire.jsword.book; | |
| 21 | ||
| 22 | import java.util.Iterator; | |
| 23 | import java.util.NoSuchElementException; | |
| 24 | ||
| 25 | /** | |
| 26 | * An iterator that filters as it goes. | |
| 27 | * | |
| 28 | * @see gnu.lgpl.License The GNU Lesser General Public License for details. | |
| 29 | * @author Joe Walker | |
| 30 | * @author DM Smith | |
| 31 | */ | |
| 32 | 0 | public class BookFilterIterator implements Iterable<Book>, Iterator<Book> { |
| 33 | /** | |
| 34 | * Simple ctor | |
| 35 | * | |
| 36 | * @param books an iterator over a set of books | |
| 37 | * @param filter | |
| 38 | * The filter to use, if null, will iterate over all values | |
| 39 | */ | |
| 40 | 0 | public BookFilterIterator(Iterable<Book> books, BookFilter filter) { |
| 41 | 0 | this.it = books.iterator(); |
| 42 | 0 | this.filter = filter; |
| 43 | 0 | } |
| 44 | ||
| 45 | /* (non-Javadoc) | |
| 46 | * @see java.lang.Iterable#iterator() | |
| 47 | */ | |
| 48 | public Iterator<Book> iterator() { | |
| 49 | 0 | return this; |
| 50 | } | |
| 51 | ||
| 52 | /* (non-Javadoc) | |
| 53 | * @see java.util.Iterator#hasNext() | |
| 54 | */ | |
| 55 | public boolean hasNext() { | |
| 56 | 0 | next = findNext(); |
| 57 | 0 | return next != null; |
| 58 | } | |
| 59 | ||
| 60 | /* (non-Javadoc) | |
| 61 | * @see java.util.Iterator#next() | |
| 62 | */ | |
| 63 | public Book next() { | |
| 64 | 0 | if (next == null) { |
| 65 | 0 | throw new NoSuchElementException(); |
| 66 | } | |
| 67 | 0 | return next; |
| 68 | } | |
| 69 | ||
| 70 | /* (non-Javadoc) | |
| 71 | * @see java.util.Iterator#remove() | |
| 72 | */ | |
| 73 | public void remove() { | |
| 74 | 0 | throw new UnsupportedOperationException(); |
| 75 | } | |
| 76 | ||
| 77 | /** | |
| 78 | * Find the next (if there is one) | |
| 79 | * | |
| 80 | * @return the next book | |
| 81 | */ | |
| 82 | private Book findNext() { | |
| 83 | 0 | while (it.hasNext()) { |
| 84 | 0 | Book book = it.next(); |
| 85 | 0 | if (filter == null || filter.test(book)) { |
| 86 | 0 | return book; |
| 87 | } | |
| 88 | 0 | } |
| 89 | ||
| 90 | 0 | return null; |
| 91 | } | |
| 92 | ||
| 93 | /** | |
| 94 | * The stored next value | |
| 95 | */ | |
| 96 | private Book next; | |
| 97 | ||
| 98 | private Iterator<Book> it; | |
| 99 | ||
| 100 | /** | |
| 101 | * The value filter | |
| 102 | */ | |
| 103 | private BookFilter filter; | |
| 104 | ||
| 105 | } |