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