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