001/*
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 */
021
022package org.biojava.bio.seq;
023
024import java.io.Serializable;
025import java.util.ArrayList;
026import java.util.Iterator;
027import java.util.List;
028
029import org.biojava.utils.ChangeEvent;
030import org.biojava.utils.ChangeSupport;
031import org.biojava.utils.ChangeVetoException;
032import org.biojava.utils.ListTools;
033
034/**
035 * A no-frills implementation of FeatureHolder.
036 *
037 * @see org.biojavax.bio.seq.RichFeatureRelationshipHolder
038 * @author Matthew Pocock
039 * @author Thomas Down
040 *
041 */
042public class SimpleFeatureHolder extends AbstractFeatureHolder implements Serializable{
043  /**
044   * The child features.
045   */
046  private List features;
047  private FeatureFilter schema;
048
049  /**
050   * Construct a new SimpleFeatureHolder with a non-informative schema.
051   */
052
053  public SimpleFeatureHolder() {
054      this.schema = FeatureFilter.all;
055  }
056
057  /**
058   * Construct a new SimpleFeatureHolder with the specified schema.
059   */
060
061  public SimpleFeatureHolder(FeatureFilter schema) {
062      this.schema = schema;
063  }
064
065  /**
066   * Initialize features.
067   */
068  {
069    features = new ArrayList();
070  }
071
072  /**
073  *Returns the list of features in this featureholder.
074  */
075  protected List getFeatures() {
076    return features;
077  }
078
079  public int countFeatures() {
080    return features.size();
081  }
082
083  public Iterator<Feature> features() {
084    return ListTools.nonRemoveIterator(features.iterator());
085  }
086
087    /**
088    *Add a feature to the featureholder
089    */
090
091  public void addFeature(Feature f)
092  throws ChangeVetoException {
093    if(!hasListeners()) {
094      features.add(f);
095    } else {
096      ChangeSupport changeSupport = getChangeSupport(FeatureHolder.FEATURES);
097      synchronized(changeSupport) {
098        ChangeEvent ce = new ChangeEvent(
099          this, FeatureHolder.FEATURES,
100          f, null
101        );
102        synchronized(ce){
103                changeSupport.firePreChangeEvent(ce);
104        }
105        features.add(f);
106        synchronized(ce){
107                changeSupport.firePostChangeEvent(ce);
108        }
109      }
110    }
111  }
112
113  public void removeFeature(Feature f)
114  throws ChangeVetoException {
115    if(!hasListeners()) {
116      features.remove(f);
117    } else {
118      ChangeSupport changeSupport = getChangeSupport(FeatureHolder.FEATURES);
119      synchronized(changeSupport) {
120        ChangeEvent ce = new ChangeEvent(
121          this, FeatureHolder.FEATURES,
122          null, f
123        );
124        changeSupport.firePreChangeEvent(ce);
125        features.remove(f);
126        changeSupport.firePostChangeEvent(ce);
127      }
128    }
129  }
130
131  public boolean containsFeature(Feature f) {
132    return features.contains(f);
133  }
134
135  public FeatureFilter getSchema() {
136      return schema;
137  }
138}