package osa.attribute.atgrammar.grammar;

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

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: </p>
 * <p>Company: </p>
 * @author not attributable
 * @version
 */

public class CommonDictionary
  {
  protected TreeMap terminal_dictionary=new TreeMap();
  protected TreeMap non_terminal_dictionary=new TreeMap();
  protected TreeMap output_dictionary=new TreeMap();

  protected Grammar grammar_reference;

  public CommonDictionary(Grammar grammar_reference)
    {
    this.grammar_reference=grammar_reference;
    }

  // accessors
  public TerminalSymbol getTerminalSymbol(String symbol_name)
    {
    return (TerminalSymbol)terminal_dictionary.get(symbol_name);
    }

  public NonTerminalSymbol getNonTerminalSymbol(String symbol_name)
    {
    return (NonTerminalSymbol)non_terminal_dictionary.get(symbol_name);
    }

  public OutputSymbol getOutputSymbol(String symbol_name)
    {
    return (OutputSymbol)output_dictionary.get(symbol_name);
    }

  public Collection getTerminals()
    {
    return terminal_dictionary.values();
    }

  public Collection getNonTerminals()
    {
    return non_terminal_dictionary.values();
    }

  public Collection getOutputs()
    {
    return output_dictionary.values();
    }


  // mutators
  public void putSymbol(Symbol symbol)
    {
    String symbol_name=symbol.getOriginalName();
    switch(symbol.getType())
          {
          case Symbol.NON_TERMINAL_SYMBOL:
            terminal_dictionary.put(symbol_name,symbol);
            break;
          case Symbol.TERMINAL_SYMBOL:
            non_terminal_dictionary.put(symbol_name,symbol);
            break;
          case Symbol.OUTPUT_SYMBOL:
            output_dictionary.put(symbol_name,symbol);
            break;
          default:
            System.err.println("Error during putting symbol in dictionary!!!");
          }
    }


  public void putTerminalSymbol(TerminalSymbol symbol)
    {
    terminal_dictionary.put(symbol.getOriginalName(),symbol);
    }

  public void putNonTerminalSymbol(NonTerminalSymbol symbol)
    {
    non_terminal_dictionary.put(symbol.getOriginalName(),symbol);
    }

  public void putOutputSymbol(OutputSymbol symbol)
    {
    output_dictionary.put(symbol.getOriginalName(),symbol);
    }




  public void removeTerminalSymbol(String symbol_name)
    {
    terminal_dictionary.remove(symbol_name);
    }

  public void removeNonTerminalSymbol(String symbol_name)
    {
    non_terminal_dictionary.remove(symbol_name);
    }

  public void removeOutputSymbol(String symbol_name)
    {
    output_dictionary.remove(symbol_name);
    }

  public void resetAllCounters()
    {
    Iterator temp_iterator;
    temp_iterator=getNonTerminals().iterator();
    while(temp_iterator.hasNext())
         {
         ((Symbol)temp_iterator.next()).resetCounter();
         }
    temp_iterator=getTerminals().iterator();
    while(temp_iterator.hasNext())
         {
         ((Symbol)temp_iterator.next()).resetCounter();
         }
    temp_iterator=getOutputs().iterator();
    while(temp_iterator.hasNext())
         {
         ((Symbol)temp_iterator.next()).resetCounter();
         }
    }

  }