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.symbol;
023
024import java.io.Serializable;
025import java.util.Collections;
026import java.util.HashMap;
027import java.util.Map;
028
029import org.biojava.utils.SingletonList;
030
031/**
032 * <p>
033 * Encapsulates an edit operation on a SymbolList.
034 * See {@link org.biojava.bio.symbol.SymbolList
035 * SymbolList} for a full description.
036 * </p>
037 *
038 * @author Matthew Pocock
039 * @author George Waldon
040 */
041public final class Edit implements Serializable {
042  public final int pos;
043  public final int length;
044  public final SymbolList replacement;
045  public final Map<String, Object> props;
046
047  /**
048   * Create a new Edit.
049   *
050   * @param pos the start of the edit
051   * @param length the length of the edit
052   * @param replacement a SymbolList representing the symbols that replace those from pos to
053   *        pos + length-1 inclusive
054   */
055  public Edit(int pos, int length, SymbolList replacement) {
056    this.pos = pos;
057    this.length = length;
058    this.replacement = replacement;
059    props = Collections.emptyMap();
060  }
061  
062  /**
063   * Create a new Edit with some properties.
064   *
065   * @param pos the start of the edit
066   * @param length the length of the edit
067   * @param replacement a SymbolList representing the symbols that replace those from pos to
068   *        pos + length-1 inclusive
069   * @param props a map of String properties and associated objects adding 
070   *        metainformation to this edit; for example a property may describe
071   *        the fate of features surrounding this edit.
072   */
073  public Edit(int pos, int length, SymbolList replacement, Map<String, Object> props) {
074    this.pos = pos;
075    this.length = length;
076    this.replacement = replacement;
077    this.props = Collections.unmodifiableMap(new HashMap<String, Object>(props));
078  }
079
080  /**
081   * Convenience construtor for making single residue changes
082   *
083   * @param pos the position of the change
084   * @param alpha the <code>Alphabet</code> of the replacement <code>Symbol</code>
085   * @param replacement the replacement <code>Symbol</code>
086   * @throws IllegalSymbolException if the replacement <code>Symbol</code> is not contained in <code>alpha</code>
087   */
088  public Edit(int pos, Alphabet alpha, Symbol replacement) throws IllegalSymbolException{
089    this.pos = pos;
090    this.length = 1;
091    SymbolList sl = new SimpleSymbolList(
092        alpha, new SingletonList(replacement));
093    this.replacement = sl;
094    props = Collections.emptyMap();
095  }
096}
097