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
022
023package org.biojava.bio.symbol;
024
025
026/**
027 * This class makes PackedSymbolLists.
028 * It could be refactored into the PackedSymbolList class eventually.
029 *
030 * @author David Huen
031 */
032public class PackedSymbolListFactory
033
034    implements SymbolListFactory
035{
036    private final static int AUTO_SELECT = 1;
037    private final static int USER_SELECT = 2;
038
039    private boolean ambiguity;
040    private int opMode = AUTO_SELECT;
041
042    /**
043     * Create a factory for PackedSymbolLists.
044     * The use of ambiguity packing is determined automatically
045     * as required.
046     */
047    public PackedSymbolListFactory()
048    {
049        opMode = AUTO_SELECT;
050        ambiguity = false; // set to avoid javac bug
051    }
052
053    /**
054     * Create a factory for PackedSymbolLists with specified packing type.
055     *
056     * @param ambiguity is ambiguity to be supported by the encoding?
057     * @deprecated the argumentless constructor creates a SymbolListFactory
058     *   that will autoselect the packing appropriately.
059     */
060    public PackedSymbolListFactory(boolean ambiguity)
061    {
062        opMode = USER_SELECT;
063        this.ambiguity = ambiguity;
064    }
065 /**
066   * Makes a packed SymbolList out of a list of Symbols.
067   *
068   * @param symbolArray the symbols to be make in a packed SymbolList
069   * @param size the length of the symbolArray array.
070   * @param alfa the Alphabet over which the SymbolList shoudl be
071   * @return a packed SymbolList with the Symbols in symbolArray and the
072   * Alphabet in alfa
073   * @exception IllegalAlphabetException if alfa and the Symbols in
074   *symbolArray disagree.
075   */
076    public SymbolList makeSymbolList(Symbol [] symbolArray, int size, Alphabet alfa)
077        throws IllegalAlphabetException
078    {
079        switch (opMode) {
080            case AUTO_SELECT:
081                // check for ambiguity symbols
082                ambiguity = false;
083                for (int i=0; i < size; i++) {
084                    if (!(symbolArray[i] instanceof AtomicSymbol)) { ambiguity = true; break; }
085                }
086            case USER_SELECT:
087            default:
088                return new PackedSymbolList(PackingFactory.getPacking((FiniteAlphabet) alfa, ambiguity), symbolArray, size, alfa);
089        }
090    }
091}
092