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