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.ComponentFeature;
030import org.biojava.bio.seq.Sequence;
031import org.biojava.bio.seq.SimpleAssembly;
032import org.biojava.bio.symbol.Alphabet;
033import org.biojava.bio.symbol.IllegalAlphabetException;
034import org.biojava.bio.symbol.Symbol;
035import org.biojava.utils.ChangeVetoException;
036import org.biojava.utils.StaticMemberPlaceHolder;
037
038/**
039 * Basic SequenceBuilder implementation which accumulates all
040 * notified information and creates a SimpleAssembly.
041 *
042 * @author David Huen
043 * @version 1.2
044 */
045
046public class SimpleAssemblyBuilder extends SequenceBuilderBase {
047    public final static SequenceBuilderFactory FACTORY = new SSBFactory();
048
049    private static class SSBFactory implements SequenceBuilderFactory, Serializable {
050        private SSBFactory() {
051        }
052
053        public SequenceBuilder makeSequenceBuilder() {
054            return new SimpleAssemblyBuilder();
055        }
056
057        private Object writeReplace() throws ObjectStreamException {
058            try {
059                return new StaticMemberPlaceHolder(SimpleAssemblyBuilder.class.getField("FACTORY"));
060            } catch (NoSuchFieldException nsfe) {
061                throw new NotSerializableException(nsfe.getMessage());
062            }
063        }
064    }
065
066    private void checkSeq()
067    {
068      // check that seq exists: if not, create it.
069      // this is done to permit lazy instatiation of the SimpleAssembly
070      // which gives time for name and uri to be set.
071      if (seq == null) seq = new SimpleAssembly(name, uri);
072    }
073
074    //
075    // SeqIOListener
076    //
077
078    public void addSymbols(Alphabet alpha, Symbol[] syms, int pos, int len)
079        throws IllegalAlphabetException
080    {
081      System.err.println("SimpleAssemblyBuilder: illegal attempt to add symbols");
082    }
083
084    public ComponentFeature addComponentSequence(ComponentFeature.Template cft)
085      throws BioException, ChangeVetoException
086    {
087        checkSeq();
088
089        return (ComponentFeature) seq.createFeature(cft);
090    }
091
092  public Sequence makeSequence()
093          throws BioException
094  {
095    checkSeq();
096
097    return super.makeSequence();
098  }
099}