package calc;

import java.util.HashMap;

/** The map from formula names to formulas.
 * Note that there is only one such map in the program.
 * @author Gary T. Leavens
 */
public class FormulaMap {
    
    /** The mapping from names to formulas. */
	private static HashMap map = new HashMap();
	
    /** Add the association of the name to the formula, 
     * replacing the old value if any.
     * @param name, the formula's name.
     * @param f1, the formula.
     */
    //@ requires name != null && formula != null;
	public static void put(String name, FormulaType formula) {
		map.put(name, formula);
	}

    /** Retrieve the formula associated with the given name.
     * @param name the formula's name
     */
    //@ requires name != null;
    //@ ensures \result != null;
	public /*@ pure @*/ static FormulaType get(String name) {
		Object o = map.get(name);
		if (o == null) {
			throw new IllegalArgumentException(
				"Unknown formula name: " + name);
		} else {
			return (FormulaType) o;
		}
	}
	
    /** Is the given name a defined name for in the formula map?
     * @param name, the name sought in the map.
     */
    //@ requires name != null;
	public /*@ pure @*/ static boolean isDefinedAt(String name) {
		return map.containsKey(name);
	}
}
