DefaultBook.java |
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 public DefaultBook(BookList bookList, BookFilter bookFilter) { 36 books = bookList; 37 filter = bookFilter; 38 } 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 if (filter.test(newBook)) { 48 book = newBook; 49 } 50 } 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 if (book == null) { 61 setDefault(newBook); 62 } 63 } 64 65 /** 66 * Unset the current default book and attempt to appoint another. 67 */ 68 protected void unsetDefault() { 69 book = null; 70 71 checkReplacement(); 72 } 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 if (book == oldBook) { 82 unsetDefault(); 83 } 84 } 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 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 if (book == null) { 103 return null; 104 } 105 106 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 if (name == null || name.length() == 0) { 127 LOGGER.warn("Attempt to set empty book as default. Ignoring"); 128 return; 129 } 130 131 for (Book aBook : books.getBooks(filter)) { 132 if (aBook.match(name)) { 133 setDefault(aBook); 134 return; 135 } 136 } 137 138 LOGGER.warn("Book not found. Ignoring: {}", name); 139 } 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 List<Book> bookList = books.getBooks(filter); 147 148 Iterator<Book> it = bookList.iterator(); 149 if (it.hasNext()) { 150 book = it.next(); 151 } 152 } 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 private static final Logger LOGGER = LoggerFactory.getLogger(DefaultBook.class); 173 } 174