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