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: TreeNode.java 2110 2011-03-08 13:55:32Z dmsmith $
21   */
22  package org.crosswire.jsword.book.sword;
23  
24  import java.io.Serializable;
25  
26  /**
27   * A node that knows where the data is in the real file and where it is in
28   * relationship to other nodes.
29   * 
30   * @see gnu.lgpl.License for license details.<br>
31   *      The copyright to this program is held by it's authors.
32   * @author DM Smith [dmsmith555 at yahoo dot com]
33   */
34  class TreeNode implements Cloneable, Serializable {
35      /**
36       * TreeNode default ctor.
37       */
38      TreeNode() {
39          this(-1);
40      }
41  
42      /**
43       * Setup with the positions of data in the file
44       * 
45       * @param theOffset
46       */
47      TreeNode(int theOffset) {
48          offset = theOffset;
49          name = "";
50          parent = -1;
51          nextSibling = -1;
52          firstChild = -1;
53          userData = new byte[0];
54      }
55  
56      /**
57       * @return the offset
58       */
59      public int getOffset() {
60          return offset;
61      }
62  
63      /**
64       * @param newOffset
65       *            the offset to set
66       */
67      public void setOffset(int newOffset) {
68          offset = newOffset;
69      }
70  
71      /**
72       * @return the name
73       */
74      public String getName() {
75          return name;
76      }
77  
78      /**
79       * @param newName
80       *            the name to set
81       */
82      public void setName(String newName) {
83          name = newName;
84      }
85  
86      /**
87       * @return the userData
88       */
89      public byte[] getUserData() {
90          return userData.clone();
91      }
92  
93      /**
94       * @param theUserData
95       *            the userData to set
96       */
97      public void setUserData(byte[] theUserData) {
98          userData = theUserData.clone();
99      }
100 
101     /**
102      * @return the firstChild
103      */
104     public int getFirstChild() {
105         return firstChild;
106     }
107 
108     /**
109      * @return whether there are children
110      */
111     public boolean hasChildren() {
112         return firstChild != -1;
113     }
114 
115     /**
116      * @param firstChild
117      *            the firstChild to set
118      */
119     public void setFirstChild(int firstChild) {
120         this.firstChild = firstChild;
121     }
122 
123     /**
124      * @return the nextSibling
125      */
126     public int getNextSibling() {
127         return nextSibling;
128     }
129 
130     /**
131      * @return if there are more siblings
132      */
133     public boolean hasNextSibling() {
134         return nextSibling != -1;
135     }
136 
137     /**
138      * @param nextSibling
139      *            the nextSibling to set
140      */
141     public void setNextSibling(int nextSibling) {
142         this.nextSibling = nextSibling;
143     }
144 
145     /**
146      * @return the parent
147      */
148     public int getParent() {
149         return parent;
150     }
151 
152     /**
153      * @param parent
154      *            the parent to set
155      */
156     public void setParent(int parent) {
157         this.parent = parent;
158     }
159 
160     @Override
161     public TreeNode clone() {
162         TreeNode clone = null;
163         try {
164             clone = (TreeNode) super.clone();
165         } catch (CloneNotSupportedException e) {
166             assert false : e;
167         }
168         return clone;
169     }
170 
171     /**
172      * The offset of this TreeNode in the offset.
173      */
174     private int offset;
175 
176     /**
177      * The name of this TreeNode. Note, this is not the path. To get the path,
178      * one needs to traverse to the parent to construct the path.
179      */
180     private String name;
181 
182     /**
183      * Optional, extra data associated with this TreeNode. For example, this is
184      * used to store offset and length for a raw genbook.
185      */
186     private byte[] userData;
187 
188     /**
189      * The offset of the parent record in the offset. Root nodes are indicated
190      * with a value of -1. That is, this TreeNode does not have a parent.
191      */
192     private int parent;
193 
194     /**
195      * The offset of the next sibling record in the offset. Final siblings are
196      * indicated with a value of -1. That is, this TreeNode does not have a next
197      * sibling.
198      */
199     private int nextSibling;
200 
201     /**
202      * The offset of the first child record in the offset. Leaf nodes are
203      * indicated with a value of -1. That is, this TreeNode does not have any
204      * children.
205      */
206     private int firstChild;
207 
208     /**
209      * Serialization ID
210      */
211     private static final long serialVersionUID = -2472601787934480762L;
212 
213 }
214