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