/*
** Module   : EachPageExclusion.java
** Abstract : represent a portion line that is to be excluded from comparisons
**
** Copyright (c) 2009, Golden Code Development Corporation.
**
** -#- -I- --Date-- -----------------------Description------------------------
** 001 GES 20090218 Represent a portion of a specific line on each page of a
**                  file, that is to be excluded from comparisons.
*/

/*
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License
** along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package com.goldencode.harness.test;

/**
 * Represent a portion of a specific line on each page of a file, that is to be
 * excluded from comparisons.
 * <p>
 * Each row is represented as EITHER a non-negative number that is indexed with
 * the first line on the page as line number 0 OR as a negative integer that is
 * indexed based on the page size. The row number should never be specified as
 * greater or equal to the length of the page. Nor should the row number ever
 * be less than the length of the page multiplied by -1. For example, to
 * reference the first line of a page use 0 and to reference the last line you
 * can use -1.
 */
public class EachPageExclusion
extends RowExclusion
{
   /**
    * Create an instance given explicit starting and ending indexes.
    *
    * @param    row
    *           The row number in which the exclusion should occur.
    * @param    start
    *           Starting index.
    * @param    end
    *           Ending index.
    */
   public EachPageExclusion(int row, int start, int end)
   {
      super(row, start, end);
   }
   
   /**              
    * Report if the current line is the line that is referenced by this
    * exclusion region.
    * <p>
    * The row number must be matched based on the configuration and type of
    * this instance. All row number processing is 0-based.
    *
    * @param    absNum
    *           Absolute row number as calculated from the beginning of the
    *           file.
    * @param    relNum
    *           Relative row number as calculated from the beginning of the
    *           current page.
    * @param    pageNum
    *           Current page number (0-based).
    * @param    pageSz
    *           Size (in number of lines) of the current page.
    * @param    isLast   
    *           <code>true</code> if the current page is the last page in the
    *           file.
    *
    * @return   <code>true</code> if the given row is the same row as is
    *           excluded by this instance.
    */
   public boolean isRow(int     absNum,
                        int     relNum,
                        int     pageNum,
                        int     pageSz,
                        boolean isLast)
   {
      // convert a negative row index into a positive relative line number 
      if (row < 0)
      {
         // a negative index is "added" to the page length to get the
         // proper index (which will be smaller than the page length)
         row = pageSz + row;
      }
      
      return (row == relNum);
   }
   
   /**
    * Obtain a descriptive string representation of this instance.
    *
    * @return   The details of this instance.
    */
   public String toString()
   {
      return String.format("each page: row %d (columns %d - %d)",
                           row,
                           start,
                           end);
   }
}
