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