/**
 * This class exists to demonstrate how to access the guts of a class
 * _without_ using an inner class.
 *
 * This is basically a wrapper around a fixed size int array that only allows
 * insertion of non-negative integers.
 *
 * @author <a href="mailto:terran@cs.unm.edu">Terran Lane</a>
 * @version 1.0
 */

import java.util.Iterator;

public class TableA implements TDRLTable {
  /**
   * Creates a <code>TableA</code> instance of the specified size.
   *
   * @param sz Table size to create.  Must be non-negative.
   * @throws IllegalArgumentException If <code>sz<=0</code>
   */
  public TableA(int sz) {
    if (sz<=0) {
      throw new IllegalArgumentException("Size must be >0");
    }
    _table=new int[sz];
  }

  public int size() { return _table.length; }

  public void put(int item, int idx) {
    if (idx<0 || idx>=size()) {
      throw new IllegalArgumentException("Idx must be in range " +
					 "0<=idx<size()");
    }
    if (item<0) {
      throw new IllegalArgumentException("TableA only stores " +
					 "non-negative integers");
    }
    _table[idx]=item;
  }

  public int get(int idx) {
    if (idx<0 || idx>=size()) {
      throw new IllegalArgumentException("Idx must be in range " +
					 "0<=idx<size()");
    }
    return _table[idx];
  }

  public Iterator getIterator() {
    return new TableAIterator(this);
  }

  /* ******************** end of public interface ******************** */

  /** The actual data store */
  private int[] _table;
}
