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