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.biojavax.bio.seq;
023import java.util.Collections;
024import java.util.Iterator;
025import java.util.List;
026
027import org.biojava.bio.symbol.Alphabet;
028import org.biojava.bio.symbol.AlphabetManager;
029import org.biojava.bio.symbol.Edit;
030import org.biojava.bio.symbol.FiniteAlphabet;
031import org.biojava.bio.symbol.IllegalAlphabetException;
032import org.biojava.bio.symbol.Symbol;
033import org.biojava.bio.symbol.SymbolList;
034import org.biojava.utils.ChangeListener;
035import org.biojava.utils.ChangeType;
036import org.biojava.utils.ChangeVetoException;
037
038/**
039 * A symbol list that is <code>Integer.MAX_VALUE</code>long, never gives index out of
040 * bounds and always returns ambiguity symbols for everything.
041 * @author Richard Holland
042 * @author MarkSchreiber
043 * @since 1.5
044 */
045public class InfinitelyAmbiguousSymbolList implements SymbolList {
046    
047    private FiniteAlphabet fa;
048    private Symbol sym;
049    
050    /**
051     * 
052     * Creates a new instance of InfinitelyAmbiguousSymbolList 
053     * @param fa the finite alphabet to return ambiguous symbols from.
054     */
055    public InfinitelyAmbiguousSymbolList(FiniteAlphabet fa) {
056        this.fa = fa;
057        this.sym = AlphabetManager.getAllAmbiguitySymbol(fa);
058    }
059
060    /**
061     * {@inheritDoc}
062     * IGNORED
063     */
064    public void addChangeListener(ChangeListener cl) {}
065
066    /**
067     * {@inheritDoc}
068     * IGNORED
069     */
070    public void addChangeListener(ChangeListener cl, ChangeType ct) {}
071
072    /**
073     * {@inheritDoc}
074     * IGNORED
075     */
076    public void edit(Edit edit) throws IndexOutOfBoundsException, IllegalAlphabetException, ChangeVetoException {}
077
078    /**
079     * {@inheritDoc}
080     */
081    public Alphabet getAlphabet() { return this.fa; }
082
083    /**
084     * {@inheritDoc}
085     * ALWAYS RETURNS TRUE
086     */
087    public boolean isUnchanging(ChangeType ct) { return true; }
088
089    /**
090     * {@inheritDoc}
091     * ALWAYS RETURNS AN ITERATOR OVER A SINGLE AMBIGUITY SYMBOL
092     */
093    public Iterator iterator() { return Collections.singletonList(this.sym).iterator(); }
094
095    /**
096     * {@inheritDoc}
097     * ALWAYS RETURNS <code>Integer.MAX_VALUE</code>
098     */
099    public int length() { return Integer.MAX_VALUE; }
100
101    /**
102     * {@inheritDoc}
103     * IGNORED
104     */
105    public void removeChangeListener(ChangeListener cl) {}
106
107    /**
108     * {@inheritDoc}
109     * IGNORED
110     */
111    public void removeChangeListener(ChangeListener cl, ChangeType ct) {}
112
113    /**
114     * {@inheritDoc}
115     * ALWAYS RETURNS THE AMBIGUITY SYMBOL REPRESENTED AS A STRING
116     */
117    public String seqString() { return this.sym.toString(); }
118
119    /**
120     * {@inheritDoc}
121     * ALWAYS RETURNS SELF
122     */
123    public SymbolList subList(int start, int end) throws IndexOutOfBoundsException { return this; }
124
125    /**
126     * {@inheritDoc}
127     * ALWAYS RETURNS THE CORRECT LENGTH STRING MADE UP OF AMBIGUITY SYMBOLS
128     */
129    public String subStr(int start, int end) throws IndexOutOfBoundsException {
130        int min = Math.min(start,end);
131        int max = Math.max(start,end);
132        StringBuffer sb = new StringBuffer();
133        String symStr = this.sym.toString();
134        while (max-->=min) sb.append(symStr);
135        return sb.toString();
136    }
137
138    /**
139     * {@inheritDoc}
140     * ALWAYS RETURNS THE AMBIGUITY SYMBOL
141     */
142    public Symbol symbolAt(int index) throws IndexOutOfBoundsException { return this.sym; }
143
144    /**
145     * {@inheritDoc}
146     * ALWAYS RETURNS A LIST CONTAINING THE AMBIGUITY SYMBOL
147     */
148    public List toList() { return Collections.singletonList(this.sym); }
149    
150}