Coverage Report - org.crosswire.jsword.book.DefaultBook
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultBook
0%
0/35
0%
0/18
2.333
 
 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.List;
 24  
 
 25  
 import org.slf4j.Logger;
 26  
 import org.slf4j.LoggerFactory;
 27  
 
 28  
 /**
 29  
  * Defines a single default book.
 30  
  * 
 31  
  * @see gnu.lgpl.License The GNU Lesser General Public License for details.
 32  
  * @author Joe Walker
 33  
  */
 34  
 public class DefaultBook {
 35  0
     public DefaultBook(BookList bookList, BookFilter bookFilter) {
 36  0
         books = bookList;
 37  0
         filter = bookFilter;
 38  0
     }
 39  
 
 40  
     /**
 41  
      * Set the default Book. It must satisfy the filter.
 42  
      * 
 43  
      * @param newBook
 44  
      *            The version to use as default.
 45  
      */
 46  
     public void setDefault(Book newBook) {
 47  0
         if (filter.test(newBook)) {
 48  0
             book = newBook;
 49  
         }
 50  0
     }
 51  
 
 52  
     /**
 53  
      * Set the default Book conditionally. It has to satisfy the filter and the
 54  
      * book must not currently be set.
 55  
      * 
 56  
      * @param newBook
 57  
      *            The version to use as default.
 58  
      */
 59  
     public void setDefaultConditionally(Book newBook) {
 60  0
         if (book == null) {
 61  0
             setDefault(newBook);
 62  
         }
 63  0
     }
 64  
 
 65  
     /**
 66  
      * Unset the current default book and attempt to appoint another.
 67  
      */
 68  
     protected void unsetDefault() {
 69  0
         book = null;
 70  
 
 71  0
         checkReplacement();
 72  0
     }
 73  
 
 74  
     /**
 75  
      * Unset the current default book, if it matches the argument and attempt to
 76  
      * appoint another.
 77  
      * 
 78  
      * @param oldBook the book to unset if it is the default
 79  
      */
 80  
     protected void unsetDefaultConditionally(Book oldBook) {
 81  0
         if (book == oldBook) {
 82  0
             unsetDefault();
 83  
         }
 84  0
     }
 85  
 
 86  
     /**
 87  
      * Get the current default book or null if there is none.
 88  
      * 
 89  
      * @return the current default version
 90  
      */
 91  
     public Book getDefault() {
 92  0
         return book;
 93  
     }
 94  
 
 95  
     /**
 96  
      * This method is identical to <code>getDefault().getName()</code> and is
 97  
      * only used by Config which works best with strings under reflection.
 98  
      * 
 99  
      * @return the default book name
 100  
      */
 101  
     public String getDefaultName() {
 102  0
         if (book == null) {
 103  0
             return null;
 104  
         }
 105  
 
 106  0
         return book.getName();
 107  
     }
 108  
 
 109  
     /**
 110  
      * Trawl through all the known Books satisfying the filter looking for the
 111  
      * one matching the given name.
 112  
      * <p>
 113  
      * This method is for use with config scripts and other things that
 114  
      * <b>need</b> to work with Strings. The preferred method is to use Book
 115  
      * objects.
 116  
      * <p>
 117  
      * This method is picky in that it only matches when the driver and the
 118  
      * version are the same. The user (probably) only cares about the version
 119  
      * though, and so might be disappointed when we fail to match AV (FooDriver)
 120  
      * against AV (BarDriver).
 121  
      * 
 122  
      * @param name
 123  
      *            The version to use as default.
 124  
      */
 125  
     public void setDefaultByName(String name) {
 126  0
         if (name == null || name.length() == 0) {
 127  0
             LOGGER.warn("Attempt to set empty book as default. Ignoring");
 128  0
             return;
 129  
         }
 130  
 
 131  0
         for (Book aBook : books.getBooks(filter)) {
 132  0
             if (aBook.match(name)) {
 133  0
                 setDefault(aBook);
 134  0
                 return;
 135  
             }
 136  
         }
 137  
 
 138  0
         LOGGER.warn("Book not found. Ignoring: {}", name);
 139  0
     }
 140  
 
 141  
     /**
 142  
      * Go through all of the current books checking to see if we need to replace
 143  
      * the current defaults with one of these.
 144  
      */
 145  
     protected void checkReplacement() {
 146  0
         List<Book> bookList = books.getBooks(filter);
 147  
 
 148  0
         Iterator<Book> it = bookList.iterator();
 149  0
         if (it.hasNext()) {
 150  0
             book = it.next();
 151  
         }
 152  0
     }
 153  
 
 154  
     /**
 155  
      * The default book
 156  
      */
 157  
     private Book book;
 158  
 
 159  
     /**
 160  
      * The list of candidate books.
 161  
      */
 162  
     private final BookList books;
 163  
 
 164  
     /**
 165  
      * The filter against books that returns candidates.
 166  
      */
 167  
     private final BookFilter filter;
 168  
 
 169  
     /**
 170  
      * The log stream
 171  
      */
 172  0
     private static final Logger LOGGER = LoggerFactory.getLogger(DefaultBook.class);
 173  
 }