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