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 java.io.IOException;
23  import java.io.Reader;
24  import java.io.Writer;
25  import java.util.Iterator;
26  
27  import org.crosswire.jsword.JSOtherMsg;
28  import org.crosswire.jsword.versification.Versification;
29  
30  /**
31   * This is a simple proxy to a real Passage object that denies all attempts to
32   * write to it.
33   * 
34   * @see gnu.lgpl.License The GNU Lesser General Public License for details.
35   * @author Joe Walker
36   */
37  final class ReadOnlyPassage implements Passage {
38      /**
39       * Construct a ReadOnlyPassage from a real Passage to which we proxy.
40       * 
41       * @param ref
42       *            The real Passage
43       * @param ignore
44       *            Do we throw up if someone tries to change us
45       */
46      ReadOnlyPassage(Passage ref, boolean ignore) {
47          this.ref = ref;
48          this.ignore = ignore;
49      }
50  
51      /* (non-Javadoc)
52       * @see org.crosswire.jsword.passage.Passage#getVersification()
53       */
54      public Versification getVersification() {
55          return ref.getVersification();
56      }
57  
58      /* (non-Javadoc)
59       * @see org.crosswire.jsword.passage.Passage#reversify(org.crosswire.jsword.versification.Versification)
60       */
61      public Passage reversify(Versification newVersification) {
62          return ref.reversify(newVersification);
63      }
64  
65      /* (non-Javadoc)
66       * @see org.crosswire.jsword.passage.VerseKey#isWhole()
67       */
68      public boolean isWhole() {
69          return ref.isWhole();
70      }
71  
72      /* (non-Javadoc)
73       * @see org.crosswire.jsword.passage.VerseKey#getWhole()
74       */
75      public Passage getWhole() {
76          return ref.getWhole();
77      }
78  
79      /* (non-Javadoc)
80       * @see org.crosswire.jsword.passage.Key#addAll(org.crosswire.jsword.passage.Key)
81       */
82      public void addAll(Key key) {
83          if (ignore) {
84              return;
85          }
86  
87          throw new IllegalStateException(JSOtherMsg.lookupText("Cannot alter a read-only passage"));
88      }
89  
90      /*
91       * (non-Javadoc)
92       * 
93       * @see
94       * org.crosswire.jsword.passage.Key#remove(org.crosswire.jsword.passage.Key)
95       */
96      public void removeAll(Key key) {
97          if (ignore) {
98              return;
99          }
100 
101         throw new IllegalStateException(JSOtherMsg.lookupText("Cannot alter a read-only passage"));
102     }
103 
104     /* (non-Javadoc)
105      * @see org.crosswire.jsword.passage.Key#retainAll(org.crosswire.jsword.passage.Key)
106      */
107     public void retainAll(Key key) {
108         if (ignore) {
109             return;
110         }
111 
112         throw new IllegalStateException(JSOtherMsg.lookupText("Cannot alter a read-only passage"));
113     }
114 
115     /* (non-Javadoc)
116      * @see org.crosswire.jsword.passage.Passage#contains(org.crosswire.jsword.passage.Key)
117      */
118     public boolean contains(Key key) {
119         return ref.contains(key);
120     }
121 
122     /* (non-Javadoc)
123      * @see org.crosswire.jsword.passage.Key#canHaveChildren()
124      */
125     public boolean canHaveChildren() {
126         return ref.canHaveChildren();
127     }
128 
129     /* (non-Javadoc)
130      * @see org.crosswire.jsword.passage.Key#getChildCount()
131      */
132     public int getChildCount() {
133         return ref.getChildCount();
134     }
135 
136     /* (non-Javadoc)
137      * @see org.crosswire.jsword.passage.Key#getCardinality()
138      */
139     public int getCardinality() {
140         return ref.getCardinality();
141     }
142 
143     /* (non-Javadoc)
144      * @see java.lang.Iterable#iterator()
145      */
146     public Iterator<Key> iterator() {
147         return ref.iterator();
148     }
149 
150     /* (non-Javadoc)
151      * @see org.crosswire.jsword.passage.Key#get(int)
152      */
153     public Key get(int index) {
154         return ref.get(index);
155     }
156 
157     /* (non-Javadoc)
158      * @see org.crosswire.jsword.passage.Key#indexOf(org.crosswire.jsword.passage.Key)
159      */
160     public int indexOf(Key that) {
161         return ref.indexOf(that);
162     }
163 
164     /* (non-Javadoc)
165      * @see org.crosswire.jsword.passage.Key#getParent()
166      */
167     public Key getParent() {
168         return ref.getParent();
169     }
170 
171     /* (non-Javadoc)
172      * @see org.crosswire.jsword.passage.Key#getName()
173      */
174     public String getName() {
175         return ref.getName();
176     }
177 
178     /* (non-Javadoc)
179      * @see org.crosswire.jsword.passage.Key#getName(org.crosswire.jsword.passage.Key)
180      */
181     public String getName(Key base) {
182         return ref.getName(base);
183     }
184 
185     /* (non-Javadoc)
186      * @see org.crosswire.jsword.passage.Key#getRootName()
187      */
188     public String getRootName() {
189         return ref.getRootName();
190     }
191 
192     /* (non-Javadoc)
193      * @see org.crosswire.jsword.passage.Key#getOsisRef()
194      */
195     public String getOsisRef() {
196         return ref.getOsisRef();
197     }
198 
199     /* (non-Javadoc)
200      * @see org.crosswire.jsword.passage.Key#getOsisID()
201      */
202     public String getOsisID() {
203         return ref.getOsisID();
204     }
205 
206     /* (non-Javadoc)
207      * @see org.crosswire.jsword.passage.Passage#getOverview()
208      */
209     public String getOverview() {
210         return ref.getOverview();
211     }
212 
213     /* (non-Javadoc)
214      * @see org.crosswire.jsword.passage.Key#isEmpty()
215      */
216     public boolean isEmpty() {
217         return ref.isEmpty();
218     }
219 
220     /* (non-Javadoc)
221      * @see org.crosswire.jsword.passage.Passage#countVerses()
222      */
223     public int countVerses() {
224         return ref.countVerses();
225     }
226 
227     /* (non-Javadoc)
228      * @see org.crosswire.jsword.passage.Passage#hasRanges(org.crosswire.jsword.passage.RestrictionType)
229      */
230     public boolean hasRanges(RestrictionType restrict) {
231         return ref.hasRanges(restrict);
232     }
233 
234     /* (non-Javadoc)
235      * @see org.crosswire.jsword.passage.Passage#countRanges(org.crosswire.jsword.passage.RestrictionType)
236      */
237     public int countRanges(RestrictionType restrict) {
238         return ref.countRanges(restrict);
239     }
240 
241     /* (non-Javadoc)
242      * @see org.crosswire.jsword.passage.Passage#trimVerses(int)
243      */
244     public Passage trimVerses(int count) {
245         return ref.trimVerses(count);
246     }
247 
248     /* (non-Javadoc)
249      * @see org.crosswire.jsword.passage.Passage#trimRanges(int, org.crosswire.jsword.passage.RestrictionType)
250      */
251     public Passage trimRanges(int count, RestrictionType restrict) {
252         return ref.trimRanges(count, restrict);
253     }
254 
255     /* (non-Javadoc)
256      * @see org.crosswire.jsword.passage.Passage#booksInPassage()
257      */
258     public int booksInPassage() {
259         return ref.booksInPassage();
260     }
261 
262     /* (non-Javadoc)
263      * @see org.crosswire.jsword.passage.Passage#getVerseAt(int)
264      */
265     public Verse getVerseAt(int offset) throws ArrayIndexOutOfBoundsException {
266         return ref.getVerseAt(offset);
267     }
268 
269     /* (non-Javadoc)
270      * @see org.crosswire.jsword.passage.Passage#getRangeAt(int, org.crosswire.jsword.passage.RestrictionType)
271      */
272     public VerseRange getRangeAt(int offset, RestrictionType restrict) throws ArrayIndexOutOfBoundsException {
273         return ref.getRangeAt(offset, restrict);
274     }
275 
276     /* (non-Javadoc)
277      * @see org.crosswire.jsword.passage.Passage#rangeIterator(org.crosswire.jsword.passage.RestrictionType)
278      */
279     public Iterator<VerseRange> rangeIterator(RestrictionType restrict) {
280         return ref.rangeIterator(restrict);
281     }
282 
283     /* (non-Javadoc)
284      * @see org.crosswire.jsword.passage.Passage#add(org.crosswire.jsword.passage.Key)
285      */
286     public void add(Key that) {
287         if (ignore) {
288             return;
289         }
290 
291         throw new IllegalStateException(JSOtherMsg.lookupText("Cannot alter a read-only passage"));
292     }
293 
294     /* (non-Javadoc)
295      * @see org.crosswire.jsword.passage.Passage#remove(org.crosswire.jsword.passage.Key)
296      */
297     public void remove(Key that) {
298         if (ignore) {
299             return;
300         }
301 
302         throw new IllegalStateException(JSOtherMsg.lookupText("Cannot alter a read-only passage"));
303     }
304 
305     /* (non-Javadoc)
306      * @see org.crosswire.jsword.passage.Passage#containsAll(org.crosswire.jsword.passage.Passage)
307      */
308     public boolean containsAll(Passage that) {
309         return ref.containsAll(that);
310     }
311 
312     /* (non-Javadoc)
313      * @see org.crosswire.jsword.passage.Key#clear()
314      */
315     public void clear() {
316         if (ignore) {
317             return;
318         }
319 
320         throw new IllegalStateException(JSOtherMsg.lookupText("Cannot alter a read-only passage"));
321     }
322 
323     /* (non-Javadoc)
324      * @see org.crosswire.jsword.passage.Key#blur(int, org.crosswire.jsword.passage.RestrictionType)
325      */
326     public void blur(int by, RestrictionType restrict) {
327         if (ignore) {
328             return;
329         }
330 
331         throw new IllegalStateException(JSOtherMsg.lookupText("Cannot alter a read-only passage"));
332     }
333 
334     /* (non-Javadoc)
335      * @see org.crosswire.jsword.passage.Passage#readDescription(java.io.Reader)
336      */
337     public void readDescription(Reader in) {
338         if (ignore) {
339             return;
340         }
341 
342         throw new IllegalStateException(JSOtherMsg.lookupText("Cannot alter a read-only passage"));
343     }
344 
345     /* (non-Javadoc)
346      * @see org.crosswire.jsword.passage.Passage#writeDescription(java.io.Writer)
347      */
348     public void writeDescription(Writer out) throws IOException {
349         ref.writeDescription(out);
350     }
351 
352     /* (non-Javadoc)
353      * @see org.crosswire.jsword.passage.Passage#optimizeReads()
354      */
355     public void optimizeReads() {
356         ref.optimizeReads();
357     }
358 
359     /* (non-Javadoc)
360      * @see org.crosswire.jsword.passage.Passage#addPassageListener(org.crosswire.jsword.passage.PassageListener)
361      */
362     public void addPassageListener(PassageListener li) {
363     }
364 
365     /* (non-Javadoc)
366      * @see org.crosswire.jsword.passage.Passage#removePassageListener(org.crosswire.jsword.passage.PassageListener)
367      */
368     public void removePassageListener(PassageListener li) {
369     }
370 
371     @Override
372     public boolean equals(Object obj) {
373         return ref.equals(obj);
374     }
375 
376     @Override
377     public int hashCode() {
378         return ref.hashCode();
379     }
380 
381     @Override
382     public String toString() {
383         return ref.toString();
384     }
385 
386     @Override
387     public ReadOnlyPassage clone() {
388         ReadOnlyPassage clone = null;
389         try {
390             clone = (ReadOnlyPassage) super.clone();
391             clone.ref = this.ref;
392             clone.ignore = this.ignore;
393         } catch (CloneNotSupportedException e) {
394             assert false : e;
395         }
396 
397         return clone;
398     }
399 
400     /* (non-Javadoc)
401      * @see java.lang.Comparable#compareTo(java.lang.Object)
402      */
403     public int compareTo(Key o) {
404         return ref.compareTo(o);
405     }
406 
407     /**
408      * The object we are proxying to
409      */
410     private Passage ref;
411 
412     /**
413      * Do we just silently ignore change attempts or throw up
414      */
415     private boolean ignore;
416 
417     /**
418      * Serialization ID
419      */
420     private static final long serialVersionUID = 3257853173036102193L;
421 
422 }
423