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.Collections;
23  import java.util.List;
24  import java.util.concurrent.CopyOnWriteArrayList;
25  
26  import org.crosswire.common.util.CollectionUtil;
27  
28  /**
29   * A basic implementation of BookList. The methods in this abstract class are
30   * duplicates of those in Books, so bugs fixed in one should be fixed in the
31   * other too.
32   * 
33   * @see gnu.lgpl.License The GNU Lesser General Public License for details.
34   * @author Joe Walker
35   */
36  public abstract class AbstractBookList implements BookList {
37      /**
38       * Build a default BookList
39       */
40      public AbstractBookList() {
41          listeners = new CopyOnWriteArrayList<BooksListener>();
42      }
43  
44      /* (non-Javadoc)
45       * @see org.crosswire.jsword.book.BookList#getBooks(org.crosswire.jsword.book.BookFilter)
46       */
47      public List<Book> getBooks(BookFilter filter) {
48          List<Book> temp = CollectionUtil.createList(new BookFilterIterator(getBooks(), filter));
49          return Collections.unmodifiableList(temp);
50      }
51  
52      /* (non-Javadoc)
53       * @see org.crosswire.jsword.book.BookList#addBooksListener(org.crosswire.jsword.book.BooksListener)
54       */
55      public void addBooksListener(BooksListener li) {
56          listeners.add(li);
57      }
58  
59      /* (non-Javadoc)
60       * @see org.crosswire.jsword.book.BookList#removeBooksListener(org.crosswire.jsword.book.BooksListener)
61       */
62      public  void removeBooksListener(BooksListener li) {
63          listeners.remove(li);
64      }
65  
66      /**
67       * Kick of an event sequence
68       * 
69       * @param source
70       *            The event source
71       * @param book
72       *            The changed Book
73       * @param added
74       *            Is it added?
75       */
76      protected void fireBooksChanged(Object source, Book book, boolean added) {
77          BooksEvent ev = new BooksEvent(source, book, added);
78          for (BooksListener listener : listeners) {
79              if (added) {
80                  listener.bookAdded(ev);
81              } else {
82                  listener.bookRemoved(ev);
83              }
84          }
85      }
86  
87      /**
88       * The list of listeners
89       */
90      private List<BooksListener> listeners = new CopyOnWriteArrayList<BooksListener>();
91  }
92