applied LGPLv3 and source code formatting.
[vamsas.git] / src / uk / ac / vamsas / objects / core / AlignmentAnnotation.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  * This is annotation over the coordinate frame defined by all the columns in\r
33  * the alignment.\r
34  * \r
35  * \r
36  * @version $Revision$ $Date$\r
37  */\r
38 public class AlignmentAnnotation extends\r
39     uk.ac.vamsas.objects.core.RangeAnnotation implements java.io.Serializable {\r
40 \r
41   // --------------------------/\r
42   // - Class/Member Variables -/\r
43   // --------------------------/\r
44 \r
45   /**\r
46    * TODO: decide if this flag is redundant - when true it would suggest that\r
47    * the annotationElement values together form a graph\r
48    */\r
49   private boolean _graph;\r
50 \r
51   /**\r
52    * keeps track of state for field: _graph\r
53    */\r
54   private boolean _has_graph;\r
55 \r
56   /**\r
57    * annotation is associated with a range on a particular group of alignment\r
58    * sequences\r
59    */\r
60   private java.util.Vector _seqrefs;\r
61 \r
62   /**\r
63    * Field _provenance.\r
64    */\r
65   private uk.ac.vamsas.objects.core.Provenance _provenance;\r
66 \r
67   // ----------------/\r
68   // - Constructors -/\r
69   // ----------------/\r
70 \r
71   public AlignmentAnnotation() {\r
72     super();\r
73     this._seqrefs = new java.util.Vector();\r
74   }\r
75 \r
76   // -----------/\r
77   // - Methods -/\r
78   // -----------/\r
79 \r
80   /**\r
81    * \r
82    * \r
83    * @param vSeqrefs\r
84    * @throws java.lang.IndexOutOfBoundsException\r
85    *           if the index given is outside the bounds of the collection\r
86    */\r
87   public void addSeqrefs(final java.lang.Object vSeqrefs)\r
88       throws java.lang.IndexOutOfBoundsException {\r
89     this._seqrefs.addElement(vSeqrefs);\r
90   }\r
91 \r
92   /**\r
93    * \r
94    * \r
95    * @param index\r
96    * @param vSeqrefs\r
97    * @throws java.lang.IndexOutOfBoundsException\r
98    *           if the index given is outside the bounds of the collection\r
99    */\r
100   public void addSeqrefs(final int index, final java.lang.Object vSeqrefs)\r
101       throws java.lang.IndexOutOfBoundsException {\r
102     this._seqrefs.add(index, vSeqrefs);\r
103   }\r
104 \r
105   /**\r
106      */\r
107   public void deleteGraph() {\r
108     this._has_graph = false;\r
109   }\r
110 \r
111   /**\r
112    * Method enumerateSeqrefs.\r
113    * \r
114    * @return an Enumeration over all java.lang.Object elements\r
115    */\r
116   public java.util.Enumeration enumerateSeqrefs() {\r
117     return this._seqrefs.elements();\r
118   }\r
119 \r
120   /**\r
121    * Overrides the java.lang.Object.equals method.\r
122    * \r
123    * @param obj\r
124    * @return true if the objects are equal.\r
125    */\r
126   public boolean equals(final java.lang.Object obj) {\r
127     if (this == obj)\r
128       return true;\r
129 \r
130     if (super.equals(obj) == false)\r
131       return false;\r
132 \r
133     if (obj instanceof AlignmentAnnotation) {\r
134 \r
135       AlignmentAnnotation temp = (AlignmentAnnotation) obj;\r
136       boolean thcycle;\r
137       boolean tmcycle;\r
138       if (this._graph != temp._graph)\r
139         return false;\r
140       if (this._has_graph != temp._has_graph)\r
141         return false;\r
142       if (this._seqrefs != null) {\r
143         if (temp._seqrefs == null)\r
144           return false;\r
145         if (this._seqrefs != temp._seqrefs) {\r
146           thcycle = org.castor.util.CycleBreaker.startingToCycle(this._seqrefs);\r
147           tmcycle = org.castor.util.CycleBreaker.startingToCycle(temp._seqrefs);\r
148           if (thcycle != tmcycle) {\r
149             if (!thcycle) {\r
150               org.castor.util.CycleBreaker.releaseCycleHandle(this._seqrefs);\r
151             }\r
152             ;\r
153             if (!tmcycle) {\r
154               org.castor.util.CycleBreaker.releaseCycleHandle(temp._seqrefs);\r
155             }\r
156             ;\r
157             return false;\r
158           }\r
159           if (!thcycle) {\r
160             if (!this._seqrefs.equals(temp._seqrefs)) {\r
161               org.castor.util.CycleBreaker.releaseCycleHandle(this._seqrefs);\r
162               org.castor.util.CycleBreaker.releaseCycleHandle(temp._seqrefs);\r
163               return false;\r
164             }\r
165             org.castor.util.CycleBreaker.releaseCycleHandle(this._seqrefs);\r
166             org.castor.util.CycleBreaker.releaseCycleHandle(temp._seqrefs);\r
167           }\r
168         }\r
169       } else if (temp._seqrefs != null)\r
170         return false;\r
171       if (this._provenance != null) {\r
172         if (temp._provenance == null)\r
173           return false;\r
174         if (this._provenance != temp._provenance) {\r
175           thcycle = org.castor.util.CycleBreaker\r
176               .startingToCycle(this._provenance);\r
177           tmcycle = org.castor.util.CycleBreaker\r
178               .startingToCycle(temp._provenance);\r
179           if (thcycle != tmcycle) {\r
180             if (!thcycle) {\r
181               org.castor.util.CycleBreaker.releaseCycleHandle(this._provenance);\r
182             }\r
183             ;\r
184             if (!tmcycle) {\r
185               org.castor.util.CycleBreaker.releaseCycleHandle(temp._provenance);\r
186             }\r
187             ;\r
188             return false;\r
189           }\r
190           if (!thcycle) {\r
191             if (!this._provenance.equals(temp._provenance)) {\r
192               org.castor.util.CycleBreaker.releaseCycleHandle(this._provenance);\r
193               org.castor.util.CycleBreaker.releaseCycleHandle(temp._provenance);\r
194               return false;\r
195             }\r
196             org.castor.util.CycleBreaker.releaseCycleHandle(this._provenance);\r
197             org.castor.util.CycleBreaker.releaseCycleHandle(temp._provenance);\r
198           }\r
199         }\r
200       } else if (temp._provenance != null)\r
201         return false;\r
202       return true;\r
203     }\r
204     return false;\r
205   }\r
206 \r
207   /**\r
208    * Returns the value of field 'graph'. The field 'graph' has the following\r
209    * description: TODO: decide if this flag is redundant - when true it would\r
210    * suggest that the annotationElement values together form a graph\r
211    * \r
212    * @return the value of field 'Graph'.\r
213    */\r
214   public boolean getGraph() {\r
215     return this._graph;\r
216   }\r
217 \r
218   /**\r
219    * Returns the value of field 'provenance'.\r
220    * \r
221    * @return the value of field 'Provenance'.\r
222    */\r
223   public uk.ac.vamsas.objects.core.Provenance getProvenance() {\r
224     return this._provenance;\r
225   }\r
226 \r
227   /**\r
228    * Method getSeqrefs.\r
229    * \r
230    * @param index\r
231    * @throws java.lang.IndexOutOfBoundsException\r
232    *           if the index given is outside the bounds of the collection\r
233    * @return the value of the java.lang.Object at the given index\r
234    */\r
235   public java.lang.Object getSeqrefs(final int index)\r
236       throws java.lang.IndexOutOfBoundsException {\r
237     // check bounds for index\r
238     if (index < 0 || index >= this._seqrefs.size()) {\r
239       throw new IndexOutOfBoundsException("getSeqrefs: Index value '" + index\r
240           + "' not in range [0.." + (this._seqrefs.size() - 1) + "]");\r
241     }\r
242 \r
243     return _seqrefs.get(index);\r
244   }\r
245 \r
246   /**\r
247    * Method getSeqrefs.Returns the contents of the collection in an Array.\r
248    * <p>\r
249    * Note: Just in case the collection contents are changing in another thread,\r
250    * we pass a 0-length Array of the correct type into the API call. This way we\r
251    * <i>know</i> that the Array returned is of exactly the correct length.\r
252    * \r
253    * @return this collection as an Array\r
254    */\r
255   public java.lang.Object[] getSeqrefs() {\r
256     java.lang.Object[] array = new java.lang.Object[0];\r
257     return (java.lang.Object[]) this._seqrefs.toArray(array);\r
258   }\r
259 \r
260   /**\r
261    * Method getSeqrefsAsReference.Returns a reference to '_seqrefs'. No type\r
262    * checking is performed on any modifications to the Vector.\r
263    * \r
264    * @return a reference to the Vector backing this class\r
265    */\r
266   public java.util.Vector getSeqrefsAsReference() {\r
267     return this._seqrefs;\r
268   }\r
269 \r
270   /**\r
271    * Method getSeqrefsCount.\r
272    * \r
273    * @return the size of this collection\r
274    */\r
275   public int getSeqrefsCount() {\r
276     return this._seqrefs.size();\r
277   }\r
278 \r
279   /**\r
280    * Method hasGraph.\r
281    * \r
282    * @return true if at least one Graph has been added\r
283    */\r
284   public boolean hasGraph() {\r
285     return this._has_graph;\r
286   }\r
287 \r
288   /**\r
289    * Overrides the java.lang.Object.hashCode method.\r
290    * <p>\r
291    * The following steps came from <b>Effective Java Programming Language\r
292    * Guide</b> by Joshua Bloch, Chapter 3\r
293    * \r
294    * @return a hash code value for the object.\r
295    */\r
296   public int hashCode() {\r
297     int result = super.hashCode();\r
298 \r
299     long tmp;\r
300     result = 37 * result + (_graph ? 0 : 1);\r
301     if (_seqrefs != null\r
302         && !org.castor.util.CycleBreaker.startingToCycle(_seqrefs)) {\r
303       result = 37 * result + _seqrefs.hashCode();\r
304       org.castor.util.CycleBreaker.releaseCycleHandle(_seqrefs);\r
305     }\r
306     if (_provenance != null\r
307         && !org.castor.util.CycleBreaker.startingToCycle(_provenance)) {\r
308       result = 37 * result + _provenance.hashCode();\r
309       org.castor.util.CycleBreaker.releaseCycleHandle(_provenance);\r
310     }\r
311 \r
312     return result;\r
313   }\r
314 \r
315   /**\r
316    * Returns the value of field 'graph'. The field 'graph' has the following\r
317    * description: TODO: decide if this flag is redundant - when true it would\r
318    * suggest that the annotationElement values together form a graph\r
319    * \r
320    * @return the value of field 'Graph'.\r
321    */\r
322   public boolean isGraph() {\r
323     return this._graph;\r
324   }\r
325 \r
326   /**\r
327    * Method isValid.\r
328    * \r
329    * @return true if this object is valid according to the schema\r
330    */\r
331   public boolean isValid() {\r
332     try {\r
333       validate();\r
334     } catch (org.exolab.castor.xml.ValidationException vex) {\r
335       return false;\r
336     }\r
337     return true;\r
338   }\r
339 \r
340   /**\r
341    * \r
342    * \r
343    * @param out\r
344    * @throws org.exolab.castor.xml.MarshalException\r
345    *           if object is null or if any SAXException is thrown during\r
346    *           marshaling\r
347    * @throws org.exolab.castor.xml.ValidationException\r
348    *           if this object is an invalid instance according to the schema\r
349    */\r
350   public void marshal(final java.io.Writer out)\r
351       throws org.exolab.castor.xml.MarshalException,\r
352       org.exolab.castor.xml.ValidationException {\r
353     Marshaller.marshal(this, out);\r
354   }\r
355 \r
356   /**\r
357    * \r
358    * \r
359    * @param handler\r
360    * @throws java.io.IOException\r
361    *           if an IOException occurs during marshaling\r
362    * @throws org.exolab.castor.xml.ValidationException\r
363    *           if this object is an invalid instance according to the schema\r
364    * @throws org.exolab.castor.xml.MarshalException\r
365    *           if object is null or if any SAXException is thrown during\r
366    *           marshaling\r
367    */\r
368   public void marshal(final org.xml.sax.ContentHandler handler)\r
369       throws java.io.IOException, org.exolab.castor.xml.MarshalException,\r
370       org.exolab.castor.xml.ValidationException {\r
371     Marshaller.marshal(this, handler);\r
372   }\r
373 \r
374   /**\r
375      */\r
376   public void removeAllSeqrefs() {\r
377     this._seqrefs.clear();\r
378   }\r
379 \r
380   /**\r
381    * Method removeSeqrefs.\r
382    * \r
383    * @param vSeqrefs\r
384    * @return true if the object was removed from the collection.\r
385    */\r
386   public boolean removeSeqrefs(final java.lang.Object vSeqrefs) {\r
387     boolean removed = _seqrefs.remove(vSeqrefs);\r
388     return removed;\r
389   }\r
390 \r
391   /**\r
392    * Method removeSeqrefsAt.\r
393    * \r
394    * @param index\r
395    * @return the element removed from the collection\r
396    */\r
397   public java.lang.Object removeSeqrefsAt(final int index) {\r
398     java.lang.Object obj = this._seqrefs.remove(index);\r
399     return obj;\r
400   }\r
401 \r
402   /**\r
403    * Sets the value of field 'graph'. The field 'graph' has the following\r
404    * description: TODO: decide if this flag is redundant - when true it would\r
405    * suggest that the annotationElement values together form a graph\r
406    * \r
407    * @param graph\r
408    *          the value of field 'graph'.\r
409    */\r
410   public void setGraph(final boolean graph) {\r
411     this._graph = graph;\r
412     this._has_graph = true;\r
413   }\r
414 \r
415   /**\r
416    * Sets the value of field 'provenance'.\r
417    * \r
418    * @param provenance\r
419    *          the value of field 'provenance'.\r
420    */\r
421   public void setProvenance(\r
422       final uk.ac.vamsas.objects.core.Provenance provenance) {\r
423     this._provenance = provenance;\r
424   }\r
425 \r
426   /**\r
427    * \r
428    * \r
429    * @param index\r
430    * @param vSeqrefs\r
431    * @throws java.lang.IndexOutOfBoundsException\r
432    *           if the index given is outside the bounds of the collection\r
433    */\r
434   public void setSeqrefs(final int index, final java.lang.Object vSeqrefs)\r
435       throws java.lang.IndexOutOfBoundsException {\r
436     // check bounds for index\r
437     if (index < 0 || index >= this._seqrefs.size()) {\r
438       throw new IndexOutOfBoundsException("setSeqrefs: Index value '" + index\r
439           + "' not in range [0.." + (this._seqrefs.size() - 1) + "]");\r
440     }\r
441 \r
442     this._seqrefs.set(index, vSeqrefs);\r
443   }\r
444 \r
445   /**\r
446    * \r
447    * \r
448    * @param vSeqrefsArray\r
449    */\r
450   public void setSeqrefs(final java.lang.Object[] vSeqrefsArray) {\r
451     // -- copy array\r
452     _seqrefs.clear();\r
453 \r
454     for (int i = 0; i < vSeqrefsArray.length; i++) {\r
455       this._seqrefs.add(vSeqrefsArray[i]);\r
456     }\r
457   }\r
458 \r
459   /**\r
460    * Sets the value of '_seqrefs' by copying the given Vector. All elements will\r
461    * be checked for type safety.\r
462    * \r
463    * @param vSeqrefsList\r
464    *          the Vector to copy.\r
465    */\r
466   public void setSeqrefs(final java.util.Vector vSeqrefsList) {\r
467     // copy vector\r
468     this._seqrefs.clear();\r
469 \r
470     this._seqrefs.addAll(vSeqrefsList);\r
471   }\r
472 \r
473   /**\r
474    * Sets the value of '_seqrefs' by setting it to the given Vector. No type\r
475    * checking is performed.\r
476    * \r
477    * @deprecated\r
478    * \r
479    * @param seqrefsVector\r
480    *          the Vector to set.\r
481    */\r
482   public void setSeqrefsAsReference(final java.util.Vector seqrefsVector) {\r
483     this._seqrefs = seqrefsVector;\r
484   }\r
485 \r
486   /**\r
487    * Method unmarshal.\r
488    * \r
489    * @param reader\r
490    * @throws org.exolab.castor.xml.MarshalException\r
491    *           if object is null or if any SAXException is thrown during\r
492    *           marshaling\r
493    * @throws org.exolab.castor.xml.ValidationException\r
494    *           if this object is an invalid instance according to the schema\r
495    * @return the unmarshaled uk.ac.vamsas.objects.core.RangeType\r
496    */\r
497   public static uk.ac.vamsas.objects.core.RangeType unmarshal(\r
498       final java.io.Reader reader)\r
499       throws org.exolab.castor.xml.MarshalException,\r
500       org.exolab.castor.xml.ValidationException {\r
501     return (uk.ac.vamsas.objects.core.RangeType) Unmarshaller.unmarshal(\r
502         uk.ac.vamsas.objects.core.AlignmentAnnotation.class, reader);\r
503   }\r
504 \r
505   /**\r
506    * \r
507    * \r
508    * @throws org.exolab.castor.xml.ValidationException\r
509    *           if this object is an invalid instance according to the schema\r
510    */\r
511   public void validate() throws org.exolab.castor.xml.ValidationException {\r
512     org.exolab.castor.xml.Validator validator = new org.exolab.castor.xml.Validator();\r
513     validator.validate(this);\r
514   }\r
515 \r
516 }\r