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.program.gff;
023
024
025/**
026 * An object that filters a stream of GFF, forwarding some
027 * <span class="type">GFFRecord</span>s to a
028 * listening <span class="type">GFFDocumentHandler</span>, and dropping others.
029 * <p>
030 * The choice to forward or drop is made by a
031 * <span class="type">GFFRecordFilter</span>.
032 * <p>
033 * Unless otherwise stated, all methods forward to the listening handler without
034 * altering the arguments in any way.
035 *
036 * @author Matthew Pocock
037 */
038public class GFFFilterer implements GFFDocumentHandler {
039  /**
040   * The <span class="type">GFFDocumentHandler</span> that will recieve all
041   * accepted entries.
042   */
043  private GFFDocumentHandler handler;
044  /**
045   * The <span class="type">GFFRecordFilter</span> that will decide what gets
046   * forwarded.
047   */
048  private GFFRecordFilter filter;
049
050  /**
051   * Create a new <span class="type">GFFFilterer</span> that will forward
052   * to <span class="arg">handler</span> everything that
053   * <span class="arg">filter</span> accepts.
054   *
055   * @param handler the listening <span class="type">GFFDocumentHandler</span>
056   * @param filter  the <span class="type">GFFRecordFilter</span> that decides
057   *                what is forwarded to <span class="arg">handler</span>
058   */
059  public GFFFilterer(GFFDocumentHandler handler, GFFRecordFilter filter) {
060    this.handler = handler;
061    this.filter = filter;
062  }
063  
064  public void startDocument(String locator) {
065    handler.startDocument(locator);
066  }
067  
068  public void endDocument() {
069    handler.endDocument();
070  }
071  
072  public void commentLine(String comment) {
073    handler.commentLine(comment);
074  }
075  
076  /**
077   * Only forward the <span class="type">GFFRecord</span>s that match a filter.
078   */
079  public void recordLine(GFFRecord record) {
080    if(filter.accept(record)) {
081      handler.recordLine(record);
082    }
083  }
084}