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