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.util.Iterator;
23  
24  import org.crosswire.jsword.JSOtherMsg;
25  
26  /**
27   * A read-only wrapper around any writable implementation of Key.
28   * 
29   * @see gnu.lgpl.License The GNU Lesser General Public License for details.
30   * @author Joe Walker
31   */
32  public class ReadOnlyKeyList implements Key {
33      /**
34       * Simple ctor
35       * 
36       * @param keys the keys
37       * @param ignore 
38       */
39      public ReadOnlyKeyList(Key keys, boolean ignore) {
40          this.keys = keys;
41          this.ignore = ignore;
42      }
43  
44      /* (non-Javadoc)
45       * @see org.crosswire.jsword.passage.Key#getCardinality()
46       */
47      public int getCardinality() {
48          return keys.getCardinality();
49      }
50  
51      /* (non-Javadoc)
52       * @see org.crosswire.jsword.passage.Key#canHaveChildren()
53       */
54      public boolean canHaveChildren() {
55          return keys.canHaveChildren();
56      }
57  
58      /* (non-Javadoc)
59       * @see org.crosswire.jsword.passage.Key#getChildCount()
60       */
61      public int getChildCount() {
62          return keys.getChildCount();
63      }
64  
65      /* (non-Javadoc)
66       * @see org.crosswire.jsword.passage.Key#isEmpty()
67       */
68      public boolean isEmpty() {
69          return keys.isEmpty();
70      }
71  
72      /* (non-Javadoc)
73       * @see org.crosswire.jsword.passage.Key#contains(org.crosswire.jsword.passage.Key)
74       */
75      public boolean contains(Key key) {
76          return keys.contains(key);
77      }
78  
79      /* (non-Javadoc)
80       * @see java.lang.Iterable#iterator()
81       */
82      public Iterator<Key> iterator() {
83          return keys.iterator();
84      }
85  
86      /* (non-Javadoc)
87       * @see org.crosswire.jsword.passage.Key#addAll(org.crosswire.jsword.passage.Key)
88       */
89      public void addAll(Key key) {
90          if (ignore) {
91              return;
92          }
93  
94          throw new IllegalStateException(JSOtherMsg.lookupText("Cannot alter a read-only key list"));
95      }
96  
97      /* (non-Javadoc)
98       * @see org.crosswire.jsword.passage.Key#removeAll(org.crosswire.jsword.passage.Key)
99       */
100     public void removeAll(Key key) {
101         if (ignore) {
102             return;
103         }
104 
105         throw new IllegalStateException(JSOtherMsg.lookupText("Cannot alter a read-only key list"));
106     }
107 
108     /* (non-Javadoc)
109      * @see org.crosswire.jsword.passage.Key#retainAll(org.crosswire.jsword.passage.Key)
110      */
111     public void retainAll(Key key) {
112         if (ignore) {
113             return;
114         }
115 
116         throw new IllegalStateException(JSOtherMsg.lookupText("Cannot alter a read-only key list"));
117     }
118 
119     /* (non-Javadoc)
120      * @see org.crosswire.jsword.passage.Key#clear()
121      */
122     public void clear() {
123         if (ignore) {
124             return;
125         }
126 
127         throw new IllegalStateException(JSOtherMsg.lookupText("Cannot alter a read-only key list"));
128     }
129 
130     /* (non-Javadoc)
131      * @see org.crosswire.jsword.passage.Key#getName()
132      */
133     public String getName() {
134         return keys.getName();
135     }
136 
137     /* (non-Javadoc)
138      * @see org.crosswire.jsword.passage.Key#getName(org.crosswire.jsword.passage.Key)
139      */
140     public String getName(Key base) {
141         return keys.getName(base);
142     }
143 
144     /* (non-Javadoc)
145      * @see org.crosswire.jsword.passage.Key#getRootName()
146      */
147     public String getRootName() {
148         return keys.getRootName();
149     }
150 
151     /* (non-Javadoc)
152      * @see org.crosswire.jsword.passage.Key#getOsisRef()
153      */
154     public String getOsisRef() {
155         return keys.getOsisRef();
156     }
157 
158     /* (non-Javadoc)
159      * @see org.crosswire.jsword.passage.Key#getOsisID()
160      */
161     public String getOsisID() {
162         return keys.getOsisID();
163     }
164 
165     @Override
166     public int hashCode() {
167         return keys.hashCode();
168     }
169 
170     @Override
171     public boolean equals(Object obj) {
172         return keys.equals(obj);
173     }
174 
175     /* (non-Javadoc)
176      * @see java.lang.Comparable#compareTo(java.lang.Object)
177      */
178     public int compareTo(Key o) {
179         return keys.compareTo(o);
180     }
181 
182    /* (non-Javadoc)
183      * @see org.crosswire.jsword.passage.Key#get(int)
184      */
185     public Key get(int index) {
186         return keys.get(index);
187     }
188 
189     /* (non-Javadoc)
190      * @see org.crosswire.jsword.passage.Key#indexOf(org.crosswire.jsword.passage.Key)
191      */
192     public int indexOf(Key that) {
193         return keys.indexOf(that);
194     }
195 
196     /* (non-Javadoc)
197      * @see org.crosswire.jsword.passage.Key#getParent()
198      */
199     public Key getParent() {
200         return keys.getParent();
201     }
202 
203     /* (non-Javadoc)
204      * @see org.crosswire.jsword.passage.Key#blur(int, org.crosswire.jsword.passage.RestrictionType)
205      */
206     public void blur(int by, RestrictionType restrict) {
207         if (ignore) {
208             return;
209         }
210 
211         throw new IllegalStateException(JSOtherMsg.lookupText("Cannot alter a read-only key list"));
212     }
213 
214     @Override
215     public ReadOnlyKeyList clone() {
216         ReadOnlyKeyList clone = null;
217         try {
218             clone = (ReadOnlyKeyList) super.clone();
219             clone.keys = keys.clone();
220         } catch (CloneNotSupportedException e) {
221             assert false : e;
222         }
223         return clone;
224     }
225 
226     /**
227      * Do we ignore write requests or throw?
228      */
229     private boolean ignore;
230 
231     /**
232      * The Key to which we proxy
233      */
234     private Key keys;
235 
236     /**
237      * Serialization ID
238      */
239     private static final long serialVersionUID = -7947159638198641657L;
240 }
241