package utils;

import java.util.*;

// at some point, this class should be amended to use weak or soft
// references to the symbols, allowing garbage collection of unused
// symbols.
public class Symbol {
  static Map symtable;
  static boolean initialized = false;

  static void init() {
    if(initialized)
      return;
    symtable = Collections.synchronizedMap(new HashMap());
    initialized = true;
  }

  public static Symbol GetSymbol(String s) {
    init();

    if(symtable.containsKey(s))
      return (Symbol)symtable.get(s);
    else
      return new Symbol(s);
  }

  public int hashCode() {
    return name.hashCode();
  }

  public String toString() {
    return name;
  }

  public boolean equals(Object o) {
    if(o instanceof Symbol) return o==this;
    else if(o instanceof String) return name.equals(o);
    else return false;
  }

  private String name;
  private Symbol(String s) {
    name = s;
    symtable.put(s,this);
  }
}
