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   * Copyright: 2005
18   *     The copyright to this program is held by its authors.
19   *
20   */
21  package org.crosswire.jsword.passage;
22  
23  import org.crosswire.jsword.versification.BibleBook;
24  import org.crosswire.jsword.versification.Versification;
25  
26  /**
27   * Types of Passage Blurring Restrictions.
28   * 
29   * @see gnu.lgpl.License The GNU Lesser General Public License for details.
30   * @author Joe Walker
31   * @author DM Smith
32   */
33  public enum RestrictionType {
34      /**
35       * There is no restriction on blurring.
36       */
37      NONE {
38          @Override
39          public boolean isSameScope(Versification v11n, Verse start, Verse end) {
40              return true;
41          }
42  
43          @Override
44          public VerseRange blur(Versification v11n, VerseRange range, int blurDown, int blurUp) {
45              Verse start = v11n.subtract(range.getStart(), blurDown);
46              Verse end = v11n.add(range.getEnd(), blurUp);
47              return new VerseRange(v11n, start, end);
48          }
49  
50          @Override
51          public VerseRange blur(Versification v11n, Verse verse, int blurDown, int blurUp) {
52              Verse start = v11n.subtract(verse, blurDown);
53              Verse end = v11n.add(verse, blurUp);
54              return new VerseRange(v11n, start, end);
55          }
56  
57          @Override
58          public VerseRange toRange(Versification v11n, Verse verse, int count) {
59              Verse end = verse;
60              if (count > 1) {
61                  end = v11n.add(verse, count - 1);
62              }
63              return new VerseRange(v11n, verse, end);
64          }
65      },
66  
67      /**
68       * Blurring is restricted to the chapter
69       */
70      CHAPTER {
71          @Override
72          public boolean isSameScope(Versification v11n, Verse start, Verse end) {
73              return v11n.isSameChapter(start, end);
74          }
75  
76          @Override
77          public VerseRange blur(Versification v11n, VerseRange range, int blurDown, int blurUp) {
78              Verse start = range.getStart();
79              BibleBook startBook = start.getBook();
80              int startChapter = start.getChapter();
81              int startVerse = start.getVerse() - blurDown;
82  
83              Verse end = range.getEnd();
84              BibleBook endBook = end.getBook();
85              int endChapter = end.getChapter();
86              int endVerse = end.getVerse() + blurUp;
87  
88              startVerse = Math.max(startVerse, 0);
89              endVerse = Math.min(endVerse, v11n.getLastVerse(endBook, endChapter));
90  
91              Verse newStart = new Verse(v11n, startBook, startChapter, startVerse);
92              Verse newEnd = new Verse(v11n, endBook, endChapter, endVerse);
93              return new VerseRange(v11n, newStart, newEnd);
94          }
95  
96          @Override
97          public VerseRange blur(Versification v11n, Verse verse, int blurDown, int blurUp) {
98              BibleBook book = verse.getBook();
99              int chapter = verse.getChapter();
100             int startVerse = verse.getVerse() - blurDown;
101             int endVerse = verse.getVerse() + blurUp;
102 
103             startVerse = Math.max(startVerse, 0);
104             endVerse = Math.min(endVerse, v11n.getLastVerse(book, chapter));
105 
106             Verse start = new Verse(v11n, book, chapter, startVerse);
107             Verse end = new Verse(v11n, book, chapter, endVerse);
108             return new VerseRange(v11n, start, end);
109         }
110 
111         @Override
112         public VerseRange toRange(Versification v11n, Verse verse, int count) {
113             Verse end = v11n.add(verse, count - 1);
114             return new VerseRange(v11n, verse, end);
115         }
116     };
117 
118     /**
119      * Are the two verses in the same scope.
120      * 
121      * @param v11n
122      *            the versification to which this reference pertains
123      * @param start
124      *            the first verse
125      * @param end
126      *            the second verse
127      * @return true if the two are in the same scope.
128      */
129     public abstract boolean isSameScope(Versification v11n, Verse start, Verse end);
130 
131     /**
132      * Blur a verse range the specified amount. Since verse ranges are
133      * immutable, it creates a new one.
134      * 
135      * @param v11n
136      *            the versification to which this reference pertains
137      * @param range
138      * @param blurDown
139      * @param blurUp
140      * @return a verse range after blurring.
141      */
142     public abstract VerseRange blur(Versification v11n, VerseRange range, int blurDown, int blurUp);
143 
144     /**
145      * Blur a verse the specified amount. Since verse are immutable and refer to
146      * a single verse, it creates a verse range.
147      * 
148      * @param v11n
149      *            the versification to which this reference pertains
150      * @param verse
151      * @param blurDown
152      * @param blurUp
153      * @return a verse range after blurring
154      */
155     public abstract VerseRange blur(Versification v11n, Verse verse, int blurDown, int blurUp);
156 
157     /**
158      * Create a range from the verse having the specified number of verses.
159      * 
160      * @param v11n
161      *            the versification to which this reference pertains
162      * @param verse
163      * @param count
164      * @return a verse range created by extending a verse forward.
165      */
166     public abstract VerseRange toRange(Versification v11n, Verse verse, int count);
167 
168     /**
169      * Get an integer representation for this RestrictionType
170      * 
171      * @return the integer representation
172      */
173     public int toInteger() {
174         return ordinal();
175     }
176 
177     /**
178      * Lookup method to convert from a String
179      * @param name 
180      * @return the matching restriction type
181      */
182     public static RestrictionType fromString(String name) {
183         for (RestrictionType v : values()) {
184             if (v.name().equalsIgnoreCase(name)) {
185                 return v;
186             }
187         }
188 
189         // cannot get here
190         assert false;
191         return null;
192     }
193 
194     /**
195      * Lookup method to convert from an integer
196      * 
197      * @param i 
198      * @return the restriction type from its ordinal value
199      */
200     public static RestrictionType fromInteger(int i) {
201         for (RestrictionType v : values()) {
202             if (v.ordinal() == i) {
203                 return v;
204             }
205         }
206 
207         // cannot get here
208         assert false;
209         return null;
210     }
211 
212     /**
213      * The default Blur settings. This is used by config to set a default.
214      * 
215      * @param value
216      *            The new default blur setting
217      */
218     public static void setBlurRestriction(int value) {
219         defaultBlurRestriction = RestrictionType.fromInteger(value);
220     }
221 
222     /**
223      * The default Blur settings. This is used by config to manage a default
224      * setting.
225      * 
226      * @return The current default blurRestriction setting
227      */
228     public static int getBlurRestriction() {
229         return getDefaultBlurRestriction().toInteger();
230     }
231 
232     /**
233      * The default Blur settings. This is used by BlurCommandWord
234      * 
235      * @return The current default blurRestriction setting
236      */
237     public static RestrictionType getDefaultBlurRestriction() {
238         return defaultBlurRestriction;
239     }
240 
241     /**
242      * A default restriction type for blurring.
243      */
244     private static RestrictionType defaultBlurRestriction = RestrictionType.NONE;
245 }
246