package prolog.util;

import java.util.*;
import prolog.model.*;
import prolog.*;
import prolog.implementation.*;

public class StringBuilder
{
	private StringBuilder()
	{
	}

	public static String build( Iterator i, String separator )
	{
		StringBuffer out = new StringBuffer();
		while( i.hasNext() )
		{
			out.append( i.next().toString() );
			if( i.hasNext() )
				out.append( separator );
		}
		return out.toString();
	}

	public static String buildSymbolDecoded( Iterator i, String separator, ISymbolTable table )
	{
		StringBuffer out = new StringBuffer();
		while( i.hasNext() )
		{
			Object next = i.next();
			if( next instanceof Value )
			{
				if( table != null )
					out.append( table.decode( ((Value)next).getName() ) );
				else
					out.append( ((Integer)next).intValue() );
			}
			else
			{
				if( table != null )
					out.append( table.decode( ((Variable)next).getName() ) );
				else
					out.append( ((Variable)next).getName() );
			}
			if( i.hasNext() )
				out.append( separator );
		}
		return out.toString();
	}

	public static String buildTabs( int len )
	{
		StringBuffer back = new StringBuffer();
		for( int i=0;i<len;i++ )
			back.append( "\t" );
		return back.toString();
	}
}
