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.HashMap;
027import java.util.Map;
028
029/**
030 * A no-frills implementation of TranslationTable that uses a Map to map from
031 * symbols in a finite source alphabet into a target alphabet.
032 *
033 * @author Matthew Pocock
034 * @author David Huen (refactoring)
035 */
036public class SimpleTranslationTable 
037      extends AbstractTranslationTable
038      implements Serializable {
039  private final Map transMap;
040  private final FiniteAlphabet source;
041  private final Alphabet target;
042
043  public Alphabet getSourceAlphabet() {
044    return source;
045  }
046
047  public Alphabet getTargetAlphabet() {
048    return target;
049  }
050
051  public Symbol doTranslate(Symbol sym) {
052    return (Symbol) transMap.get(sym);
053  }
054
055  /**
056   * Alter the translation mapping.
057   *
058   * @param from source AtomicSymbol
059   * @param to   target AtomicSymbol to be returned by translate(from)
060   * @throws IllegalSymbolException if either from is not in the source
061   *         alphabet or to is not in the target alphabet
062   */
063  public void setTranslation(AtomicSymbol from, AtomicSymbol to)
064  throws IllegalSymbolException {
065    source.validate(from);
066    target.validate(to);
067    transMap.put(from, to);
068  }
069
070  /**
071   * Create a new translation table that will translate symbols from source to
072   * target.
073   * <p>
074   * The source alphabet must be finite, as a Map object is used to associate
075   * a source Symbol with a target Symbol.
076   * The target alphabet need not be finite.
077   *
078   * @param source  the FiniteAlphabet to translate from
079   * @param target  the Alphabet to translate into
080   */
081  public SimpleTranslationTable(FiniteAlphabet source, Alphabet target) {
082    this.source = source;
083    this.target = target;
084    this.transMap = new HashMap();
085  }
086
087  /**
088   * Create a new translation table that will translate symbols from source to
089   * target.
090   * <p>
091   * The Map transMap should contain keys in the source alphabet with values in
092   * the target alphabet. However, this is not currently checked.
093   * <p>
094   * The source alphabet must be finite, as a Map object is used to associate
095   * a source Symbol with a target Symbol.
096   * The target alphabet need not be finite.
097   *
098   * @param source  the FiniteAlphabet to translate from
099   * @param target  the Alphabet to translate into
100   */
101  public SimpleTranslationTable(
102    FiniteAlphabet source, Alphabet target, Map transMap
103  ) {
104    this.source = source;
105    this.target = target;
106    this.transMap = transMap;
107  }
108}