/**
 * IndexItem
 *
 * File Revision Date: 04/21/04
 */

package lab12;

import lab07.Queue;
import lab09.Item;

/**
 * This is the data type stored in each node of a {@link WordIndex},
 * which is itself a subclass BinaryTree
 * (ordered binary tree). This specialized item stores a "word"
 * (of type String) and an occurrence count for the word. In addition,
 * it includes a queue of line numbers where this word has been found
 * while scanning a textfile.
 * The queue is provided by the class edu.unca.cs.lists.Queue.
 */
 
public class IndexItem extends Item implements Comparable {

        private Queue 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 Queue ();
        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 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();
//        String blanks = "";
//        for (int n = 0; n < 20 - prefix.length(); n++)
//            blanks += " ";
//        return prefix + ":" + blanks + 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 (Object obj) {
        if (!(obj instanceof IndexItem))
            throw new ClassCastException();
        
        String thisValue = getValue();
        String otherValue = ((IndexItem) obj).getValue();
        return thisValue.compareTo (otherValue);
    }
}
