package logo.lang;

//This is the super class for VarContext &
//SubTable. It is used to quickly look up a
//variable or subroutine definiton, or to
//iteratively dump all definitions.

import java.util.Hashtable;
import java.util.Enumeration;

public class Association {

Hashtable ht = new Hashtable();
Enumeration names=null;

   public Pair lookup(String name) {
      return (Pair) ht.get(name);
   }

   public Pair getFirst() { //returns the 1st LogoVar in the
                               //table; used to dump all variables
      names=ht.elements();
      return getNext();
   }

   public Pair getNext() {
      Pair result=null;
      if (names!=null) {
        if ( names.hasMoreElements() ) {
          result=(Pair)names.nextElement();
        }
      }
      return result;
   }

   public void setObject(Pair newPair) {
      //try to find the variable in the table:
      Pair oldPair = lookup(newPair.getName()); //Do we really need a new Pair?
      if (oldPair==null) { //Yes, we need a new Pair.
           ht.put(newPair.getName(),newPair);
      } else { //No, we need to change the value of the old Pair.
         oldPair.setObject(newPair.getObject());
      }
   }

   public Object getObject(String name) {
      Object result = null;
      Pair p = lookup(name);
      if (p!=null) {
         result=p.getObject();
      }
      return result;
   }


}

