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.io;
023
024import java.io.NotSerializableException;
025import java.io.ObjectStreamException;
026import java.io.Serializable;
027
028import org.biojava.bio.BioException;
029import org.biojava.bio.seq.Sequence;
030import org.biojava.bio.seq.impl.SimpleSequence;
031import org.biojava.bio.symbol.Alphabet;
032import org.biojava.bio.symbol.IllegalAlphabetException;
033import org.biojava.bio.symbol.SimpleSymbolListFactory;
034import org.biojava.bio.symbol.Symbol;
035import org.biojava.bio.symbol.SymbolList;
036import org.biojava.utils.StaticMemberPlaceHolder;
037
038/**
039 * Basic SequenceBuilder implementation which accumulates all
040 * notified information and creates a SimpleSequence.
041 *
042 * <p>More functionality is offered by {@link org.biojavax.bio.seq.io.SimpleRichSequenceBuilder SimpleRichSequenceBuilder},
043 * Use of this class is prefered.</p>
044 *
045 * @author Thomas Down
046 * @author David Huen (modified to derive from SequenceBuilderBase)
047 * @version 1.1 [newio proposal]
048 * @see org.biojavax.bio.seq.io.SimpleRichSequenceBuilder
049 */
050
051public class SimpleSequenceBuilder extends SequenceBuilderBase {
052    public final static SequenceBuilderFactory FACTORY = new SSBFactory();
053
054    private static class SSBFactory implements SequenceBuilderFactory, Serializable {
055        private SSBFactory() {
056        }
057
058        public SequenceBuilder makeSequenceBuilder() {
059            return new SimpleSequenceBuilder();
060        }
061
062        private Object writeReplace() throws ObjectStreamException {
063            try {
064                return new StaticMemberPlaceHolder(SimpleSequenceBuilder.class.getField("FACTORY"));
065            } catch (NoSuchFieldException nsfe) {
066                throw new NotSerializableException(nsfe.getMessage());
067            }
068        }
069    }
070
071  private ChunkedSymbolListFactory slFactory;
072
073  {
074         slFactory = new ChunkedSymbolListFactory(new SimpleSymbolListFactory());
075  }
076
077    //
078    // SeqIOListener
079    //
080
081    public void addSymbols(Alphabet alpha, Symbol[] syms, int pos, int len)
082        throws IllegalAlphabetException
083    {
084        slFactory.addSymbols(alpha, syms, pos, len);
085    }
086
087
088  public Sequence makeSequence()
089          throws BioException {
090        SymbolList symbols = slFactory.makeSymbolList();
091        seq = new SimpleSequence(symbols, uri, name, annotation);
092
093    return super.makeSequence();
094  }
095}