/**
 * IndexItem
 *
 * Created 11/28/06
 * Last modified 11/28/06
 */

package lab14;

import java.util.*;

/**
 * This is the data type of each element stored in a {@link WordIndex}.
 * As a subclass of {@link Item}, an IndexItem stores a "word" (of type 
 * String) and an occurrence count for that word. In addition, it includes 
 * a <B>java.util.Queue</B> of line numbers where this word appears in a 
 * textfile.
 * The {@link java.util.Queue} implementation used here is just 
 * a {@link java.util.LinkedList}.
 */
public class IndexItem extends Item implements Comparable<IndexItem> {

        private Queue<Integer> lineNumbers;

    /**
     * Creates a new {@link IndexItem} object containing the specified
     * word, and stores the associated lineNumber where that word was
     * found as the first value in an internal Queue of line numbers.
     * @param word A word found in the process of scanning a
     * textfile. This should be the first occurrence of this
     * word in the file.
     * @param lineNumber The line number in the textfile where this
     * word was first found.
     */
    public IndexItem (String word, int lineNumber) {
  	super (word);
  	lineNumbers = new LinkedList<Integer> ();
        lineNumbers.add (new Integer (lineNumber));
    } 

    /**
     * Adds the specified line number to the internal <B>Queue</B>.
     * This method should be called for each <I>repeated</I>
     * occurrence of the word in the textfile being scanned.
     * @param lineNumber Line number for this
     * occurrence of the word in the textfile. 
     */
    public void addLineNumber (int lineNumber) {
        lineNumbers.add (new Integer (lineNumber));
    }

    /**
     * Adds the specified line number (wrapped as an Integer object)
     * to the internal <B>Queue</B>.
     * This method should be called for each <I>repeated</I>
     * occurrence of the word in the textfile being scanned.
     * @param lineNumber Line number for this
     * occurrence of the word in the textfile, wrapped as an
     * Integer object. 
     */
    public void addLineNumber (Integer lineNumber) {
        lineNumbers.add (lineNumber);
    }

    /**
     * Returns a reference to the Queue of line numbers for the word
     * contained in this IndexItem. This list contains one entry
     * for each occurrence of this word found while scanning a textfile.
     * @return Queue of line numbers.
     */
    public Queue<Integer> getLineNumbers () {
        return lineNumbers;
    }

    /**
     * Overrides <B>Item</B> version of <B>toString()</B>.
     * Prints word, with occurrence count in parentheses, followed
     * by a list of line numbers locating each occurrence in the text.
     */
    public String toString () {
        String prefix = super.toString ();
        return prefix + ":\t" + lineNumbers.toString();
    }
    
    /**
     * Compares the word contained in this item to the word in another
     * IndexItem. Returns the same results that a direct comparison
     * of the words (of type String) would yield. Throws a
     * ClassCastException if the object parameter is not an IndexItem.
     * @return The result of comparing the words (0 if identical,
     * negative if this object is lexicographically less than the
     * parameter, positive if greater).
     */
    public int compareTo (IndexItem otherIndexItem) {
        String thisValue = this.getValue();
        String otherValue = otherIndexItem.getValue();
        return thisValue.compareTo (otherValue);
    }

    public boolean equals (Object otherObject) {
        if (!(otherObject instanceof IndexItem)) {
            return false;
        }
        String thisValue = this.getValue();
        String otherValue = ((IndexItem) otherObject).getValue();
        return thisValue.equals (otherValue);        
    }
}
