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.common.util.ItemIterator;
25  
26  /**
27   * A simple default implementation of the Key interface.
28   * 
29   * @see gnu.lgpl.License The GNU Lesser General Public License for details.
30   * @author Joe Walker
31   */
32  public class DefaultLeafKeyList implements Key {
33      /**
34       * Default ctor
35       * @param name 
36       */
37      public DefaultLeafKeyList(String name) {
38          this(name, name, null);
39      }
40  
41      /**
42       * Default ctor
43       * @param name 
44       * @param osisName 
45       */
46      public DefaultLeafKeyList(String name, String osisName) {
47          this(name, osisName, null);
48      }
49  
50      /**
51       * Default ctor
52       * @param name 
53       * @param osisName 
54       * @param parent 
55       */
56      public DefaultLeafKeyList(String name, String osisName, Key parent) {
57          this.name = name;
58          this.parent = parent;
59          this.osisName = osisName;
60      }
61  
62      /* (non-Javadoc)
63       * @see org.crosswire.jsword.passage.Key#canHaveChildren()
64       */
65      public boolean canHaveChildren() {
66          return false;
67      }
68  
69      /* (non-Javadoc)
70       * @see org.crosswire.jsword.passage.Key#getChildCount()
71       */
72      public int getChildCount() {
73          return 0;
74      }
75  
76      /* (non-Javadoc)
77       * @see org.crosswire.jsword.passage.Key#getName()
78       */
79      public String getName() {
80          return name;
81      }
82  
83      /* (non-Javadoc)
84       * @see org.crosswire.jsword.passage.Key#getName(org.crosswire.jsword.passage.Key)
85       */
86      public String getName(Key base) {
87          return getName();
88      }
89  
90      /* (non-Javadoc)
91       * @see org.crosswire.jsword.passage.Key#getRootName()
92       */
93      public String getRootName() {
94          return getName();
95      }
96  
97      /* (non-Javadoc)
98       * @see org.crosswire.jsword.passage.Key#getOsisRef()
99       */
100     public String getOsisRef() {
101         return osisName;
102     }
103 
104     /* (non-Javadoc)
105      * @see org.crosswire.jsword.passage.Key#getOsisID()
106      */
107     public String getOsisID() {
108         return getOsisRef();
109     }
110 
111     /* (non-Javadoc)
112      * @see org.crosswire.jsword.passage.Key#getParent()
113      */
114     public Key getParent() {
115         return parent;
116     }
117 
118     /* (non-Javadoc)
119      * @see org.crosswire.jsword.passage.Key#getCardinality()
120      */
121     public int getCardinality() {
122         return 1;
123     }
124 
125     /* (non-Javadoc)
126      * @see org.crosswire.jsword.passage.Key#isEmpty()
127      */
128     public boolean isEmpty() {
129         return false;
130     }
131 
132     /* (non-Javadoc)
133      * @see org.crosswire.jsword.passage.Key#contains(org.crosswire.jsword.passage.Key)
134      */
135     public boolean contains(Key key) {
136         return this.equals(key);
137     }
138 
139     /* (non-Javadoc)
140      * @see java.lang.Iterable#iterator()
141      */
142     public Iterator<Key> iterator() {
143         return new ItemIterator<Key>(this);
144     }
145 
146     /* (non-Javadoc)
147      * @see org.crosswire.jsword.passage.Key#addAll(org.crosswire.jsword.passage.Key)
148      */
149     public void addAll(Key key) {
150         throw new UnsupportedOperationException();
151     }
152 
153     /* (non-Javadoc)
154      * @see org.crosswire.jsword.passage.Key#removeAll(org.crosswire.jsword.passage.Key)
155      */
156     public void removeAll(Key key) {
157         throw new UnsupportedOperationException();
158     }
159 
160     /* (non-Javadoc)
161      * @see org.crosswire.jsword.passage.Key#retainAll(org.crosswire.jsword.passage.Key)
162      */
163     public void retainAll(Key key) {
164         throw new UnsupportedOperationException();
165     }
166 
167     /* (non-Javadoc)
168      * @see org.crosswire.jsword.passage.Key#clear()
169      */
170     public void clear() {
171     }
172 
173     /* (non-Javadoc)
174      * @see org.crosswire.jsword.passage.Key#get(int)
175      */
176     public Key get(int index) {
177         if (index == 0) {
178             return this;
179         }
180         return null;
181     }
182 
183     /* (non-Javadoc)
184      * @see org.crosswire.jsword.passage.Key#indexOf(org.crosswire.jsword.passage.Key)
185      */
186     public int indexOf(Key that) {
187         if (this.equals(that)) {
188             return 0;
189         }
190         return -1;
191     }
192 
193     /* (non-Javadoc)
194      * @see org.crosswire.jsword.passage.Key#blur(int, org.crosswire.jsword.passage.RestrictionType)
195      */
196     public void blur(int by, RestrictionType restrict) {
197         throw new UnsupportedOperationException();
198     }
199 
200     @Override
201     public String toString() {
202         return getName();
203     }
204 
205     @Override
206     public boolean equals(Object obj) {
207         if (this == obj) {
208             return true;
209         }
210 
211         // Since this can not be null
212         if (obj == null) {
213             return false;
214         }
215 
216         // We might consider checking for equality against all Keys?
217         // However currently we don't.
218 
219         // Check that that is the same as this
220         // Don't use instanceof since that breaks inheritance
221         if (!obj.getClass().equals(this.getClass())) {
222             return false;
223         }
224 
225         // The real bit ...
226         DefaultLeafKeyList that = (DefaultLeafKeyList) obj;
227         return name.equals(that.name);
228     }
229 
230     @Override
231     public int hashCode() {
232         return name.hashCode();
233     }
234 
235     /* (non-Javadoc)
236      * @see java.lang.Comparable#compareTo(java.lang.Object)
237      */
238     public int compareTo(Key obj) {
239         DefaultLeafKeyList that = (DefaultLeafKeyList) obj;
240         return name.compareTo(that.name);
241     }
242 
243     @Override
244     public DefaultLeafKeyList clone() {
245         DefaultLeafKeyList clone = null;
246         try {
247             clone = (DefaultLeafKeyList) super.clone();
248             if (parent != null) {
249                 clone.parent = parent.clone();
250             }
251         } catch (CloneNotSupportedException e) {
252             assert false : e;
253         }
254         return clone;
255     }
256 
257     /**
258      * The parent of this key
259      */
260     private Key parent;
261 
262     /**
263      * The human readable string that this key represents
264      */
265     private String name;
266 
267     /**
268      * The OSIS version of this Key
269      */
270     private String osisName;
271 
272     /**
273      *
274      */
275     private static final long serialVersionUID = -7462556005744186622L;
276 
277 }
278