applied LGPLv3 and source code formatting.
[vamsas.git] / src / uk / ac / vamsas / objects / core / VAMSAS.java
1 /*
2  * This file is part of the Vamsas Client version 0.1. 
3  * Copyright 2009 by Jim Procter, Iain Milne, Pierre Marguerite, 
4  *  Andrew Waterhouse and Dominik Lindner.
5  * 
6  * Earlier versions have also been incorporated into Jalview version 2.4 
7  * since 2008, and TOPALi version 2 since 2007.
8  * 
9  * The Vamsas Client is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *  
14  * The Vamsas Client is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Lesser General Public License for more details.
18  * 
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with the Vamsas Client.  If not, see <http://www.gnu.org/licenses/>.
21  */
22 package uk.ac.vamsas.objects.core;
23
24 //---------------------------------/
25 //- Imported classes and packages -/
26 //---------------------------------/
27
28 import org.exolab.castor.xml.Marshaller;
29 import org.exolab.castor.xml.Unmarshaller;
30
31 /**
32  * contains unassociated trees and a number of analysis sets
33  * 
34  * 
35  * @version $Revision$ $Date: 2007-06-28 14:51:44 +0100 (Thu, 28 Jun 2007)
36  *          $
37  */
38 public class VAMSAS extends uk.ac.vamsas.client.Vobject implements
39     java.io.Serializable {
40
41   // --------------------------/
42   // - Class/Member Variables -/
43   // --------------------------/
44
45   /**
46    * Primary Key for vamsas object referencing
47    */
48   private java.lang.String _id;
49
50   /**
51    * Field _modifiable.
52    */
53   private java.lang.String _modifiable;
54
55   /**
56    * Field _treeList.
57    */
58   private java.util.Vector _treeList;
59
60   /**
61    * Field _dataSetList.
62    */
63   private java.util.Vector _dataSetList;
64
65   // ----------------/
66   // - Constructors -/
67   // ----------------/
68
69   public VAMSAS() {
70     super();
71     this._treeList = new java.util.Vector();
72     this._dataSetList = new java.util.Vector();
73   }
74
75   // -----------/
76   // - Methods -/
77   // -----------/
78
79   /**
80    * 
81    * 
82    * @param vDataSet
83    * @throws java.lang.IndexOutOfBoundsException
84    *           if the index given is outside the bounds of the collection
85    */
86   public void addDataSet(final uk.ac.vamsas.objects.core.DataSet vDataSet)
87       throws java.lang.IndexOutOfBoundsException {
88     this._dataSetList.addElement(vDataSet);
89   }
90
91   /**
92    * 
93    * 
94    * @param index
95    * @param vDataSet
96    * @throws java.lang.IndexOutOfBoundsException
97    *           if the index given is outside the bounds of the collection
98    */
99   public void addDataSet(final int index,
100       final uk.ac.vamsas.objects.core.DataSet vDataSet)
101       throws java.lang.IndexOutOfBoundsException {
102     this._dataSetList.add(index, vDataSet);
103   }
104
105   /**
106    * 
107    * 
108    * @param vTree
109    * @throws java.lang.IndexOutOfBoundsException
110    *           if the index given is outside the bounds of the collection
111    */
112   public void addTree(final uk.ac.vamsas.objects.core.Tree vTree)
113       throws java.lang.IndexOutOfBoundsException {
114     this._treeList.addElement(vTree);
115   }
116
117   /**
118    * 
119    * 
120    * @param index
121    * @param vTree
122    * @throws java.lang.IndexOutOfBoundsException
123    *           if the index given is outside the bounds of the collection
124    */
125   public void addTree(final int index,
126       final uk.ac.vamsas.objects.core.Tree vTree)
127       throws java.lang.IndexOutOfBoundsException {
128     this._treeList.add(index, vTree);
129   }
130
131   /**
132    * Method enumerateDataSet.
133    * 
134    * @return an Enumeration over all uk.ac.vamsas.objects.core.DataSet elements
135    */
136   public java.util.Enumeration enumerateDataSet() {
137     return this._dataSetList.elements();
138   }
139
140   /**
141    * Method enumerateTree.
142    * 
143    * @return an Enumeration over all uk.ac.vamsas.objects.core.Tree elements
144    */
145   public java.util.Enumeration enumerateTree() {
146     return this._treeList.elements();
147   }
148
149   /**
150    * Overrides the java.lang.Object.equals method.
151    * 
152    * @param obj
153    * @return true if the objects are equal.
154    */
155   public boolean equals(final java.lang.Object obj) {
156     if (this == obj)
157       return true;
158
159     if (super.equals(obj) == false)
160       return false;
161
162     if (obj instanceof VAMSAS) {
163
164       VAMSAS temp = (VAMSAS) obj;
165       boolean thcycle;
166       boolean tmcycle;
167       if (this._id != null) {
168         if (temp._id == null)
169           return false;
170         if (this._id != temp._id) {
171           thcycle = org.castor.util.CycleBreaker.startingToCycle(this._id);
172           tmcycle = org.castor.util.CycleBreaker.startingToCycle(temp._id);
173           if (thcycle != tmcycle) {
174             if (!thcycle) {
175               org.castor.util.CycleBreaker.releaseCycleHandle(this._id);
176             }
177             ;
178             if (!tmcycle) {
179               org.castor.util.CycleBreaker.releaseCycleHandle(temp._id);
180             }
181             ;
182             return false;
183           }
184           if (!thcycle) {
185             if (!this._id.equals(temp._id)) {
186               org.castor.util.CycleBreaker.releaseCycleHandle(this._id);
187               org.castor.util.CycleBreaker.releaseCycleHandle(temp._id);
188               return false;
189             }
190             org.castor.util.CycleBreaker.releaseCycleHandle(this._id);
191             org.castor.util.CycleBreaker.releaseCycleHandle(temp._id);
192           }
193         }
194       } else if (temp._id != null)
195         return false;
196       if (this._modifiable != null) {
197         if (temp._modifiable == null)
198           return false;
199         if (this._modifiable != temp._modifiable) {
200           thcycle = org.castor.util.CycleBreaker
201               .startingToCycle(this._modifiable);
202           tmcycle = org.castor.util.CycleBreaker
203               .startingToCycle(temp._modifiable);
204           if (thcycle != tmcycle) {
205             if (!thcycle) {
206               org.castor.util.CycleBreaker.releaseCycleHandle(this._modifiable);
207             }
208             ;
209             if (!tmcycle) {
210               org.castor.util.CycleBreaker.releaseCycleHandle(temp._modifiable);
211             }
212             ;
213             return false;
214           }
215           if (!thcycle) {
216             if (!this._modifiable.equals(temp._modifiable)) {
217               org.castor.util.CycleBreaker.releaseCycleHandle(this._modifiable);
218               org.castor.util.CycleBreaker.releaseCycleHandle(temp._modifiable);
219               return false;
220             }
221             org.castor.util.CycleBreaker.releaseCycleHandle(this._modifiable);
222             org.castor.util.CycleBreaker.releaseCycleHandle(temp._modifiable);
223           }
224         }
225       } else if (temp._modifiable != null)
226         return false;
227       if (this._treeList != null) {
228         if (temp._treeList == null)
229           return false;
230         if (this._treeList != temp._treeList) {
231           thcycle = org.castor.util.CycleBreaker
232               .startingToCycle(this._treeList);
233           tmcycle = org.castor.util.CycleBreaker
234               .startingToCycle(temp._treeList);
235           if (thcycle != tmcycle) {
236             if (!thcycle) {
237               org.castor.util.CycleBreaker.releaseCycleHandle(this._treeList);
238             }
239             ;
240             if (!tmcycle) {
241               org.castor.util.CycleBreaker.releaseCycleHandle(temp._treeList);
242             }
243             ;
244             return false;
245           }
246           if (!thcycle) {
247             if (!this._treeList.equals(temp._treeList)) {
248               org.castor.util.CycleBreaker.releaseCycleHandle(this._treeList);
249               org.castor.util.CycleBreaker.releaseCycleHandle(temp._treeList);
250               return false;
251             }
252             org.castor.util.CycleBreaker.releaseCycleHandle(this._treeList);
253             org.castor.util.CycleBreaker.releaseCycleHandle(temp._treeList);
254           }
255         }
256       } else if (temp._treeList != null)
257         return false;
258       if (this._dataSetList != null) {
259         if (temp._dataSetList == null)
260           return false;
261         if (this._dataSetList != temp._dataSetList) {
262           thcycle = org.castor.util.CycleBreaker
263               .startingToCycle(this._dataSetList);
264           tmcycle = org.castor.util.CycleBreaker
265               .startingToCycle(temp._dataSetList);
266           if (thcycle != tmcycle) {
267             if (!thcycle) {
268               org.castor.util.CycleBreaker
269                   .releaseCycleHandle(this._dataSetList);
270             }
271             ;
272             if (!tmcycle) {
273               org.castor.util.CycleBreaker
274                   .releaseCycleHandle(temp._dataSetList);
275             }
276             ;
277             return false;
278           }
279           if (!thcycle) {
280             if (!this._dataSetList.equals(temp._dataSetList)) {
281               org.castor.util.CycleBreaker
282                   .releaseCycleHandle(this._dataSetList);
283               org.castor.util.CycleBreaker
284                   .releaseCycleHandle(temp._dataSetList);
285               return false;
286             }
287             org.castor.util.CycleBreaker.releaseCycleHandle(this._dataSetList);
288             org.castor.util.CycleBreaker.releaseCycleHandle(temp._dataSetList);
289           }
290         }
291       } else if (temp._dataSetList != null)
292         return false;
293       return true;
294     }
295     return false;
296   }
297
298   /**
299    * Method getDataSet.
300    * 
301    * @param index
302    * @throws java.lang.IndexOutOfBoundsException
303    *           if the index given is outside the bounds of the collection
304    * @return the value of the uk.ac.vamsas.objects.core.DataSet at the given
305    *         index
306    */
307   public uk.ac.vamsas.objects.core.DataSet getDataSet(final int index)
308       throws java.lang.IndexOutOfBoundsException {
309     // check bounds for index
310     if (index < 0 || index >= this._dataSetList.size()) {
311       throw new IndexOutOfBoundsException("getDataSet: Index value '" + index
312           + "' not in range [0.." + (this._dataSetList.size() - 1) + "]");
313     }
314
315     return (uk.ac.vamsas.objects.core.DataSet) _dataSetList.get(index);
316   }
317
318   /**
319    * Method getDataSet.Returns the contents of the collection in an Array.
320    * <p>
321    * Note: Just in case the collection contents are changing in another thread,
322    * we pass a 0-length Array of the correct type into the API call. This way we
323    * <i>know</i> that the Array returned is of exactly the correct length.
324    * 
325    * @return this collection as an Array
326    */
327   public uk.ac.vamsas.objects.core.DataSet[] getDataSet() {
328     uk.ac.vamsas.objects.core.DataSet[] array = new uk.ac.vamsas.objects.core.DataSet[0];
329     return (uk.ac.vamsas.objects.core.DataSet[]) this._dataSetList
330         .toArray(array);
331   }
332
333   /**
334    * Method getDataSetAsReference.Returns a reference to '_dataSetList'. No type
335    * checking is performed on any modifications to the Vector.
336    * 
337    * @return a reference to the Vector backing this class
338    */
339   public java.util.Vector getDataSetAsReference() {
340     return this._dataSetList;
341   }
342
343   /**
344    * Method getDataSetCount.
345    * 
346    * @return the size of this collection
347    */
348   public int getDataSetCount() {
349     return this._dataSetList.size();
350   }
351
352   /**
353    * Returns the value of field 'id'. The field 'id' has the following
354    * description: Primary Key for vamsas object referencing
355    * 
356    * @return the value of field 'Id'.
357    */
358   public java.lang.String getId() {
359     return this._id;
360   }
361
362   /**
363    * Returns the value of field 'modifiable'.
364    * 
365    * @return the value of field 'Modifiable'.
366    */
367   public java.lang.String getModifiable() {
368     return this._modifiable;
369   }
370
371   /**
372    * Method getTree.
373    * 
374    * @param index
375    * @throws java.lang.IndexOutOfBoundsException
376    *           if the index given is outside the bounds of the collection
377    * @return the value of the uk.ac.vamsas.objects.core.Tree at the given index
378    */
379   public uk.ac.vamsas.objects.core.Tree getTree(final int index)
380       throws java.lang.IndexOutOfBoundsException {
381     // check bounds for index
382     if (index < 0 || index >= this._treeList.size()) {
383       throw new IndexOutOfBoundsException("getTree: Index value '" + index
384           + "' not in range [0.." + (this._treeList.size() - 1) + "]");
385     }
386
387     return (uk.ac.vamsas.objects.core.Tree) _treeList.get(index);
388   }
389
390   /**
391    * Method getTree.Returns the contents of the collection in an Array.
392    * <p>
393    * Note: Just in case the collection contents are changing in another thread,
394    * we pass a 0-length Array of the correct type into the API call. This way we
395    * <i>know</i> that the Array returned is of exactly the correct length.
396    * 
397    * @return this collection as an Array
398    */
399   public uk.ac.vamsas.objects.core.Tree[] getTree() {
400     uk.ac.vamsas.objects.core.Tree[] array = new uk.ac.vamsas.objects.core.Tree[0];
401     return (uk.ac.vamsas.objects.core.Tree[]) this._treeList.toArray(array);
402   }
403
404   /**
405    * Method getTreeAsReference.Returns a reference to '_treeList'. No type
406    * checking is performed on any modifications to the Vector.
407    * 
408    * @return a reference to the Vector backing this class
409    */
410   public java.util.Vector getTreeAsReference() {
411     return this._treeList;
412   }
413
414   /**
415    * Method getTreeCount.
416    * 
417    * @return the size of this collection
418    */
419   public int getTreeCount() {
420     return this._treeList.size();
421   }
422
423   /**
424    * Overrides the java.lang.Object.hashCode method.
425    * <p>
426    * The following steps came from <b>Effective Java Programming Language
427    * Guide</b> by Joshua Bloch, Chapter 3
428    * 
429    * @return a hash code value for the object.
430    */
431   public int hashCode() {
432     int result = super.hashCode();
433
434     long tmp;
435     if (_id != null && !org.castor.util.CycleBreaker.startingToCycle(_id)) {
436       result = 37 * result + _id.hashCode();
437       org.castor.util.CycleBreaker.releaseCycleHandle(_id);
438     }
439     if (_modifiable != null
440         && !org.castor.util.CycleBreaker.startingToCycle(_modifiable)) {
441       result = 37 * result + _modifiable.hashCode();
442       org.castor.util.CycleBreaker.releaseCycleHandle(_modifiable);
443     }
444     if (_treeList != null
445         && !org.castor.util.CycleBreaker.startingToCycle(_treeList)) {
446       result = 37 * result + _treeList.hashCode();
447       org.castor.util.CycleBreaker.releaseCycleHandle(_treeList);
448     }
449     if (_dataSetList != null
450         && !org.castor.util.CycleBreaker.startingToCycle(_dataSetList)) {
451       result = 37 * result + _dataSetList.hashCode();
452       org.castor.util.CycleBreaker.releaseCycleHandle(_dataSetList);
453     }
454
455     return result;
456   }
457
458   /**
459    * Method isValid.
460    * 
461    * @return true if this object is valid according to the schema
462    */
463   public boolean isValid() {
464     try {
465       validate();
466     } catch (org.exolab.castor.xml.ValidationException vex) {
467       return false;
468     }
469     return true;
470   }
471
472   /**
473    * 
474    * 
475    * @param out
476    * @throws org.exolab.castor.xml.MarshalException
477    *           if object is null or if any SAXException is thrown during
478    *           marshaling
479    * @throws org.exolab.castor.xml.ValidationException
480    *           if this object is an invalid instance according to the schema
481    */
482   public void marshal(final java.io.Writer out)
483       throws org.exolab.castor.xml.MarshalException,
484       org.exolab.castor.xml.ValidationException {
485     Marshaller.marshal(this, out);
486   }
487
488   /**
489    * 
490    * 
491    * @param handler
492    * @throws java.io.IOException
493    *           if an IOException occurs during marshaling
494    * @throws org.exolab.castor.xml.ValidationException
495    *           if this object is an invalid instance according to the schema
496    * @throws org.exolab.castor.xml.MarshalException
497    *           if object is null or if any SAXException is thrown during
498    *           marshaling
499    */
500   public void marshal(final org.xml.sax.ContentHandler handler)
501       throws java.io.IOException, org.exolab.castor.xml.MarshalException,
502       org.exolab.castor.xml.ValidationException {
503     Marshaller.marshal(this, handler);
504   }
505
506   /**
507      */
508   public void removeAllDataSet() {
509     this._dataSetList.clear();
510   }
511
512   /**
513      */
514   public void removeAllTree() {
515     this._treeList.clear();
516   }
517
518   /**
519    * Method removeDataSet.
520    * 
521    * @param vDataSet
522    * @return true if the object was removed from the collection.
523    */
524   public boolean removeDataSet(final uk.ac.vamsas.objects.core.DataSet vDataSet) {
525     boolean removed = _dataSetList.remove(vDataSet);
526     return removed;
527   }
528
529   /**
530    * Method removeDataSetAt.
531    * 
532    * @param index
533    * @return the element removed from the collection
534    */
535   public uk.ac.vamsas.objects.core.DataSet removeDataSetAt(final int index) {
536     java.lang.Object obj = this._dataSetList.remove(index);
537     return (uk.ac.vamsas.objects.core.DataSet) obj;
538   }
539
540   /**
541    * Method removeTree.
542    * 
543    * @param vTree
544    * @return true if the object was removed from the collection.
545    */
546   public boolean removeTree(final uk.ac.vamsas.objects.core.Tree vTree) {
547     boolean removed = _treeList.remove(vTree);
548     return removed;
549   }
550
551   /**
552    * Method removeTreeAt.
553    * 
554    * @param index
555    * @return the element removed from the collection
556    */
557   public uk.ac.vamsas.objects.core.Tree removeTreeAt(final int index) {
558     java.lang.Object obj = this._treeList.remove(index);
559     return (uk.ac.vamsas.objects.core.Tree) obj;
560   }
561
562   /**
563    * 
564    * 
565    * @param index
566    * @param vDataSet
567    * @throws java.lang.IndexOutOfBoundsException
568    *           if the index given is outside the bounds of the collection
569    */
570   public void setDataSet(final int index,
571       final uk.ac.vamsas.objects.core.DataSet vDataSet)
572       throws java.lang.IndexOutOfBoundsException {
573     // check bounds for index
574     if (index < 0 || index >= this._dataSetList.size()) {
575       throw new IndexOutOfBoundsException("setDataSet: Index value '" + index
576           + "' not in range [0.." + (this._dataSetList.size() - 1) + "]");
577     }
578
579     this._dataSetList.set(index, vDataSet);
580   }
581
582   /**
583    * 
584    * 
585    * @param vDataSetArray
586    */
587   public void setDataSet(final uk.ac.vamsas.objects.core.DataSet[] vDataSetArray) {
588     // -- copy array
589     _dataSetList.clear();
590
591     for (int i = 0; i < vDataSetArray.length; i++) {
592       this._dataSetList.add(vDataSetArray[i]);
593     }
594   }
595
596   /**
597    * Sets the value of '_dataSetList' by copying the given Vector. All elements
598    * will be checked for type safety.
599    * 
600    * @param vDataSetList
601    *          the Vector to copy.
602    */
603   public void setDataSet(final java.util.Vector vDataSetList) {
604     // copy vector
605     this._dataSetList.clear();
606
607     this._dataSetList.addAll(vDataSetList);
608   }
609
610   /**
611    * Sets the value of '_dataSetList' by setting it to the given Vector. No type
612    * checking is performed.
613    * 
614    * @deprecated
615    * 
616    * @param dataSetVector
617    *          the Vector to set.
618    */
619   public void setDataSetAsReference(final java.util.Vector dataSetVector) {
620     this._dataSetList = dataSetVector;
621   }
622
623   /**
624    * Sets the value of field 'id'. The field 'id' has the following description:
625    * Primary Key for vamsas object referencing
626    * 
627    * @param id
628    *          the value of field 'id'.
629    */
630   public void setId(final java.lang.String id) {
631     this._id = id;
632   }
633
634   /**
635    * Sets the value of field 'modifiable'.
636    * 
637    * @param modifiable
638    *          the value of field 'modifiable'.
639    */
640   public void setModifiable(final java.lang.String modifiable) {
641     this._modifiable = modifiable;
642   }
643
644   /**
645    * 
646    * 
647    * @param index
648    * @param vTree
649    * @throws java.lang.IndexOutOfBoundsException
650    *           if the index given is outside the bounds of the collection
651    */
652   public void setTree(final int index,
653       final uk.ac.vamsas.objects.core.Tree vTree)
654       throws java.lang.IndexOutOfBoundsException {
655     // check bounds for index
656     if (index < 0 || index >= this._treeList.size()) {
657       throw new IndexOutOfBoundsException("setTree: Index value '" + index
658           + "' not in range [0.." + (this._treeList.size() - 1) + "]");
659     }
660
661     this._treeList.set(index, vTree);
662   }
663
664   /**
665    * 
666    * 
667    * @param vTreeArray
668    */
669   public void setTree(final uk.ac.vamsas.objects.core.Tree[] vTreeArray) {
670     // -- copy array
671     _treeList.clear();
672
673     for (int i = 0; i < vTreeArray.length; i++) {
674       this._treeList.add(vTreeArray[i]);
675     }
676   }
677
678   /**
679    * Sets the value of '_treeList' by copying the given Vector. All elements
680    * will be checked for type safety.
681    * 
682    * @param vTreeList
683    *          the Vector to copy.
684    */
685   public void setTree(final java.util.Vector vTreeList) {
686     // copy vector
687     this._treeList.clear();
688
689     this._treeList.addAll(vTreeList);
690   }
691
692   /**
693    * Sets the value of '_treeList' by setting it to the given Vector. No type
694    * checking is performed.
695    * 
696    * @deprecated
697    * 
698    * @param treeVector
699    *          the Vector to set.
700    */
701   public void setTreeAsReference(final java.util.Vector treeVector) {
702     this._treeList = treeVector;
703   }
704
705   /**
706    * Method unmarshal.
707    * 
708    * @param reader
709    * @throws org.exolab.castor.xml.MarshalException
710    *           if object is null or if any SAXException is thrown during
711    *           marshaling
712    * @throws org.exolab.castor.xml.ValidationException
713    *           if this object is an invalid instance according to the schema
714    * @return the unmarshaled uk.ac.vamsas.objects.core.VAMSAS
715    */
716   public static uk.ac.vamsas.objects.core.VAMSAS unmarshal(
717       final java.io.Reader reader)
718       throws org.exolab.castor.xml.MarshalException,
719       org.exolab.castor.xml.ValidationException {
720     return (uk.ac.vamsas.objects.core.VAMSAS) Unmarshaller.unmarshal(
721         uk.ac.vamsas.objects.core.VAMSAS.class, reader);
722   }
723
724   /**
725    * 
726    * 
727    * @throws org.exolab.castor.xml.ValidationException
728    *           if this object is an invalid instance according to the schema
729    */
730   public void validate() throws org.exolab.castor.xml.ValidationException {
731     org.exolab.castor.xml.Validator validator = new org.exolab.castor.xml.Validator();
732     validator.validate(this);
733   }
734
735 }