/* $Id$
 */
package calc;

import java.util.HashMap;

/** Map from formula names to formulas.
 * @author Gary T. Leavens
 */
public class FormulaMap {
	
	/** The mapping from formula names to formulas. */
    private HashMap map = new HashMap();
    
	/* prevent others from instantiating this type */
	private FormulaMap() {}
    
	/** The only instance of this type. */
	private static FormulaMap instance = null;

	/** Return the only instance of this type */
	public synchronized static FormulaMap getInstance() {
		if (instance == null) {
			instance = new FormulaMap();
		}
		return instance;
	}

    /** Add the association of the name to the formula,
     * replacing the old value if any.
     * @param name the formula's name
     * @param formula the formula
     */
    //@ requires name != null && formula != null;
    public 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 FormulaType get(String name) {
        Object o = map.get(name);
        if (o == null) {
            throw new IllegalArgumentException("Unknown formula name: " + name);
        } else {
            return (FormulaType) o;
        }
    }
}
