[jsword-svn] jsword/java/jsword/org/crosswire/jsword/book s

jswordcvs at crosswire.org jswordcvs at crosswire.org
Tue Mar 22 19:05:40 MST 2005


Update of /cvs/jsword/jsword/java/jsword/org/crosswire/jsword/book
In directory www.crosswire.org:/tmp/cvs-serv26577/java/jsword/org/crosswire/jsword/book

Modified Files:
	BookFilters.java Books.java 
Log Message:
Made BookDriver a singleton with an instance() method which is used by reflection in Books to get an instance of a particular BookDriver.
Made registerDriver also re-register the driver.

Index: BookFilters.java
===================================================================
RCS file: /cvs/jsword/jsword/java/jsword/org/crosswire/jsword/book/BookFilters.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -C2 -d -r1.9 -r1.10
*** BookFilters.java	21 Mar 2005 02:37:28 -0000	1.9
--- BookFilters.java	23 Mar 2005 02:05:38 -0000	1.10
***************
*** 258,260 ****
--- 258,274 ----
          };
      }
+ 
+     /**
+      * A filter that accepts Books that match either of two criteria.
+      */
+     public static BookFilter getBooksByDriver(final BookDriver driver)
+     {
+         return new BookFilter()
+         {
+             public boolean test(Book book)
+             {
+                 return book.getDriver() == driver;
+             }
+         };
+     }
  }

Index: Books.java
===================================================================
RCS file: /cvs/jsword/jsword/java/jsword/org/crosswire/jsword/book/Books.java,v
retrieving revision 1.40
retrieving revision 1.41
diff -C2 -d -r1.40 -r1.41
*** Books.java	19 Mar 2005 18:44:58 -0000	1.40
--- Books.java	23 Mar 2005 02:05:38 -0000	1.41
***************
*** 1,8 ****
--- 1,11 ----
  package org.crosswire.jsword.book;
  
+ import java.lang.reflect.Method;
  import java.util.ArrayList;
  import java.util.Collections;
+ import java.util.HashSet;
  import java.util.Iterator;
  import java.util.List;
+ import java.util.Set;
  
  import org.crosswire.common.activate.Activator;
***************
*** 49,53 ****
      {
          books = new ArrayList();
!         drivers = new ArrayList();
          listeners = new EventListenerList();
          threaded = false;
--- 52,56 ----
      {
          books = new ArrayList();
!         drivers = new HashSet();
          listeners = new EventListenerList();
          threaded = false;
***************
*** 192,198 ****
          //log.debug("registering book: "+bmd.getName());
  
!         books.add(book);
! 
!         fireBooksChanged(instance, book, true);
      }
  
--- 195,203 ----
          //log.debug("registering book: "+bmd.getName());
  
!         if (!books.contains(book))
!         {
!             books.add(book);
!             fireBooksChanged(instance, book, true);
!         }
      }
  
***************
*** 220,224 ****
  
      /**
!      * Add to the list of drivers
       * @param driver The BookDriver to add
       */
--- 225,231 ----
  
      /**
!      * Register the driver, adding its books to the list. Any books that this driver
!      * used, but not any more are removed. This can be called repeatedly to re-register
!      * the driver.
       * @param driver The BookDriver to add
       */
***************
*** 227,241 ****
          log.debug("begin registering driver: " + driver.getClass().getName()); //$NON-NLS-1$
  
-         if (drivers.contains(driver))
-         {
-             throw new BookException(Msg.DUPLICATE_DRIVER);
-         }
- 
          drivers.add(driver);
  
          Book[] bookArray = driver.getBooks();
          for (int j = 0; j < bookArray.length; j++)
          {
!             addBook(bookArray[j]);
          }
  
--- 234,266 ----
          log.debug("begin registering driver: " + driver.getClass().getName()); //$NON-NLS-1$
  
          drivers.add(driver);
  
+         // Go through all the books and add all the new ones.
+         // Remove those that are not known to the driver, but used to be.
          Book[] bookArray = driver.getBooks();
+         Set current = CollectionUtil.createSet(new BookFilterIterator(getBooks().iterator(), BookFilters.getBooksByDriver(driver)));
+ 
          for (int j = 0; j < bookArray.length; j++)
          {
!             Book b = bookArray[j];
!             if (current.contains(b))
!             {
!                 // Since it was already in there, we don't add it.
!                 // By removing it from current we will be left with
!                 // what is not now known by the driver.
!                 current.remove(b);
!             }
!             else
!             {
!                 addBook(bookArray[j]);
!             }
!         }
!         
!         // Remove the books from the previous version of the driver
!         // that are not in this version.
!         Iterator iter = current.iterator();
!         while (iter.hasNext())
!         {
!             removeBook((Book)iter.next());
          }
  
***************
*** 372,376 ****
                  try
                  {
!                     BookDriver driver = (BookDriver) types[i].newInstance();
                      registerDriver(driver);
                  }
--- 397,403 ----
                  try
                  {
!                     Method driverInstance = types[i].getMethod("instance", new Class[0]); //$NON-NLS-1$
! //                    Object retval = driverInstance.invoke(null, new Object[0]);                   
!                     BookDriver driver = (BookDriver) driverInstance.invoke(null, new Object[0]);//types[i].newInstance();
                      registerDriver(driver);
                  }
***************
*** 396,400 ****
       * An array of BookDrivers
       */
!     private List drivers;
  
      /**
--- 423,427 ----
       * An array of BookDrivers
       */
!     private Set drivers;
  
      /**



More information about the jsword-svn mailing list