// $Id: ParsingTable.java,v 1.5 2004/03/05 17:05:44 m31 Exp $
//
// This file is part of the program suite "Principia Mathematica II"
// which is a working prototype for the main project:
// "Hilbert II" - http://www.qedeq.org
//
// Copyright 2001-2004  Michael Meyling <mime@qedeq.org>.
//
// "Principia Mathematica II" is free software; you can redistribute
// it and/or modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

package com.meyling.principia.io;


import java.util.HashMap;
import java.util.Map;

import com.meyling.principia.argument.Argument;


/**
 * Table for {@link Argument} {@link Position}s.
 *
 * @version $Revision: 1.5 $
 * @author  Michael Meyling
 */
 public class ParsingTable {


    /** table which maps an argument to its position */
    private final Map table = new HashMap();



    /**
     * Constructs a new parsing table.
     */
    public ParsingTable() {
    }


    /**
     * Get position of argument.
     *
     * @param   argument    lookup information for this argument
     * @return  position information
     * @throws  IllegalArgumentException if argument is unknown
     */
    public final Position get(final Argument argument) {
        final ArgumentWrapper aw = new ArgumentWrapper(argument);
        if (!this.table.containsKey(aw)) {
            throw new IllegalArgumentException("TODO");
        }
        final Position position = (Position) this.table.get(aw);
        return position;
    }


    /**
     * Put argument position.
     *
     * @param   argument    lookup information for this argument
     * @param   position    <code>argument</code> has this position
     * @throws  IllegalArgumentException if argument was already there
     */
    public final void put(final Argument argument,
            final Position position) {
        final ArgumentWrapper aw = new ArgumentWrapper(argument);
        if (this.table.containsKey(aw)) {
            throw new IllegalArgumentException("TODO");
        }
        this.table.put(aw, position);
    }



    /**
     * Wrapper for Arguments.
     *
     * @version $Revision: 1.5 $
     * @author  Michael Meyling
     */
     private static class ArgumentWrapper {

        /** argument */
        private final Argument argument;


        /**
         * Constructs an marker for begin and end position.
         *
         * @param argument to remember
         */
        private ArgumentWrapper(final Argument argument) {
            this.argument = argument;
        }


        /**
         * Get wrapped argument.
         *
         * @return  start position
         */
        private final Argument getArgument() {
            return this.argument;
        }


        /**
         * Get hash code for this object.
         *
         * @return  hash code of {@link #toString}
         */
        public final int hashCode() {
            return this.argument.hashCode();
        }


        /**
         * Has object same {@link #getArgument}?
         *
         * @param   object  compare with this object
         * @return  are the arguments of this instance and <code>object</code>
         *          the same?
         */
        public final boolean equals(final Object object) {
            if (object == null || !(object instanceof ArgumentWrapper)) {
                return false;
            }
            return this.argument == ((ArgumentWrapper) object).argument;
        }


        /**
         * Get the argument in <code>String</code> form.
         *
         * @return  readable argument
         */
        public  final String toString() {
            return argument.toString();
        }

    }


 }
