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.db;
023
024import org.biojava.utils.ChangeEvent;
025import org.biojava.utils.ChangeForwarder;
026import org.biojava.utils.ChangeSupport;
027import org.biojava.utils.ChangeType;
028
029/**
030 * An abstract implementation of SequenceDB that wraps up another database.
031 *
032 * @author Matthew Pocock
033 */
034public abstract class SequenceDBWrapper extends AbstractSequenceDB
035implements java.io.Serializable{
036  private final SequenceDB parent;
037  private transient SequencesForwarder seqFor;
038  
039  protected ChangeSupport getChangeSupport(ChangeType ct) {
040    ChangeSupport changeSupport = super.getChangeSupport(ct);
041    
042    if(ct.isMatchingType(SequenceDB.SEQUENCES)) {
043      seqFor = new SequencesForwarder(this, changeSupport);
044      parent.addChangeListener(seqFor, SequenceDB.SEQUENCES);
045    }
046    
047    return changeSupport;
048  }
049  
050  /**
051   * Return the parent SequenceDB.
052   *
053   * @return the parent SequenceDB
054   */
055  public SequenceDB getParent() {
056    return this.parent;
057  }
058  
059  protected class SequencesForwarder extends ChangeForwarder {
060    public SequencesForwarder(Object source, ChangeSupport cs) {
061      super(source, cs);
062    }
063    
064    public ChangeEvent generateEvent(ChangeEvent ce) {
065      if(ce.getType() == SequenceDB.SEQUENCES) {
066        Object previous = ce.getPrevious();
067        if(previous != null) {
068          return new ChangeEvent(
069            getSource(),
070            SequenceDB.SEQUENCES,
071            null,
072            previous,
073            ce
074          );
075        }
076      }
077      return null;
078    }
079  }
080
081  public SequenceDBWrapper(SequenceDB parent) {
082    this.parent = parent;
083  }
084}