package com.srbenoit.modeling.grid;

/**
 * A base class for objects that may be contained in a 2-dimensional grid. Each object has an X and
 * Y position, a radius, and a grid address.
 */
public class LinkedListGridMember2D extends DynamicGridMember2D {

    /** the next element in the list */
    private LinkedListGridMember2D flink;

    /** the prior element in the list */
    private LinkedListGridMember2D blink;

    /**
     * Constructs a new <code>LinkedListGridMember</code>.
     *
     * @param  xCoord       the X coordinate
     * @param  yCoord       the Y coordinate
     * @param  rad          the radius of the object
     * @param  theType      the element type
     * @param  structureId  the ID of the structure this member belongs to
     * @param  theMass      the mass of the object
     */
    public LinkedListGridMember2D(final double xCoord, final double yCoord, final double rad,
        final EnumElementType theType, final int structureId, final double theMass) {

        super(xCoord, yCoord, rad, theType, structureId, theMass);

        this.flink = this;
        this.blink = this;
    }

    /**
     * Adds this member in the linked list before a given element.
     *
     * @param  elem  the element before which to add this element
     */
    public void addBefore(final LinkedListGridMember2D elem) {

        this.setPrior(elem.getPrior());
        this.setNext(elem);
        elem.getPrior().setNext(this);
        elem.setPrior(this);
    }

    /**
     * Adds this member in the linked list after a given element.
     *
     * @param  elem  the element after which to add this element
     */
    public void addAfter(final LinkedListGridMember2D elem) {

        setNext(elem.getNext());
        setPrior(elem);
        elem.getNext().setPrior(this);
        elem.setNext(this);
    }

    /**
     * Removes this element from the list in which it is installed.
     */
    public void remove() {

        this.getNext().setPrior(getPrior());
        this.getPrior().setNext(getNext());
        setNext(this);
        setPrior(this);
    }

    /**
     * Gets the next item in the linked list.
     *
     * @return  the next item
     */
    public LinkedListGridMember2D getNext() {

        return this.flink;
    }

    /**
     * Gets the prior item in the linked list.
     *
     * @return  the prior item
     */
    public LinkedListGridMember2D getPrior() {

        return this.blink;
    }

    /**
     * Sets the next item in the linked list.
     *
     * @param  next  the next item
     */
    public void setNext(final LinkedListGridMember2D next) {

        this.flink = next;
    }

    /**
     * Sets the prior item in the linked list.
     *
     * @param  prior  the prior item
     */
    public void setPrior(final LinkedListGridMember2D prior) {

        this.blink = prior;
    }
}
