0ba05637cac2eed60a4a30920d334325f374e487
[jalview.git] / src / jalview / binding / SequenceSet.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3  * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
10  *  
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.binding;
19
20 //---------------------------------/
21 //- Imported classes and packages -/
22 //---------------------------------/
23
24 import org.exolab.castor.xml.Marshaller;
25 import org.exolab.castor.xml.Unmarshaller;
26
27 /**
28  * Class SequenceSet.
29  * 
30  * @version $Revision$ $Date$
31  */
32 public class SequenceSet implements java.io.Serializable
33 {
34
35   // --------------------------/
36   // - Class/Member Variables -/
37   // --------------------------/
38
39   /**
40    * Field _gapChar.
41    */
42   private java.lang.String _gapChar;
43
44   /**
45    * Field _aligned.
46    */
47   private boolean _aligned;
48
49   /**
50    * keeps track of state for field: _aligned
51    */
52   private boolean _has_aligned;
53
54   /**
55    * Field _sequenceList.
56    */
57   private java.util.Vector _sequenceList;
58
59   /**
60    * Field _annotationList.
61    */
62   private java.util.Vector _annotationList;
63
64   // ----------------/
65   // - Constructors -/
66   // ----------------/
67
68   public SequenceSet()
69   {
70     super();
71     this._sequenceList = new java.util.Vector();
72     this._annotationList = new java.util.Vector();
73   }
74
75   // -----------/
76   // - Methods -/
77   // -----------/
78
79   /**
80    * 
81    * 
82    * @param vAnnotation
83    * @throws java.lang.IndexOutOfBoundsException
84    *           if the index given is outside the bounds of the collection
85    */
86   public void addAnnotation(final jalview.binding.Annotation vAnnotation)
87           throws java.lang.IndexOutOfBoundsException
88   {
89     this._annotationList.addElement(vAnnotation);
90   }
91
92   /**
93    * 
94    * 
95    * @param index
96    * @param vAnnotation
97    * @throws java.lang.IndexOutOfBoundsException
98    *           if the index given is outside the bounds of the collection
99    */
100   public void addAnnotation(final int index,
101           final jalview.binding.Annotation vAnnotation)
102           throws java.lang.IndexOutOfBoundsException
103   {
104     this._annotationList.add(index, vAnnotation);
105   }
106
107   /**
108    * 
109    * 
110    * @param vSequence
111    * @throws java.lang.IndexOutOfBoundsException
112    *           if the index given is outside the bounds of the collection
113    */
114   public void addSequence(final jalview.binding.Sequence vSequence)
115           throws java.lang.IndexOutOfBoundsException
116   {
117     this._sequenceList.addElement(vSequence);
118   }
119
120   /**
121    * 
122    * 
123    * @param index
124    * @param vSequence
125    * @throws java.lang.IndexOutOfBoundsException
126    *           if the index given is outside the bounds of the collection
127    */
128   public void addSequence(final int index,
129           final jalview.binding.Sequence vSequence)
130           throws java.lang.IndexOutOfBoundsException
131   {
132     this._sequenceList.add(index, vSequence);
133   }
134
135   /**
136      */
137   public void deleteAligned()
138   {
139     this._has_aligned = false;
140   }
141
142   /**
143    * Method enumerateAnnotation.
144    * 
145    * @return an Enumeration over all jalview.binding.Annotation elements
146    */
147   public java.util.Enumeration enumerateAnnotation()
148   {
149     return this._annotationList.elements();
150   }
151
152   /**
153    * Method enumerateSequence.
154    * 
155    * @return an Enumeration over all jalview.binding.Sequence elements
156    */
157   public java.util.Enumeration enumerateSequence()
158   {
159     return this._sequenceList.elements();
160   }
161
162   /**
163    * Returns the value of field 'aligned'.
164    * 
165    * @return the value of field 'Aligned'.
166    */
167   public boolean getAligned()
168   {
169     return this._aligned;
170   }
171
172   /**
173    * Method getAnnotation.
174    * 
175    * @param index
176    * @throws java.lang.IndexOutOfBoundsException
177    *           if the index given is outside the bounds of the collection
178    * @return the value of the jalview.binding.Annotation at the given index
179    */
180   public jalview.binding.Annotation getAnnotation(final int index)
181           throws java.lang.IndexOutOfBoundsException
182   {
183     // check bounds for index
184     if (index < 0 || index >= this._annotationList.size())
185     {
186       throw new IndexOutOfBoundsException("getAnnotation: Index value '"
187               + index + "' not in range [0.."
188               + (this._annotationList.size() - 1) + "]");
189     }
190
191     return (jalview.binding.Annotation) _annotationList.get(index);
192   }
193
194   /**
195    * Method getAnnotation.Returns the contents of the collection in an Array.
196    * <p>
197    * Note: Just in case the collection contents are changing in another thread,
198    * we pass a 0-length Array of the correct type into the API call. This way we
199    * <i>know</i> that the Array returned is of exactly the correct length.
200    * 
201    * @return this collection as an Array
202    */
203   public jalview.binding.Annotation[] getAnnotation()
204   {
205     jalview.binding.Annotation[] array = new jalview.binding.Annotation[0];
206     return (jalview.binding.Annotation[]) this._annotationList
207             .toArray(array);
208   }
209
210   /**
211    * Method getAnnotationCount.
212    * 
213    * @return the size of this collection
214    */
215   public int getAnnotationCount()
216   {
217     return this._annotationList.size();
218   }
219
220   /**
221    * Returns the value of field 'gapChar'.
222    * 
223    * @return the value of field 'GapChar'.
224    */
225   public java.lang.String getGapChar()
226   {
227     return this._gapChar;
228   }
229
230   /**
231    * Method getSequence.
232    * 
233    * @param index
234    * @throws java.lang.IndexOutOfBoundsException
235    *           if the index given is outside the bounds of the collection
236    * @return the value of the jalview.binding.Sequence at the given index
237    */
238   public jalview.binding.Sequence getSequence(final int index)
239           throws java.lang.IndexOutOfBoundsException
240   {
241     // check bounds for index
242     if (index < 0 || index >= this._sequenceList.size())
243     {
244       throw new IndexOutOfBoundsException("getSequence: Index value '"
245               + index + "' not in range [0.."
246               + (this._sequenceList.size() - 1) + "]");
247     }
248
249     return (jalview.binding.Sequence) _sequenceList.get(index);
250   }
251
252   /**
253    * Method getSequence.Returns the contents of the collection in an Array.
254    * <p>
255    * Note: Just in case the collection contents are changing in another thread,
256    * we pass a 0-length Array of the correct type into the API call. This way we
257    * <i>know</i> that the Array returned is of exactly the correct length.
258    * 
259    * @return this collection as an Array
260    */
261   public jalview.binding.Sequence[] getSequence()
262   {
263     jalview.binding.Sequence[] array = new jalview.binding.Sequence[0];
264     return (jalview.binding.Sequence[]) this._sequenceList.toArray(array);
265   }
266
267   /**
268    * Method getSequenceCount.
269    * 
270    * @return the size of this collection
271    */
272   public int getSequenceCount()
273   {
274     return this._sequenceList.size();
275   }
276
277   /**
278    * Method hasAligned.
279    * 
280    * @return true if at least one Aligned has been added
281    */
282   public boolean hasAligned()
283   {
284     return this._has_aligned;
285   }
286
287   /**
288    * Returns the value of field 'aligned'.
289    * 
290    * @return the value of field 'Aligned'.
291    */
292   public boolean isAligned()
293   {
294     return this._aligned;
295   }
296
297   /**
298    * Method isValid.
299    * 
300    * @return true if this object is valid according to the schema
301    */
302   public boolean isValid()
303   {
304     try
305     {
306       validate();
307     } catch (org.exolab.castor.xml.ValidationException vex)
308     {
309       return false;
310     }
311     return true;
312   }
313
314   /**
315    * 
316    * 
317    * @param out
318    * @throws org.exolab.castor.xml.MarshalException
319    *           if object is null or if any SAXException is thrown during
320    *           marshaling
321    * @throws org.exolab.castor.xml.ValidationException
322    *           if this object is an invalid instance according to the schema
323    */
324   public void marshal(final java.io.Writer out)
325           throws org.exolab.castor.xml.MarshalException,
326           org.exolab.castor.xml.ValidationException
327   {
328     Marshaller.marshal(this, out);
329   }
330
331   /**
332    * 
333    * 
334    * @param handler
335    * @throws java.io.IOException
336    *           if an IOException occurs during marshaling
337    * @throws org.exolab.castor.xml.ValidationException
338    *           if this object is an invalid instance according to the schema
339    * @throws org.exolab.castor.xml.MarshalException
340    *           if object is null or if any SAXException is thrown during
341    *           marshaling
342    */
343   public void marshal(final org.xml.sax.ContentHandler handler)
344           throws java.io.IOException,
345           org.exolab.castor.xml.MarshalException,
346           org.exolab.castor.xml.ValidationException
347   {
348     Marshaller.marshal(this, handler);
349   }
350
351   /**
352      */
353   public void removeAllAnnotation()
354   {
355     this._annotationList.clear();
356   }
357
358   /**
359      */
360   public void removeAllSequence()
361   {
362     this._sequenceList.clear();
363   }
364
365   /**
366    * Method removeAnnotation.
367    * 
368    * @param vAnnotation
369    * @return true if the object was removed from the collection.
370    */
371   public boolean removeAnnotation(
372           final jalview.binding.Annotation vAnnotation)
373   {
374     boolean removed = _annotationList.remove(vAnnotation);
375     return removed;
376   }
377
378   /**
379    * Method removeAnnotationAt.
380    * 
381    * @param index
382    * @return the element removed from the collection
383    */
384   public jalview.binding.Annotation removeAnnotationAt(final int index)
385   {
386     java.lang.Object obj = this._annotationList.remove(index);
387     return (jalview.binding.Annotation) obj;
388   }
389
390   /**
391    * Method removeSequence.
392    * 
393    * @param vSequence
394    * @return true if the object was removed from the collection.
395    */
396   public boolean removeSequence(final jalview.binding.Sequence vSequence)
397   {
398     boolean removed = _sequenceList.remove(vSequence);
399     return removed;
400   }
401
402   /**
403    * Method removeSequenceAt.
404    * 
405    * @param index
406    * @return the element removed from the collection
407    */
408   public jalview.binding.Sequence removeSequenceAt(final int index)
409   {
410     java.lang.Object obj = this._sequenceList.remove(index);
411     return (jalview.binding.Sequence) obj;
412   }
413
414   /**
415    * Sets the value of field 'aligned'.
416    * 
417    * @param aligned
418    *          the value of field 'aligned'.
419    */
420   public void setAligned(final boolean aligned)
421   {
422     this._aligned = aligned;
423     this._has_aligned = true;
424   }
425
426   /**
427    * 
428    * 
429    * @param index
430    * @param vAnnotation
431    * @throws java.lang.IndexOutOfBoundsException
432    *           if the index given is outside the bounds of the collection
433    */
434   public void setAnnotation(final int index,
435           final jalview.binding.Annotation vAnnotation)
436           throws java.lang.IndexOutOfBoundsException
437   {
438     // check bounds for index
439     if (index < 0 || index >= this._annotationList.size())
440     {
441       throw new IndexOutOfBoundsException("setAnnotation: Index value '"
442               + index + "' not in range [0.."
443               + (this._annotationList.size() - 1) + "]");
444     }
445
446     this._annotationList.set(index, vAnnotation);
447   }
448
449   /**
450    * 
451    * 
452    * @param vAnnotationArray
453    */
454   public void setAnnotation(
455           final jalview.binding.Annotation[] vAnnotationArray)
456   {
457     // -- copy array
458     _annotationList.clear();
459
460     for (int i = 0; i < vAnnotationArray.length; i++)
461     {
462       this._annotationList.add(vAnnotationArray[i]);
463     }
464   }
465
466   /**
467    * Sets the value of field 'gapChar'.
468    * 
469    * @param gapChar
470    *          the value of field 'gapChar'.
471    */
472   public void setGapChar(final java.lang.String gapChar)
473   {
474     this._gapChar = gapChar;
475   }
476
477   /**
478    * 
479    * 
480    * @param index
481    * @param vSequence
482    * @throws java.lang.IndexOutOfBoundsException
483    *           if the index given is outside the bounds of the collection
484    */
485   public void setSequence(final int index,
486           final jalview.binding.Sequence vSequence)
487           throws java.lang.IndexOutOfBoundsException
488   {
489     // check bounds for index
490     if (index < 0 || index >= this._sequenceList.size())
491     {
492       throw new IndexOutOfBoundsException("setSequence: Index value '"
493               + index + "' not in range [0.."
494               + (this._sequenceList.size() - 1) + "]");
495     }
496
497     this._sequenceList.set(index, vSequence);
498   }
499
500   /**
501    * 
502    * 
503    * @param vSequenceArray
504    */
505   public void setSequence(final jalview.binding.Sequence[] vSequenceArray)
506   {
507     // -- copy array
508     _sequenceList.clear();
509
510     for (int i = 0; i < vSequenceArray.length; i++)
511     {
512       this._sequenceList.add(vSequenceArray[i]);
513     }
514   }
515
516   /**
517    * Method unmarshal.
518    * 
519    * @param reader
520    * @throws org.exolab.castor.xml.MarshalException
521    *           if object is null or if any SAXException is thrown during
522    *           marshaling
523    * @throws org.exolab.castor.xml.ValidationException
524    *           if this object is an invalid instance according to the schema
525    * @return the unmarshaled jalview.binding.SequenceSet
526    */
527   public static jalview.binding.SequenceSet unmarshal(
528           final java.io.Reader reader)
529           throws org.exolab.castor.xml.MarshalException,
530           org.exolab.castor.xml.ValidationException
531   {
532     return (jalview.binding.SequenceSet) Unmarshaller.unmarshal(
533             jalview.binding.SequenceSet.class, reader);
534   }
535
536   /**
537    * 
538    * 
539    * @throws org.exolab.castor.xml.ValidationException
540    *           if this object is an invalid instance according to the schema
541    */
542   public void validate() throws org.exolab.castor.xml.ValidationException
543   {
544     org.exolab.castor.xml.Validator validator = new org.exolab.castor.xml.Validator();
545     validator.validate(this);
546   }
547
548 }