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