/* $Id: LatexListBo.java,v 1.6 2005/08/19 04:12:52 m31 Exp $
 *
 * This file is part of the project "Hilbert II" - http://www.qedeq.org
 *
 * Copyright 2000-2005,  Michael Meyling <mime@qedeq.org>.
 *
 * "Hilbert 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 org.qedeq.kernel.bo.module;

import java.util.ArrayList;
import java.util.List;

import org.qedeq.kernel.base.module.Latex;
import org.qedeq.kernel.base.module.LatexList;
import org.qedeq.kernel.utility.EqualsUtility;


/**
 * List of LaTeX text parts.
 *
 * @version $Revision: 1.6 $
 * @author  Michael Meyling
 */
public final class LatexListBo implements LatexList {

    /** Contains all list elements. */
    private final List list;

    /**
     * Constructs an empty list of LaTeX text parts.
     */
    public LatexListBo() {
        this.list = new ArrayList();
    }

    /**
     * Add LaTeX text to this list.
     *
     * @param   latex   Text to add.
     * @throws  NullPointerListEntryException   Tried to add NullPointer.
     * @throws  DuplicateLanguageEntryException Language entry already exists.
     */
    public final void add(final Latex latex) throws NullPointerListEntryException,
            DuplicateLanguageEntryException   {
        if (latex == null) {
            throw new NullPointerListEntryException(1000, "Null pointer not permitted.");
        }
        for (int i = 0; i < list.size(); i++) {
            if (((Latex) list.get(i)).getLanguage().equals(latex.getLanguage())) {
                throw new DuplicateLanguageEntryException(1001,
                        "Language entry exists already", i);
            }
        }
        list.add(latex);
    }

    public final int size() {
        return list.size();
    }

    public final Latex get(final int index) {
        return (Latex) list.get(index);
    }

    public boolean equals(final Object obj) {
        if (!(obj instanceof LatexListBo)) {
            return false;
        }
        final LatexListBo otherList = (LatexListBo) obj;
        if (size() != otherList.size()) {
            return false;
        }
        for (int i = 0; i < size(); i++) {
            if (!EqualsUtility.equals(get(i), otherList.get(i))) {
                return false;
            }
        }
        return true;
    }

    public int hashCode() {
        int hash = 0;
        for (int i = 0; i < size(); i++) {
            hash = hash ^ (i + 1);
            if (get(i) != null) {
                hash = hash ^ get(i).hashCode();
            }
        }
        return hash;
    }

    public String toString() {
        final StringBuffer buffer = new StringBuffer("Latex:\n");
        for (int i = 0; i < size(); i++) {
            if (i != 0) {
                buffer.append("\n");
            }
            buffer.append((i + 1) + ":\t");
            buffer.append(get(i) != null ? get(i).toString() : null);
        }
        return buffer.toString();
    }

}
