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
025import java.io.Serializable;
026import java.util.Collections;
027import java.util.Iterator;
028import java.util.List;
029import java.util.NoSuchElementException;
030
031import org.biojava.bio.Annotation;
032import org.biojava.bio.seq.io.SymbolTokenization;
033import org.biojava.utils.SingletonList;
034
035/**
036 * An alphabet that contains a single atomic symbol.
037 *
038 * @author Matthew Pocock
039 */
040public class SingletonAlphabet
041extends AbstractAlphabet
042implements FiniteAlphabet, Serializable {
043  private final AtomicSymbol sym;
044  private List alphabets;
045  
046  public SingletonAlphabet(AtomicSymbol sym) {
047    this.sym = sym;
048  }
049
050  public List getAlphabets() {
051    if(this.alphabets == null) {
052      this.alphabets = new SingletonList(this);
053    }
054    return this.alphabets;
055  }
056  
057  protected boolean containsImpl(AtomicSymbol s) {
058    return s == sym;
059  }
060  
061  public String getName() {
062    return sym.getName() + "-alphabet";
063  }
064  
065  public SymbolTokenization getTokenization(String name)
066  throws NoSuchElementException {
067    throw new NoSuchElementException(
068      "No parsers associated with " + getName() +
069      ": " + name
070    );
071  }
072  
073  public Iterator iterator() {
074    return Collections.singleton(sym).iterator();
075  }
076  
077  public int size() {
078    return 1;
079  }
080  
081  public Annotation getAnnotation() {
082    return Annotation.EMPTY_ANNOTATION;
083  }
084  
085  public void addSymbolImpl(AtomicSymbol sym) throws IllegalSymbolException {
086    throw new IllegalSymbolException(
087      "Can't add symbols to alphabet: " + sym.getName() +
088      " in " + getName()
089    );
090  }
091  
092  public void removeSymbol(Symbol sym) throws IllegalSymbolException {
093    throw new IllegalSymbolException(
094      "Can't remove symbols from alphabet: " + sym.getName() +
095      " in " + getName()
096    );
097  }
098  
099  protected AtomicSymbol getSymbolImpl(List symList)
100  throws IllegalSymbolException {
101    return (AtomicSymbol) symList.get(0);
102  }
103}