/*
 *  List of ALLL objects
 *
 *  2004 - 2011 Frantisek Zboril jr.
 *  Faculty of Information Technology
 *  zborilf@fit.vutbr.cz
 *  http://www.fit.vutbr.cz/~zborilf
 *
 * (C) Brno University of Technology
 * Freeware licence VUT v Brně
 * https://wis.fit.vutbr.cz/FIT/db/dir.php/dr/VZ2007/externiInformace/softwareNaWebu/Priloha2_rozh23.pdf

 */

package jTMass.core.jALLL.jTObjects;

import jTMass.TMass;
import java.util.LinkedList;
import jTMass.core.jAMass.TAgent;


public class TList extends TObject{
	
	protected LinkedList<TObject> PList;

	public void set(int index, TObject item){
		PList.set(index,item);
	}
	
	public int size(){
		return(PList.size());
	}
	
	public void append(TObject object){
		PList.addLast(object);
	}

        @Override
	public void setType(int type){
		if((type==TConstants._GeneralList)||(type==TConstants._QueryList)
				||(type==TConstants._Delete)||(type==TConstants._Add)
                                ||(type==TConstants._Plan_Direct_Script)
                                ||(type==TConstants._Plan_Indirect_Call)){
			super.setType(type);
		}
                else
                    TMass._Terminal.reportError("Wrong object type");
	}
	
	public boolean isEmpty(){
		return(PList.isEmpty());
	}
	
	public void add(TObject object){
		if(object!=null)
			PList.add(object);
	}
	
	
	public TObject get(int index){
		return((TObject)PList.get(index));
	}
	
	
	public void addFirst(TObject object){
		PList.addFirst((TObject)object);
	}
	
	
	public TObject popFirst(){
		TObject object=(TObject)PList.getFirst();
		PList.removeFirst();
		return(object);
	}
	
	
	public void removeFirst(){
		PList.removeFirst();
	}
	
	
	public void appendFirst(TList object){
		if(!object.isEmpty()){
			TObject myobject=object.popFirst();
			this.appendFirst(object);
			PList.addFirst(myobject);
		}
	}
	
	@Override
	public TObject cloneObject(){
		TObject clone;
		TList list=new TList(this.getType());
		for(int i=0;i<PList.size();i++){
			TObject source=PList.get(i);
			clone=source.cloneObject();
			list.add(clone);
		}
			
		return(list);
	}
	
	@Override
	public String getContent(){

            String listContent,listClosure;
            if(this instanceof TPlan)
                {listContent=new String("<");listClosure=new String(">");}
            else
            if (PType==TConstants._QueryList)
                {listContent=new String("[");listClosure=new String("]");}
            else
            if (PType==TConstants._GeneralList)
		{listContent=new String("(");listClosure=new String(")");}
            else {listContent=new String("(");listClosure=new String(")");}
		 
            for(int i=0;i<PList.size();i++){
                listContent+=((TObject)PList.get(i)).getContent();
		if ((i+1)<PList.size()) listContent+=",";
            }
		 
            listContent=listContent.concat(listClosure);
            return(listContent);
	}

        @Override
	public TObject substituteRegisters(TAgent agent){
		if(PList!=null)
			for(int i=0;i<PList.size();i++){
				TObject item=(TObject)PList.get(i);
				if(item!=null)
					PList.set(i,item.substituteRegisters(agent));
			}
		return(this);
	}

        @Override
	public void matureRegisters(){
		if(PList!=null)
			for(int i=0;i<PList.size();i++)
				PList.get(i).matureRegisters();
	}
	
	// ===== CONSTRUCTORS =====

	public TList(){
   		super(TConstants._GeneralList);
   		PList=new LinkedList<TObject>();
        }
   
	
	public TList(int type){
   		super(type);
   		PList=new LinkedList();
   }
		
}