// $Id: UsedbyList.java,v 1.5 2004/02/27 16:30:00 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.module;


import com.meyling.principia.argument.AbstractDynamicArgumentList;
import com.meyling.principia.argument.Argument;
import com.meyling.principia.argument.ArgumentException;


/**
 * List of known references to a module.
 *
 * @version $Revision: 1.5 $
 * @author  Michael Meyling
 */
public class UsedbyList
        extends AbstractDynamicArgumentList implements Argument {


    /**
     * Constructs a list of locations.
     *
     * @param   arguments     list of locations
     * @throws  ArgumentException    if there not an argument at all
     *          or there is any argument
     *          that is not an instance of {@link Specification}
     */
    public UsedbyList(final Argument[] arguments)
            throws ArgumentException {
        super(arguments);
        if (arguments.length == 0) {
            throw new ArgumentException(
                ModuleConstants.CODE_CONSTRUCTOR_WRONG_ARGUMENT_NUMBER,
                ModuleCreator.getName(Specification.class)
                + ModuleConstants.AT_LEAST_ONE_ARGUMENT_EXPECTED);
        }
        for (int i = 0; i < arguments.length; i++) {
            if (!(arguments[i] instanceof Specification)) {
                throw new ArgumentException(
                    ModuleConstants.CODE_CONSTRUCTOR_WRONG_ARGUMENT_TYPE,
                    ModuleCreator.getName(Specification.class)
                    + ModuleConstants.EXPECTED_IN_ARGUMENT
                    + (i + 1),
                    arguments[i]);
            }
        }

    }


    /** @inherited */
    public final Argument create(final Argument[] arguments)
            throws ArgumentException {
        return new UsedbyList(arguments);
    }


    /** @inherited */
    public final String toString() {
        StringBuffer buffer = new StringBuffer("used by: \n");
        for (int i = 0; i < getArgumentSize(); i++) {
            if (i != 0) {
                buffer.append("\n");
            };
            buffer.append(i + ":\t");
            buffer.append(getArgument(i));
        }
        return buffer.toString();
    }


}
