/*
 * A Prolog-like list object in Java
 * To be used in code generated by 
 * the Popeye system.
 */

import java.util.Enumeration;
import java.util.Vector;

public class PopList implements Cloneable{

    protected Vector vBody;

    /*
     * Constructor
     */
    public PopList(){
	vBody = new Vector();
    }

    /*
     * Returns the head of the list
     */
    public Object pophead(){
	return vBody.firstElement();
    }
    
    /*
     * Returns a new PopList with the head stripped
     * off.
     */
    public void poptail(PopList lstTail){
	lstTail.vBody = (Vector)vBody.clone();
	lstTail.vBody.removeElementAt(0);
    }

    /*
     * Returns true if the list is empty,
     * false, otherwise.
     */
    public boolean popisnil(){
	if (vBody.isEmpty()){
	    return true;
	}	
	return false;
    }
    
    /*
     * Sticks a new element at the beginning
     * of the list.
     */
    public void popbuild( Object o ){
	vBody.insertElementAt(o, 0);
    }

    /* 
     * Appends a list at the end of the current list
     * i.e. : a.append(b) = a @ b
     */
    public void popappend( PopList lstOther ){
	PopList lstThisClone = (PopList)this.clone();
	PopList lstOtherClone = (PopList)lstOther.clone();
	
	lstThisClone.popreverse();
	Enumeration eThisClone = lstThisClone.vBody.elements();
	Object o;

	while (eThisClone.hasMoreElements()){
	    o = eThisClone.nextElement();
	    lstOtherClone.popbuild(o);
	} 
	
	vBody = lstOtherClone.vBody;
    }

    /* 
     * Deletes all occurences of an object
     */
    public void popdelete( Object o ){
	while (vBody.contains(o)){
	    vBody.removeElement(o);
	}
    }

    /* 
     * Reverses the current list
     */
    public void popreverse(){
	Vector vInverted = new Vector();
	Enumeration eBody = vBody.elements();
	Object o;

	while (eBody.hasMoreElements()){
	    o = eBody.nextElement();
	    vInverted.insertElementAt(o,0);
	}
	vBody = vInverted;
    }

    public synchronized Object clone(){
	PopList lstClone = new PopList();
	lstClone.vBody = (Vector)vBody.clone();
	return lstClone;
    }
    
    public final String toString(){
	String s = vBody.toString();
	return s;
    }	

}


