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