applied LGPLv3 and source code formatting.
[vamsas.git] / src / uk / ac / vamsas / objects / core / RangeType.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  * Specify an ordered set of positions and/or regions on the principle dimension\r
26  * of some associated vamsas object Keeping to jaxb-1.0 specification for the\r
27  * moment - this choice should become a substitution group when we use jaxb-2.0\r
28  * capable bindings\r
29  * \r
30  * \r
31  * @version $Revision$ $Date$\r
32  */\r
33 public abstract class RangeType extends uk.ac.vamsas.client.Vobject implements\r
34     java.io.Serializable {\r
35 \r
36   // --------------------------/\r
37   // - Class/Member Variables -/\r
38   // --------------------------/\r
39 \r
40   /**\r
41    * Internal choice value storage\r
42    */\r
43   private java.lang.Object _choiceValue;\r
44 \r
45   /**\r
46    * a position within the associated object's coordinate system\r
47    * \r
48    */\r
49   private java.util.Vector _posList;\r
50 \r
51   /**\r
52    * a region from start to end, with flag for inclusivity of terminii\r
53    */\r
54   private java.util.Vector _segList;\r
55 \r
56   // ----------------/\r
57   // - Constructors -/\r
58   // ----------------/\r
59 \r
60   public RangeType() {\r
61     super();\r
62     this._posList = new java.util.Vector();\r
63     this._segList = new java.util.Vector();\r
64   }\r
65 \r
66   // -----------/\r
67   // - Methods -/\r
68   // -----------/\r
69 \r
70   /**\r
71    * \r
72    * \r
73    * @param vPos\r
74    * @throws java.lang.IndexOutOfBoundsException\r
75    *           if the index given is outside the bounds of the collection\r
76    */\r
77   public void addPos(final uk.ac.vamsas.objects.core.Pos vPos)\r
78       throws java.lang.IndexOutOfBoundsException {\r
79     this._posList.addElement(vPos);\r
80   }\r
81 \r
82   /**\r
83    * \r
84    * \r
85    * @param index\r
86    * @param vPos\r
87    * @throws java.lang.IndexOutOfBoundsException\r
88    *           if the index given is outside the bounds of the collection\r
89    */\r
90   public void addPos(final int index, final uk.ac.vamsas.objects.core.Pos vPos)\r
91       throws java.lang.IndexOutOfBoundsException {\r
92     this._posList.add(index, vPos);\r
93   }\r
94 \r
95   /**\r
96    * \r
97    * \r
98    * @param vSeg\r
99    * @throws java.lang.IndexOutOfBoundsException\r
100    *           if the index given is outside the bounds of the collection\r
101    */\r
102   public void addSeg(final uk.ac.vamsas.objects.core.Seg vSeg)\r
103       throws java.lang.IndexOutOfBoundsException {\r
104     this._segList.addElement(vSeg);\r
105   }\r
106 \r
107   /**\r
108    * \r
109    * \r
110    * @param index\r
111    * @param vSeg\r
112    * @throws java.lang.IndexOutOfBoundsException\r
113    *           if the index given is outside the bounds of the collection\r
114    */\r
115   public void addSeg(final int index, final uk.ac.vamsas.objects.core.Seg vSeg)\r
116       throws java.lang.IndexOutOfBoundsException {\r
117     this._segList.add(index, vSeg);\r
118   }\r
119 \r
120   /**\r
121    * Method enumeratePos.\r
122    * \r
123    * @return an Enumeration over all uk.ac.vamsas.objects.core.Pos elements\r
124    */\r
125   public java.util.Enumeration enumeratePos() {\r
126     return this._posList.elements();\r
127   }\r
128 \r
129   /**\r
130    * Method enumerateSeg.\r
131    * \r
132    * @return an Enumeration over all uk.ac.vamsas.objects.core.Seg elements\r
133    */\r
134   public java.util.Enumeration enumerateSeg() {\r
135     return this._segList.elements();\r
136   }\r
137 \r
138   /**\r
139    * Overrides the java.lang.Object.equals method.\r
140    * \r
141    * @param obj\r
142    * @return true if the objects are equal.\r
143    */\r
144   public boolean equals(final java.lang.Object obj) {\r
145     if (this == obj)\r
146       return true;\r
147 \r
148     if (super.equals(obj) == false)\r
149       return false;\r
150 \r
151     if (obj instanceof RangeType) {\r
152 \r
153       RangeType temp = (RangeType) obj;\r
154       boolean thcycle;\r
155       boolean tmcycle;\r
156       if (this._choiceValue != null) {\r
157         if (temp._choiceValue == null)\r
158           return false;\r
159         if (this._choiceValue != temp._choiceValue) {\r
160           thcycle = org.castor.util.CycleBreaker\r
161               .startingToCycle(this._choiceValue);\r
162           tmcycle = org.castor.util.CycleBreaker\r
163               .startingToCycle(temp._choiceValue);\r
164           if (thcycle != tmcycle) {\r
165             if (!thcycle) {\r
166               org.castor.util.CycleBreaker\r
167                   .releaseCycleHandle(this._choiceValue);\r
168             }\r
169             ;\r
170             if (!tmcycle) {\r
171               org.castor.util.CycleBreaker\r
172                   .releaseCycleHandle(temp._choiceValue);\r
173             }\r
174             ;\r
175             return false;\r
176           }\r
177           if (!thcycle) {\r
178             if (!this._choiceValue.equals(temp._choiceValue)) {\r
179               org.castor.util.CycleBreaker\r
180                   .releaseCycleHandle(this._choiceValue);\r
181               org.castor.util.CycleBreaker\r
182                   .releaseCycleHandle(temp._choiceValue);\r
183               return false;\r
184             }\r
185             org.castor.util.CycleBreaker.releaseCycleHandle(this._choiceValue);\r
186             org.castor.util.CycleBreaker.releaseCycleHandle(temp._choiceValue);\r
187           }\r
188         }\r
189       } else if (temp._choiceValue != null)\r
190         return false;\r
191       if (this._posList != null) {\r
192         if (temp._posList == null)\r
193           return false;\r
194         if (this._posList != temp._posList) {\r
195           thcycle = org.castor.util.CycleBreaker.startingToCycle(this._posList);\r
196           tmcycle = org.castor.util.CycleBreaker.startingToCycle(temp._posList);\r
197           if (thcycle != tmcycle) {\r
198             if (!thcycle) {\r
199               org.castor.util.CycleBreaker.releaseCycleHandle(this._posList);\r
200             }\r
201             ;\r
202             if (!tmcycle) {\r
203               org.castor.util.CycleBreaker.releaseCycleHandle(temp._posList);\r
204             }\r
205             ;\r
206             return false;\r
207           }\r
208           if (!thcycle) {\r
209             if (!this._posList.equals(temp._posList)) {\r
210               org.castor.util.CycleBreaker.releaseCycleHandle(this._posList);\r
211               org.castor.util.CycleBreaker.releaseCycleHandle(temp._posList);\r
212               return false;\r
213             }\r
214             org.castor.util.CycleBreaker.releaseCycleHandle(this._posList);\r
215             org.castor.util.CycleBreaker.releaseCycleHandle(temp._posList);\r
216           }\r
217         }\r
218       } else if (temp._posList != null)\r
219         return false;\r
220       if (this._segList != null) {\r
221         if (temp._segList == null)\r
222           return false;\r
223         if (this._segList != temp._segList) {\r
224           thcycle = org.castor.util.CycleBreaker.startingToCycle(this._segList);\r
225           tmcycle = org.castor.util.CycleBreaker.startingToCycle(temp._segList);\r
226           if (thcycle != tmcycle) {\r
227             if (!thcycle) {\r
228               org.castor.util.CycleBreaker.releaseCycleHandle(this._segList);\r
229             }\r
230             ;\r
231             if (!tmcycle) {\r
232               org.castor.util.CycleBreaker.releaseCycleHandle(temp._segList);\r
233             }\r
234             ;\r
235             return false;\r
236           }\r
237           if (!thcycle) {\r
238             if (!this._segList.equals(temp._segList)) {\r
239               org.castor.util.CycleBreaker.releaseCycleHandle(this._segList);\r
240               org.castor.util.CycleBreaker.releaseCycleHandle(temp._segList);\r
241               return false;\r
242             }\r
243             org.castor.util.CycleBreaker.releaseCycleHandle(this._segList);\r
244             org.castor.util.CycleBreaker.releaseCycleHandle(temp._segList);\r
245           }\r
246         }\r
247       } else if (temp._segList != null)\r
248         return false;\r
249       return true;\r
250     }\r
251     return false;\r
252   }\r
253 \r
254   /**\r
255    * Returns the value of field 'choiceValue'. The field 'choiceValue' has the\r
256    * following description: Internal choice value storage\r
257    * \r
258    * @return the value of field 'ChoiceValue'.\r
259    */\r
260   public java.lang.Object getChoiceValue() {\r
261     return this._choiceValue;\r
262   }\r
263 \r
264   /**\r
265    * Method getPos.\r
266    * \r
267    * @param index\r
268    * @throws java.lang.IndexOutOfBoundsException\r
269    *           if the index given is outside the bounds of the collection\r
270    * @return the value of the uk.ac.vamsas.objects.core.Pos at the given index\r
271    */\r
272   public uk.ac.vamsas.objects.core.Pos getPos(final int index)\r
273       throws java.lang.IndexOutOfBoundsException {\r
274     // check bounds for index\r
275     if (index < 0 || index >= this._posList.size()) {\r
276       throw new IndexOutOfBoundsException("getPos: Index value '" + index\r
277           + "' not in range [0.." + (this._posList.size() - 1) + "]");\r
278     }\r
279 \r
280     return (uk.ac.vamsas.objects.core.Pos) _posList.get(index);\r
281   }\r
282 \r
283   /**\r
284    * Method getPos.Returns the contents of the collection in an Array.\r
285    * <p>\r
286    * Note: Just in case the collection contents are changing in another thread,\r
287    * we pass a 0-length Array of the correct type into the API call. This way we\r
288    * <i>know</i> that the Array returned is of exactly the correct length.\r
289    * \r
290    * @return this collection as an Array\r
291    */\r
292   public uk.ac.vamsas.objects.core.Pos[] getPos() {\r
293     uk.ac.vamsas.objects.core.Pos[] array = new uk.ac.vamsas.objects.core.Pos[0];\r
294     return (uk.ac.vamsas.objects.core.Pos[]) this._posList.toArray(array);\r
295   }\r
296 \r
297   /**\r
298    * Method getPosAsReference.Returns a reference to '_posList'. No type\r
299    * checking is performed on any modifications to the Vector.\r
300    * \r
301    * @return a reference to the Vector backing this class\r
302    */\r
303   public java.util.Vector getPosAsReference() {\r
304     return this._posList;\r
305   }\r
306 \r
307   /**\r
308    * Method getPosCount.\r
309    * \r
310    * @return the size of this collection\r
311    */\r
312   public int getPosCount() {\r
313     return this._posList.size();\r
314   }\r
315 \r
316   /**\r
317    * Method getSeg.\r
318    * \r
319    * @param index\r
320    * @throws java.lang.IndexOutOfBoundsException\r
321    *           if the index given is outside the bounds of the collection\r
322    * @return the value of the uk.ac.vamsas.objects.core.Seg at the given index\r
323    */\r
324   public uk.ac.vamsas.objects.core.Seg getSeg(final int index)\r
325       throws java.lang.IndexOutOfBoundsException {\r
326     // check bounds for index\r
327     if (index < 0 || index >= this._segList.size()) {\r
328       throw new IndexOutOfBoundsException("getSeg: Index value '" + index\r
329           + "' not in range [0.." + (this._segList.size() - 1) + "]");\r
330     }\r
331 \r
332     return (uk.ac.vamsas.objects.core.Seg) _segList.get(index);\r
333   }\r
334 \r
335   /**\r
336    * Method getSeg.Returns the contents of the collection in an Array.\r
337    * <p>\r
338    * Note: Just in case the collection contents are changing in another thread,\r
339    * we pass a 0-length Array of the correct type into the API call. This way we\r
340    * <i>know</i> that the Array returned is of exactly the correct length.\r
341    * \r
342    * @return this collection as an Array\r
343    */\r
344   public uk.ac.vamsas.objects.core.Seg[] getSeg() {\r
345     uk.ac.vamsas.objects.core.Seg[] array = new uk.ac.vamsas.objects.core.Seg[0];\r
346     return (uk.ac.vamsas.objects.core.Seg[]) this._segList.toArray(array);\r
347   }\r
348 \r
349   /**\r
350    * Method getSegAsReference.Returns a reference to '_segList'. No type\r
351    * checking is performed on any modifications to the Vector.\r
352    * \r
353    * @return a reference to the Vector backing this class\r
354    */\r
355   public java.util.Vector getSegAsReference() {\r
356     return this._segList;\r
357   }\r
358 \r
359   /**\r
360    * Method getSegCount.\r
361    * \r
362    * @return the size of this collection\r
363    */\r
364   public int getSegCount() {\r
365     return this._segList.size();\r
366   }\r
367 \r
368   /**\r
369    * Overrides the java.lang.Object.hashCode method.\r
370    * <p>\r
371    * The following steps came from <b>Effective Java Programming Language\r
372    * Guide</b> by Joshua Bloch, Chapter 3\r
373    * \r
374    * @return a hash code value for the object.\r
375    */\r
376   public int hashCode() {\r
377     int result = super.hashCode();\r
378 \r
379     long tmp;\r
380     if (_choiceValue != null\r
381         && !org.castor.util.CycleBreaker.startingToCycle(_choiceValue)) {\r
382       result = 37 * result + _choiceValue.hashCode();\r
383       org.castor.util.CycleBreaker.releaseCycleHandle(_choiceValue);\r
384     }\r
385     if (_posList != null\r
386         && !org.castor.util.CycleBreaker.startingToCycle(_posList)) {\r
387       result = 37 * result + _posList.hashCode();\r
388       org.castor.util.CycleBreaker.releaseCycleHandle(_posList);\r
389     }\r
390     if (_segList != null\r
391         && !org.castor.util.CycleBreaker.startingToCycle(_segList)) {\r
392       result = 37 * result + _segList.hashCode();\r
393       org.castor.util.CycleBreaker.releaseCycleHandle(_segList);\r
394     }\r
395 \r
396     return result;\r
397   }\r
398 \r
399   /**\r
400    * Method isValid.\r
401    * \r
402    * @return true if this object is valid according to the schema\r
403    */\r
404   public boolean isValid() {\r
405     try {\r
406       validate();\r
407     } catch (org.exolab.castor.xml.ValidationException vex) {\r
408       return false;\r
409     }\r
410     return true;\r
411   }\r
412 \r
413   /**\r
414      */\r
415   public void removeAllPos() {\r
416     this._posList.clear();\r
417   }\r
418 \r
419   /**\r
420      */\r
421   public void removeAllSeg() {\r
422     this._segList.clear();\r
423   }\r
424 \r
425   /**\r
426    * Method removePos.\r
427    * \r
428    * @param vPos\r
429    * @return true if the object was removed from the collection.\r
430    */\r
431   public boolean removePos(final uk.ac.vamsas.objects.core.Pos vPos) {\r
432     boolean removed = _posList.remove(vPos);\r
433     return removed;\r
434   }\r
435 \r
436   /**\r
437    * Method removePosAt.\r
438    * \r
439    * @param index\r
440    * @return the element removed from the collection\r
441    */\r
442   public uk.ac.vamsas.objects.core.Pos removePosAt(final int index) {\r
443     java.lang.Object obj = this._posList.remove(index);\r
444     return (uk.ac.vamsas.objects.core.Pos) obj;\r
445   }\r
446 \r
447   /**\r
448    * Method removeSeg.\r
449    * \r
450    * @param vSeg\r
451    * @return true if the object was removed from the collection.\r
452    */\r
453   public boolean removeSeg(final uk.ac.vamsas.objects.core.Seg vSeg) {\r
454     boolean removed = _segList.remove(vSeg);\r
455     return removed;\r
456   }\r
457 \r
458   /**\r
459    * Method removeSegAt.\r
460    * \r
461    * @param index\r
462    * @return the element removed from the collection\r
463    */\r
464   public uk.ac.vamsas.objects.core.Seg removeSegAt(final int index) {\r
465     java.lang.Object obj = this._segList.remove(index);\r
466     return (uk.ac.vamsas.objects.core.Seg) obj;\r
467   }\r
468 \r
469   /**\r
470    * \r
471    * \r
472    * @param index\r
473    * @param vPos\r
474    * @throws java.lang.IndexOutOfBoundsException\r
475    *           if the index given is outside the bounds of the collection\r
476    */\r
477   public void setPos(final int index, final uk.ac.vamsas.objects.core.Pos vPos)\r
478       throws java.lang.IndexOutOfBoundsException {\r
479     // check bounds for index\r
480     if (index < 0 || index >= this._posList.size()) {\r
481       throw new IndexOutOfBoundsException("setPos: Index value '" + index\r
482           + "' not in range [0.." + (this._posList.size() - 1) + "]");\r
483     }\r
484 \r
485     this._posList.set(index, vPos);\r
486   }\r
487 \r
488   /**\r
489    * \r
490    * \r
491    * @param vPosArray\r
492    */\r
493   public void setPos(final uk.ac.vamsas.objects.core.Pos[] vPosArray) {\r
494     // -- copy array\r
495     _posList.clear();\r
496 \r
497     for (int i = 0; i < vPosArray.length; i++) {\r
498       this._posList.add(vPosArray[i]);\r
499     }\r
500   }\r
501 \r
502   /**\r
503    * Sets the value of '_posList' by copying the given Vector. All elements will\r
504    * be checked for type safety.\r
505    * \r
506    * @param vPosList\r
507    *          the Vector to copy.\r
508    */\r
509   public void setPos(final java.util.Vector vPosList) {\r
510     // copy vector\r
511     this._posList.clear();\r
512 \r
513     this._posList.addAll(vPosList);\r
514   }\r
515 \r
516   /**\r
517    * Sets the value of '_posList' by setting it to the given Vector. No type\r
518    * checking is performed.\r
519    * \r
520    * @deprecated\r
521    * \r
522    * @param posVector\r
523    *          the Vector to set.\r
524    */\r
525   public void setPosAsReference(final java.util.Vector posVector) {\r
526     this._posList = posVector;\r
527   }\r
528 \r
529   /**\r
530    * \r
531    * \r
532    * @param index\r
533    * @param vSeg\r
534    * @throws java.lang.IndexOutOfBoundsException\r
535    *           if the index given is outside the bounds of the collection\r
536    */\r
537   public void setSeg(final int index, final uk.ac.vamsas.objects.core.Seg vSeg)\r
538       throws java.lang.IndexOutOfBoundsException {\r
539     // check bounds for index\r
540     if (index < 0 || index >= this._segList.size()) {\r
541       throw new IndexOutOfBoundsException("setSeg: Index value '" + index\r
542           + "' not in range [0.." + (this._segList.size() - 1) + "]");\r
543     }\r
544 \r
545     this._segList.set(index, vSeg);\r
546   }\r
547 \r
548   /**\r
549    * \r
550    * \r
551    * @param vSegArray\r
552    */\r
553   public void setSeg(final uk.ac.vamsas.objects.core.Seg[] vSegArray) {\r
554     // -- copy array\r
555     _segList.clear();\r
556 \r
557     for (int i = 0; i < vSegArray.length; i++) {\r
558       this._segList.add(vSegArray[i]);\r
559     }\r
560   }\r
561 \r
562   /**\r
563    * Sets the value of '_segList' by copying the given Vector. All elements will\r
564    * be checked for type safety.\r
565    * \r
566    * @param vSegList\r
567    *          the Vector to copy.\r
568    */\r
569   public void setSeg(final java.util.Vector vSegList) {\r
570     // copy vector\r
571     this._segList.clear();\r
572 \r
573     this._segList.addAll(vSegList);\r
574   }\r
575 \r
576   /**\r
577    * Sets the value of '_segList' by setting it to the given Vector. No type\r
578    * checking is performed.\r
579    * \r
580    * @deprecated\r
581    * \r
582    * @param segVector\r
583    *          the Vector to set.\r
584    */\r
585   public void setSegAsReference(final java.util.Vector segVector) {\r
586     this._segList = segVector;\r
587   }\r
588 \r
589   /**\r
590    * \r
591    * \r
592    * @throws org.exolab.castor.xml.ValidationException\r
593    *           if this object is an invalid instance according to the schema\r
594    */\r
595   public void validate() throws org.exolab.castor.xml.ValidationException {\r
596     org.exolab.castor.xml.Validator validator = new org.exolab.castor.xml.Validator();\r
597     validator.validate(this);\r
598   }\r
599 \r
600 }\r