// file:    Incremental.java
// author:  Robert Keller
// purpose: Class Incremental of poly package

package polya;

public class Incremental extends Polylist
  {
  Object value;

  public Object first()
    {
    ensureGrown();
    return ((Polylist)value).first();
    }

  public Polylist rest()
    {
    ensureGrown();
    return ((Polylist)value).rest();
    }

  public boolean isEmpty()
    {
    ensureGrown();
    return ((Polylist)value).isEmpty();
    }

  public boolean nonEmpty()
    {
    ensureGrown();
    return ((Polylist)value).nonEmpty();
    }

  public String toString()
    {
    if( value instanceof Growable )
      return "...";
    else
      return ((Polylist)value).toString();
    }

  public Incremental(Growable growable)
    {
    value = growable;
    }

  public void ensureGrown()
    {
    while( value instanceof Growable )
      {
      value = ((Growable)value).grow();
      }
    }

  // use with caution!

  public boolean grown()
    {
    return !(value instanceof Growable);
    }

  public Polylist getList()
    {
    ensureGrown();
    return (Polylist)value;
    }
  }
