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