/**
  *
  *                      OOAS Compiler
  *
  *       Copyright 2015, AIT Austrian Institute of Technology.
  * This code is based on the C# Version of the OOAS Compiler, which is
  * copyright 2015 by the Institute of Software Technology, Graz University
  * of Technology with portions copyright by the AIT Austrian Institute of
  * Technology. All rights reserved.
  *
  * SEE THE "LICENSE" FILE FOR THE TERMS UNDER WHICH THIS FILE IS PROVIDED.
  *
  * If you modify the file please update the list of contributors below to in-
  * clude your name. Please also stick to the coding convention of using TABs
  * to do the basic (block-level) indentation and spaces for anything after
  * that. (Enable the display of special chars and it should be pretty obvious
  * what this means.) Also, remove all trailing whitespace.
  *
  * Contributors:
  *               Willibald Krenn (AIT)
  *               Stephan Zimmerer (AIT)
  *               Markus Demetz (AIT)
  *               Christoph Czurda (AIT)
  *
  */


package org.momut.ooas.parser;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map.Entry;

import org.momut.ooas.ast.identifiers.Identifier;
import org.momut.ooas.utils.OoasCompilerHashMap;

public class SymbolTable
{
	private OoasCompilerHashMap<String, Identifier> m_symbols;

	public List<Identifier> symbolList() { return new ArrayList<Identifier>(m_symbols.values());}

	public SymbolTable()
	{
		m_symbols = new OoasCompilerHashMap<String, Identifier>();
	}

	public SymbolTable(SymbolTable toCopy)
	{
		if (toCopy != null)
			m_symbols = new OoasCompilerHashMap<String, Identifier>(toCopy.m_symbols);
		else
			m_symbols = new OoasCompilerHashMap<String, Identifier>();
	}

	public boolean Defined(Identifier aValue)
	{
		return aValue == m_symbols.get(aValue.tokenText()); // ref comp!
	}

	public boolean Defined(String aKey)
	{
		return m_symbols.containsKey(aKey);
	}

	public Identifier Get(String aKey)
	{
		return m_symbols.get(aKey);
	}

	public void AddIdentifier(Identifier anIdent)
	{
		if (anIdent != null) {
			final Identifier oldValue = m_symbols.put(anIdent.tokenText(), anIdent);
			if (oldValue != null)
				throw new UnsupportedOperationException(
					String.format("Identifier '%s' already exists", anIdent.tokenText()));
		}
	}

	public void AddSymbolTable(SymbolTable otherTable)
	{
		for (final Entry<String,Identifier> x: otherTable.m_symbols.entrySet())
			if ( ! x.getKey().equals("self")) {
				final Identifier oldValue = m_symbols.put(x.getKey(), x.getValue());
				if (oldValue != null)
					throw new UnsupportedOperationException(
						String.format("Identifier '%s' already exists", x.getKey()));
			}
	}

	public void Replace(String aKey, Identifier anIdent)
	{
		if (anIdent != null)
		{
			m_symbols.remove(aKey);
			m_symbols.put(anIdent.tokenText(), anIdent);
		}
	}

	public void SetSymbolList(List<Identifier> newList)
	{
		Collections.sort(newList);
		m_symbols = new OoasCompilerHashMap<String, Identifier>();
		for (final Identifier x : newList)
			AddIdentifier(x);
	}

	public void Sort()
	{
		final List<Identifier> newList = new ArrayList<Identifier>(m_symbols.values());
		Collections.sort(newList);
		m_symbols = new OoasCompilerHashMap<String, Identifier>();
		for (final Identifier x : newList)
			AddIdentifier(x);
	}

	public int size() {
		return m_symbols.size();
	}
}
