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 as published by
5    * the Free Software Foundation. This program is distributed in the hope
6    * that it will be useful, but WITHOUT ANY WARRANTY; without even the
7    * 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   * Copyright: 2005
18   *     The copyright to this program is held by it's authors.
19   *
20   * ID: $Id: BookFilterIterator.java 2050 2010-12-09 15:31:45Z dmsmith $
21   */
22  package org.crosswire.jsword.book;
23  
24  import java.util.Iterator;
25  import java.util.List;
26  import java.util.NoSuchElementException;
27  
28  /**
29   * An iterator that filters as it goes.
30   * 
31   * @see gnu.lgpl.License for license details.<br>
32   *      The copyright to this program is held by it's authors.
33   * @author Joe Walker [joe at eireneh dot com]
34   * @author DM Smith [dmsmith555 at yahoo dot com]
35   */
36  public class BookFilterIterator implements Iterable<Book>, Iterator<Book> {
37      /**
38       * Simple ctor
39       * 
40       * @param filter
41       *            The filter to use, if null, will iterate over all values
42       */
43      public BookFilterIterator(List<Book> books, BookFilter filter) {
44          this.it = books.iterator();
45          this.filter = filter;
46      }
47  
48      /* (non-Javadoc)
49       * @see java.lang.Iterable#iterator()
50       */
51      public Iterator<Book> iterator() {
52          return this;
53      }
54  
55      /* (non-Javadoc)
56       * @see java.util.Iterator#hasNext()
57       */
58      public boolean hasNext() {
59          next = findNext();
60          return next != null;
61      }
62  
63      /* (non-Javadoc)
64       * @see java.util.Iterator#next()
65       */
66      public Book next() {
67          if (next == null) {
68              throw new NoSuchElementException();
69          }
70          return next;
71      }
72  
73      /* (non-Javadoc)
74       * @see java.util.Iterator#remove()
75       */
76      public void remove() {
77          throw new UnsupportedOperationException();
78      }
79  
80      /**
81       * Find the next (if there is one)
82       */
83      private Book findNext() {
84          while (it.hasNext()) {
85              Book book = it.next();
86              if (filter == null || filter.test(book)) {
87                  return book;
88              }
89          }
90  
91          return null;
92      }
93  
94      /**
95       * The stored next value
96       */
97      private Book next;
98  
99      private Iterator<Book> it;
100 
101     /**
102      * The value filter
103      */
104     private BookFilter filter;
105 
106 }
107