/*
 *                    BioJava development code
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  If you do not have a copy,
 * see:
 *
 *      http://www.gnu.org/copyleft/lesser.html
 *
 * Copyright for this code is held jointly by the individual
 * authors.  These should be listed in @author doc comments.
 *
 * For more information on the BioJava project and its aims,
 * or to join the biojava-l mailing list, visit the home page
 * at:
 *
 *      http://www.biojava.org/
 *
 */

package org.biojava.bio.symbol;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Collections;

import org.biojava.bio.BioError;

/**
 * An n-th order view of another SymbolList.
 * <p>
 * In practice, what this means is that you can view a DNA sequence into an
 * overlapping dinucleotide sequence without having to do any work yourself.
 * <p>  Example
 * <p>  1 2 3 4 5 6 7      Index
 * <p>  a t c g g a t      Source SymbolList
 * <P>  a t
 * <P>    t c
 * <P>      c g...         Order 2 OrderNSymbolList
 * <p>
 * <p> Or overlapping trinucleotide sequence:
 * <P>  a t c
 * <p>    t c g...         Order 3 OrderNSymbolList
 *
 * @author Matthew Pocock
 * @author George Waldon (javadoc)
 */

class OrderNSymbolList extends AbstractSymbolList implements Serializable {
  /**
   * The source sequence that we will transliterate.
   */
  private final SymbolList source;

  /**
   * The alphabet for each overlapping tuple.
   */
  private final Alphabet alpha;

  /**
   * The view order.
   */
  private final int order;

  /**
   * Retrieve the underlying SymbolList being viewed.
   *
   * @return the source SymbolList
   */
  public SymbolList getSource() {
    return source;
  }

  /**
   * Create an order n OrderNSymbolList from source.
   */
  public OrderNSymbolList(SymbolList source, int order)
  throws IllegalAlphabetException {
    this.source = source;
    Alphabet a = source.getAlphabet();
    this.alpha = AlphabetManager.getCrossProductAlphabet(
      Collections.nCopies(order, a)
    );
    this.order = order;
  }

  public Alphabet getAlphabet() {
    return alpha;
  }

  public int length() {
    return source.length() - order + 1;
  }

  public Symbol symbolAt(int index)
  throws IndexOutOfBoundsException {
    if(index < 1 || index > length()) {
      throw new IndexOutOfBoundsException(
        "index must be within (1 .. " +
        length() + "), not " + index
      );
    }

    try {
      // changed to this form to avoid constructing the sub-list objects
      Symbol [] syms = new Symbol[order];
      for(int i = 0; i < order; i++) {
        syms[i] = source.symbolAt(index + i);
      }
      return alpha.getSymbol(Arrays.asList(syms));
    } catch (IllegalSymbolException iae) {
      throw new BioError("Alphabet changed underneath me", iae);
    }
  }
}

@@ -969,25 +969,32 @@
  Example
+:
 %0A * %3Cp
-%3E
+re%3E%0A *
   
+ 
 1 2 3 4 
@@ -1013,19 +1013,16 @@
 ndex%0A * 
-%3Cp%3E
   a t c 
@@ -1051,27 +1051,24 @@
 mbolList%0A * 
-%3CP%3E
   a t%0A * %3CP%3E
@@ -1060,27 +1060,24 @@
  *   a t%0A * 
-%3CP%3E
     t c%0A * %3C
@@ -1075,19 +1075,16 @@
  t c%0A * 
-%3CP%3E
       c 
@@ -1128,92 +1128,50 @@
 %0A * 
-%3Cp%3E
 %0A * 
-%3Cp%3E Or overlapping trinucleotide sequence:
+  a t c
 %0A * 
-%3CP%3E
+ 
   
-a
  t c
+ g
 %0A * 
-%3Cp%3E
     
-t
+ 
  c 
+g 
 g...
-  
     
@@ -1198,16 +1198,26 @@
 bolList%0A
+ * %3C/pre%3E%0A
  *%0A * @a

Use of <pre> tag in javadoc