1
20 package org.crosswire.jsword.passage;
21
22 import org.crosswire.jsword.versification.BibleBook;
23 import org.crosswire.jsword.versification.Versification;
24
25
32 public enum RestrictionType {
33
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
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
128 public abstract boolean isSameScope(Versification v11n, Verse start, Verse end);
129
130
141 public abstract VerseRange blur(Versification v11n, VerseRange range, int blurDown, int blurUp);
142
143
154 public abstract VerseRange blur(Versification v11n, Verse verse, int blurDown, int blurUp);
155
156
165 public abstract VerseRange toRange(Versification v11n, Verse verse, int count);
166
167
172 public int toInteger() {
173 return ordinal();
174 }
175
176
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 assert false;
190 return null;
191 }
192
193
199 public static RestrictionType fromInteger(int i) {
200 for (RestrictionType v : values()) {
201 if (v.ordinal() == i) {
202 return v;
203 }
204 }
205
206 assert false;
208 return null;
209 }
210
211
217 public static void setBlurRestriction(int value) {
218 defaultBlurRestriction = RestrictionType.fromInteger(value);
219 }
220
221
227 public static int getBlurRestriction() {
228 return getDefaultBlurRestriction().toInteger();
229 }
230
231
236 public static RestrictionType getDefaultBlurRestriction() {
237 return defaultBlurRestriction;
238 }
239
240
243 private static RestrictionType defaultBlurRestriction = RestrictionType.NONE;
244 }
245