applied LGPLv3 and source code formatting.
[vamsas.git] / src / uk / ac / vamsas / objects / core / Treenode.java
1 /*\r
2  * This file is part of the Vamsas Client version 0.1. \r
3  * Copyright 2009 by Jim Procter, Iain Milne, Pierre Marguerite, \r
4  *  Andrew Waterhouse and Dominik Lindner.\r
5  * \r
6  * Earlier versions have also been incorporated into Jalview version 2.4 \r
7  * since 2008, and TOPALi version 2 since 2007.\r
8  * \r
9  * The Vamsas Client is free software: you can redistribute it and/or modify\r
10  * it under the terms of the GNU Lesser General Public License as published by\r
11  * the Free Software Foundation, either version 3 of the License, or\r
12  * (at your option) any later version.\r
13  *  \r
14  * The Vamsas Client is distributed in the hope that it will be useful,\r
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
17  * GNU Lesser General Public License for more details.\r
18  * \r
19  * You should have received a copy of the GNU Lesser General Public License\r
20  * along with the Vamsas Client.  If not, see <http://www.gnu.org/licenses/>.\r
21  */\r
22 package uk.ac.vamsas.objects.core;\r
23 \r
24 //---------------------------------/\r
25 //- Imported classes and packages -/\r
26 //---------------------------------/\r
27 \r
28 import org.exolab.castor.xml.Marshaller;\r
29 import org.exolab.castor.xml.Unmarshaller;\r
30 \r
31 /**\r
32  * node identity and mapping data between tree representations and vamsas\r
33  * document objects\r
34  * \r
35  * @version $Revision$ $Date$\r
36  */\r
37 public class Treenode extends uk.ac.vamsas.objects.core.NodeType implements\r
38     java.io.Serializable {\r
39 \r
40   // --------------------------/\r
41   // - Class/Member Variables -/\r
42   // --------------------------/\r
43 \r
44   /**\r
45    * reference to one or more trees containing the node being described.\r
46    */\r
47   private java.util.Vector _treeId;\r
48 \r
49   /**\r
50    * String uniquely identifying a particular node in the referenced tree\r
51    * according to the format of the tree representation that is referenced.\r
52    * \r
53    */\r
54   private java.lang.String _nodespec;\r
55 \r
56   // ----------------/\r
57   // - Constructors -/\r
58   // ----------------/\r
59 \r
60   public Treenode() {\r
61     super();\r
62     this._treeId = new java.util.Vector();\r
63   }\r
64 \r
65   // -----------/\r
66   // - Methods -/\r
67   // -----------/\r
68 \r
69   /**\r
70    * \r
71    * \r
72    * @param vTreeId\r
73    * @throws java.lang.IndexOutOfBoundsException\r
74    *           if the index given is outside the bounds of the collection\r
75    */\r
76   public void addTreeId(final java.lang.Object vTreeId)\r
77       throws java.lang.IndexOutOfBoundsException {\r
78     this._treeId.addElement(vTreeId);\r
79   }\r
80 \r
81   /**\r
82    * \r
83    * \r
84    * @param index\r
85    * @param vTreeId\r
86    * @throws java.lang.IndexOutOfBoundsException\r
87    *           if the index given is outside the bounds of the collection\r
88    */\r
89   public void addTreeId(final int index, final java.lang.Object vTreeId)\r
90       throws java.lang.IndexOutOfBoundsException {\r
91     this._treeId.add(index, vTreeId);\r
92   }\r
93 \r
94   /**\r
95    * Method enumerateTreeId.\r
96    * \r
97    * @return an Enumeration over all java.lang.Object elements\r
98    */\r
99   public java.util.Enumeration enumerateTreeId() {\r
100     return this._treeId.elements();\r
101   }\r
102 \r
103   /**\r
104    * Overrides the java.lang.Object.equals method.\r
105    * \r
106    * @param obj\r
107    * @return true if the objects are equal.\r
108    */\r
109   public boolean equals(final java.lang.Object obj) {\r
110     if (this == obj)\r
111       return true;\r
112 \r
113     if (super.equals(obj) == false)\r
114       return false;\r
115 \r
116     if (obj instanceof Treenode) {\r
117 \r
118       Treenode temp = (Treenode) obj;\r
119       boolean thcycle;\r
120       boolean tmcycle;\r
121       if (this._treeId != null) {\r
122         if (temp._treeId == null)\r
123           return false;\r
124         if (this._treeId != temp._treeId) {\r
125           thcycle = org.castor.util.CycleBreaker.startingToCycle(this._treeId);\r
126           tmcycle = org.castor.util.CycleBreaker.startingToCycle(temp._treeId);\r
127           if (thcycle != tmcycle) {\r
128             if (!thcycle) {\r
129               org.castor.util.CycleBreaker.releaseCycleHandle(this._treeId);\r
130             }\r
131             ;\r
132             if (!tmcycle) {\r
133               org.castor.util.CycleBreaker.releaseCycleHandle(temp._treeId);\r
134             }\r
135             ;\r
136             return false;\r
137           }\r
138           if (!thcycle) {\r
139             if (!this._treeId.equals(temp._treeId)) {\r
140               org.castor.util.CycleBreaker.releaseCycleHandle(this._treeId);\r
141               org.castor.util.CycleBreaker.releaseCycleHandle(temp._treeId);\r
142               return false;\r
143             }\r
144             org.castor.util.CycleBreaker.releaseCycleHandle(this._treeId);\r
145             org.castor.util.CycleBreaker.releaseCycleHandle(temp._treeId);\r
146           }\r
147         }\r
148       } else if (temp._treeId != null)\r
149         return false;\r
150       if (this._nodespec != null) {\r
151         if (temp._nodespec == null)\r
152           return false;\r
153         if (this._nodespec != temp._nodespec) {\r
154           thcycle = org.castor.util.CycleBreaker\r
155               .startingToCycle(this._nodespec);\r
156           tmcycle = org.castor.util.CycleBreaker\r
157               .startingToCycle(temp._nodespec);\r
158           if (thcycle != tmcycle) {\r
159             if (!thcycle) {\r
160               org.castor.util.CycleBreaker.releaseCycleHandle(this._nodespec);\r
161             }\r
162             ;\r
163             if (!tmcycle) {\r
164               org.castor.util.CycleBreaker.releaseCycleHandle(temp._nodespec);\r
165             }\r
166             ;\r
167             return false;\r
168           }\r
169           if (!thcycle) {\r
170             if (!this._nodespec.equals(temp._nodespec)) {\r
171               org.castor.util.CycleBreaker.releaseCycleHandle(this._nodespec);\r
172               org.castor.util.CycleBreaker.releaseCycleHandle(temp._nodespec);\r
173               return false;\r
174             }\r
175             org.castor.util.CycleBreaker.releaseCycleHandle(this._nodespec);\r
176             org.castor.util.CycleBreaker.releaseCycleHandle(temp._nodespec);\r
177           }\r
178         }\r
179       } else if (temp._nodespec != null)\r
180         return false;\r
181       return true;\r
182     }\r
183     return false;\r
184   }\r
185 \r
186   /**\r
187    * Returns the value of field 'nodespec'. The field 'nodespec' has the\r
188    * following description: String uniquely identifying a particular node in the\r
189    * referenced tree according to the format of the tree representation that is\r
190    * referenced.\r
191    * \r
192    * \r
193    * @return the value of field 'Nodespec'.\r
194    */\r
195   public java.lang.String getNodespec() {\r
196     return this._nodespec;\r
197   }\r
198 \r
199   /**\r
200    * Method getTreeId.\r
201    * \r
202    * @param index\r
203    * @throws java.lang.IndexOutOfBoundsException\r
204    *           if the index given is outside the bounds of the collection\r
205    * @return the value of the java.lang.Object at the given index\r
206    */\r
207   public java.lang.Object getTreeId(final int index)\r
208       throws java.lang.IndexOutOfBoundsException {\r
209     // check bounds for index\r
210     if (index < 0 || index >= this._treeId.size()) {\r
211       throw new IndexOutOfBoundsException("getTreeId: Index value '" + index\r
212           + "' not in range [0.." + (this._treeId.size() - 1) + "]");\r
213     }\r
214 \r
215     return _treeId.get(index);\r
216   }\r
217 \r
218   /**\r
219    * Method getTreeId.Returns the contents of the collection in an Array.\r
220    * <p>\r
221    * Note: Just in case the collection contents are changing in another thread,\r
222    * we pass a 0-length Array of the correct type into the API call. This way we\r
223    * <i>know</i> that the Array returned is of exactly the correct length.\r
224    * \r
225    * @return this collection as an Array\r
226    */\r
227   public java.lang.Object[] getTreeId() {\r
228     java.lang.Object[] array = new java.lang.Object[0];\r
229     return (java.lang.Object[]) this._treeId.toArray(array);\r
230   }\r
231 \r
232   /**\r
233    * Method getTreeIdAsReference.Returns a reference to '_treeId'. No type\r
234    * checking is performed on any modifications to the Vector.\r
235    * \r
236    * @return a reference to the Vector backing this class\r
237    */\r
238   public java.util.Vector getTreeIdAsReference() {\r
239     return this._treeId;\r
240   }\r
241 \r
242   /**\r
243    * Method getTreeIdCount.\r
244    * \r
245    * @return the size of this collection\r
246    */\r
247   public int getTreeIdCount() {\r
248     return this._treeId.size();\r
249   }\r
250 \r
251   /**\r
252    * Overrides the java.lang.Object.hashCode method.\r
253    * <p>\r
254    * The following steps came from <b>Effective Java Programming Language\r
255    * Guide</b> by Joshua Bloch, Chapter 3\r
256    * \r
257    * @return a hash code value for the object.\r
258    */\r
259   public int hashCode() {\r
260     int result = super.hashCode();\r
261 \r
262     long tmp;\r
263     if (_treeId != null\r
264         && !org.castor.util.CycleBreaker.startingToCycle(_treeId)) {\r
265       result = 37 * result + _treeId.hashCode();\r
266       org.castor.util.CycleBreaker.releaseCycleHandle(_treeId);\r
267     }\r
268     if (_nodespec != null\r
269         && !org.castor.util.CycleBreaker.startingToCycle(_nodespec)) {\r
270       result = 37 * result + _nodespec.hashCode();\r
271       org.castor.util.CycleBreaker.releaseCycleHandle(_nodespec);\r
272     }\r
273 \r
274     return result;\r
275   }\r
276 \r
277   /**\r
278    * Method isValid.\r
279    * \r
280    * @return true if this object is valid according to the schema\r
281    */\r
282   public boolean isValid() {\r
283     try {\r
284       validate();\r
285     } catch (org.exolab.castor.xml.ValidationException vex) {\r
286       return false;\r
287     }\r
288     return true;\r
289   }\r
290 \r
291   /**\r
292    * \r
293    * \r
294    * @param out\r
295    * @throws org.exolab.castor.xml.MarshalException\r
296    *           if object is null or if any SAXException is thrown during\r
297    *           marshaling\r
298    * @throws org.exolab.castor.xml.ValidationException\r
299    *           if this object is an invalid instance according to the schema\r
300    */\r
301   public void marshal(final java.io.Writer out)\r
302       throws org.exolab.castor.xml.MarshalException,\r
303       org.exolab.castor.xml.ValidationException {\r
304     Marshaller.marshal(this, out);\r
305   }\r
306 \r
307   /**\r
308    * \r
309    * \r
310    * @param handler\r
311    * @throws java.io.IOException\r
312    *           if an IOException occurs during marshaling\r
313    * @throws org.exolab.castor.xml.ValidationException\r
314    *           if this object is an invalid instance according to the schema\r
315    * @throws org.exolab.castor.xml.MarshalException\r
316    *           if object is null or if any SAXException is thrown during\r
317    *           marshaling\r
318    */\r
319   public void marshal(final org.xml.sax.ContentHandler handler)\r
320       throws java.io.IOException, org.exolab.castor.xml.MarshalException,\r
321       org.exolab.castor.xml.ValidationException {\r
322     Marshaller.marshal(this, handler);\r
323   }\r
324 \r
325   /**\r
326      */\r
327   public void removeAllTreeId() {\r
328     this._treeId.clear();\r
329   }\r
330 \r
331   /**\r
332    * Method removeTreeId.\r
333    * \r
334    * @param vTreeId\r
335    * @return true if the object was removed from the collection.\r
336    */\r
337   public boolean removeTreeId(final java.lang.Object vTreeId) {\r
338     boolean removed = _treeId.remove(vTreeId);\r
339     return removed;\r
340   }\r
341 \r
342   /**\r
343    * Method removeTreeIdAt.\r
344    * \r
345    * @param index\r
346    * @return the element removed from the collection\r
347    */\r
348   public java.lang.Object removeTreeIdAt(final int index) {\r
349     java.lang.Object obj = this._treeId.remove(index);\r
350     return obj;\r
351   }\r
352 \r
353   /**\r
354    * Sets the value of field 'nodespec'. The field 'nodespec' has the following\r
355    * description: String uniquely identifying a particular node in the\r
356    * referenced tree according to the format of the tree representation that is\r
357    * referenced.\r
358    * \r
359    * \r
360    * @param nodespec\r
361    *          the value of field 'nodespec'.\r
362    */\r
363   public void setNodespec(final java.lang.String nodespec) {\r
364     this._nodespec = nodespec;\r
365   }\r
366 \r
367   /**\r
368    * \r
369    * \r
370    * @param index\r
371    * @param vTreeId\r
372    * @throws java.lang.IndexOutOfBoundsException\r
373    *           if the index given is outside the bounds of the collection\r
374    */\r
375   public void setTreeId(final int index, final java.lang.Object vTreeId)\r
376       throws java.lang.IndexOutOfBoundsException {\r
377     // check bounds for index\r
378     if (index < 0 || index >= this._treeId.size()) {\r
379       throw new IndexOutOfBoundsException("setTreeId: Index value '" + index\r
380           + "' not in range [0.." + (this._treeId.size() - 1) + "]");\r
381     }\r
382 \r
383     this._treeId.set(index, vTreeId);\r
384   }\r
385 \r
386   /**\r
387    * \r
388    * \r
389    * @param vTreeIdArray\r
390    */\r
391   public void setTreeId(final java.lang.Object[] vTreeIdArray) {\r
392     // -- copy array\r
393     _treeId.clear();\r
394 \r
395     for (int i = 0; i < vTreeIdArray.length; i++) {\r
396       this._treeId.add(vTreeIdArray[i]);\r
397     }\r
398   }\r
399 \r
400   /**\r
401    * Sets the value of '_treeId' by copying the given Vector. All elements will\r
402    * be checked for type safety.\r
403    * \r
404    * @param vTreeIdList\r
405    *          the Vector to copy.\r
406    */\r
407   public void setTreeId(final java.util.Vector vTreeIdList) {\r
408     // copy vector\r
409     this._treeId.clear();\r
410 \r
411     this._treeId.addAll(vTreeIdList);\r
412   }\r
413 \r
414   /**\r
415    * Sets the value of '_treeId' by setting it to the given Vector. No type\r
416    * checking is performed.\r
417    * \r
418    * @deprecated\r
419    * \r
420    * @param treeIdVector\r
421    *          the Vector to set.\r
422    */\r
423   public void setTreeIdAsReference(final java.util.Vector treeIdVector) {\r
424     this._treeId = treeIdVector;\r
425   }\r
426 \r
427   /**\r
428    * Method unmarshal.\r
429    * \r
430    * @param reader\r
431    * @throws org.exolab.castor.xml.MarshalException\r
432    *           if object is null or if any SAXException is thrown during\r
433    *           marshaling\r
434    * @throws org.exolab.castor.xml.ValidationException\r
435    *           if this object is an invalid instance according to the schema\r
436    * @return the unmarshaled uk.ac.vamsas.objects.core.NodeType\r
437    */\r
438   public static uk.ac.vamsas.objects.core.NodeType unmarshal(\r
439       final java.io.Reader reader)\r
440       throws org.exolab.castor.xml.MarshalException,\r
441       org.exolab.castor.xml.ValidationException {\r
442     return (uk.ac.vamsas.objects.core.NodeType) Unmarshaller.unmarshal(\r
443         uk.ac.vamsas.objects.core.Treenode.class, reader);\r
444   }\r
445 \r
446   /**\r
447    * \r
448    * \r
449    * @throws org.exolab.castor.xml.ValidationException\r
450    *           if this object is an invalid instance according to the schema\r
451    */\r
452   public void validate() throws org.exolab.castor.xml.ValidationException {\r
453     org.exolab.castor.xml.Validator validator = new org.exolab.castor.xml.Validator();\r
454     validator.validate(this);\r
455   }\r
456 \r
457 }\r