package osa.attribute.atgrammar.grammar;

import java.util.*;

import osa.attribute.atgrammar.grammar.symbols.*;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2005</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class SymbolsString
  implements Cloneable
  {
  ArrayList string=new ArrayList();

  protected int length=0;

  public SymbolsString()
    {
    }

  public void appendSymbol(Symbol symbol)
    {
    string.add(symbol);
    length++;
    }

  public void removeSymbolAt(int index)
    {
    string.remove(index);
    }

  public void insertStringAt(int index, SymbolsString str)
     {
     for(int i=0;i<str.length;i++)
        {
        insertSymbolAt(index+i,str.getSymbolAt(i));
        }
     }

  public void insertSymbolAt(int index, Symbol symbol)
    {
    string.add(index, symbol);
    length++;
    }

  public void deleteSymbolAt(int index)
    {
    string.remove(index);
    length--;
    }

  public Symbol getSymbolAt(int index)
    {
    Symbol return_value=null;
    if(index>=this.length)
      {
      return null;
      }
    return_value=(Symbol)string.get(index);
    return return_value;
    }

  public int getLength()
    {
    return string.size();
    }

  public String toString()
    {
    String result=new String();

    for(int i=0;i<this.length;i++)
       {
       result+=this.getSymbolAt(i).toString();
       }
    return result;
    }

  public Object clone()
    {
    SymbolsString cloned=new SymbolsString();
    cloned.length=length;
    Symbol temp_symbol;
    for(int i=0;i<length;i++)
       {
       temp_symbol=(Symbol)string.get(i);
       if(!temp_symbol.isDeleted())
         {
         cloned.string.add(temp_symbol.clone());
         }
       }
    return cloned;
    }

  public boolean deleteSymbol(Symbol symbol)
    {
    Symbol temp_symbol;
    boolean is_encountered=false;

    for(int i=0;i<length;i++)
       {
       temp_symbol=(Symbol)string.get(i);
       if(temp_symbol.getOriginalReference() == symbol.getOriginalReference())
         {
         is_encountered=true;
         this.deleteSymbolAt(i);
         i--;
         }
       }
    return is_encountered;
    }

  }