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.util.Iterator;
025
026import org.biojava.bio.BioError;
027import org.biojava.bio.BioException;
028import org.biojava.utils.AbstractChangeable;
029import org.biojava.utils.ChangeVetoException;
030
031/**
032 * An abstract implementation of FeatureHolder.
033 *
034 * This provides the filter method, but who wants to code that more than
035 * once? It also has support for the ChangeEvents.
036 *
037 * @author Matthew Pocock
038 * @author Thomas Down
039 */
040public abstract class AbstractFeatureHolder
041  extends
042    AbstractChangeable
043  implements
044    FeatureHolder
045{
046    public FeatureHolder filter(FeatureFilter filter) {
047        boolean recurse = !FilterUtils.areProperSubset(filter, FeatureFilter.top_level);
048        return filter(filter, recurse);
049    }
050
051  public FeatureHolder filter(FeatureFilter ff, boolean recurse) {
052    SimpleFeatureHolder res = new SimpleFeatureHolder();
053    for(Iterator f = features(); f.hasNext();) {
054      Feature feat = (Feature) f.next();
055      if(ff.accept(feat)) {
056        try {
057          res.addFeature(feat);
058        } catch (ChangeVetoException cve) {
059          throw new BioError(
060            "Assertion failed: Couldn't add a feature to my new FeatureHolder"
061          );
062        }
063      }
064      if(recurse) {
065        FeatureHolder r = feat.filter(ff, recurse);
066        for(Iterator rf = r.features(); rf.hasNext();) {
067          try {
068            res.addFeature((Feature) rf.next());
069          } catch (ChangeVetoException cve) {
070            throw new BioError(
071              "Assertion failure: Should be able to manipulate this FeatureHolder", cve
072            );
073          }
074        }
075      }
076    }
077    return res;
078  }
079
080  public Feature createFeature(Feature.Template temp)
081  throws BioException, ChangeVetoException {
082    throw new ChangeVetoException(
083      "This FeatureHolder does not support creation of new Features."
084    );
085  }
086
087  public void removeFeature(Feature f)
088  throws ChangeVetoException, BioException {
089    throw new ChangeVetoException(
090      "This FeatureHolder does not support removal of Features."
091    );
092  }
093}