package data.ooimpl;

import java.util.*;

import data.DataBasket;

import data.events.StockChangeEvent;

/**
  * StockChangeEvent that desribes changes in a CountingStock's contents.
  *
  * @author Steffen Zschaler
  * @version 2.0 19/08/1999
  * @since v2.0
  */
class CountingStockChangeEvent extends StockChangeEvent {

  /**
    * The number of affected items.
    */
  private int m_nCount;

  /**
    * The affected key.
    */
  private String m_sKey;

  /**
    * Create a new CountingStockChangeEvent.
    *
    * @param csiSource the Stock that triggers the event.
    * @param dbBasket the DataBasket that was used to perform the operation.
    * @param sKey the affected key.
    * @param nCount the number of affected items.
    */
  public CountingStockChangeEvent (CountingStockImpl csiSource,
                                   DataBasket dbBasket,
                                   String sKey,
                                   int nCount) {
    super (csiSource, dbBasket);

    m_sKey = sKey;
    m_nCount = nCount;
  }

  /**
    * Get the affected key.
    *
    * @override Never
    */
  public String getAffectedKey() {
    return m_sKey;
  }

  /**
    * Get the number of affected items.
    *
    * @override Never
    */
  public int countAffectedItems() {
    return m_nCount;
  }

  /**
    * Get the affected items.
    *
    * @override Never
    */
  public Iterator getAffectedItems() {
    return new Iterator() {
      private int m_nRemaining = m_nCount;

      public boolean hasNext() {
        return (m_nRemaining > 0);
      }

      public Object next() {
        if ((m_nRemaining--) <= 0) {
          throw new NoSuchElementException();
        }

        return new StockItemImpl (m_sKey);
      }

      public void remove() {
        throw new UnsupportedOperationException();
      }
    };
  }
}