/* $Id: SubsectionListBo.java,v 1.3 2006/10/20 20:23:00 m31 Exp $
 *
 * This file is part of the project "Hilbert II" - http://www.qedeq.org
 *
 * Copyright 2000-2006,  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.SubsectionList;
import org.qedeq.kernel.base.module.SubsectionType;
import org.qedeq.kernel.utility.EqualsUtility;


/**
 * List of nodes. In LaTeX terms a node is something like an "subsection".
 *
 * @version $Revision: 1.3 $
 * @author  Michael Meyling
 */
public final class SubsectionListBo implements SubsectionList {

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

    /**
     * Constructs an empty node list.
     */
    public SubsectionListBo() {
        this.list = new ArrayList();

    }

    /**
     * Add subsection to this list.
     *
     * @param   node    Subsection or node to add.
     */
    public final void add(final SubsectionType node) {
        list.add(node);
    }

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

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

    public boolean equals(final Object obj) {
        if (!(obj instanceof SubsectionListBo)) {
            return false;
        }
        final SubsectionListBo otherList = (SubsectionListBo) 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("List of nodes:\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();
    }

}

