home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-02-20 | 4.9 KB | 166 lines |
-
- package simula.simset;
-
- import simula.SimulaException;
-
- /**
- * This class implements the elementary object in a double linked list.
- *
- * @see Linkage
- * @see Head
- */
- public class Link extends Linkage
- {
- //---------------------------- VARIABLES -------------------------
- /**
- * This is a reference to the list which this object belongs to.
- */
- protected Head list;
-
-
- //---------------------------- CONSTRUCTOR -------------------------
- /**
- * The new object doesn't belong to any list.
- */
- public Link() {
- list = null;
- }
- /**
- * This has a similar action to <a href=#precede>precede</a>, except that
- * this method inserts this object after the parameter if this is a
- * <code>Link</code> object, otherwise places it as the first object in
- * the given list.
- *
- * @param _elem the element to be followed by this object.
- * @exception SimulaException The argument is <code>Null</code> or
- * belongs to a <code>Null</code> list.
- * @see Head
- */
- public void follow(Linkage _elem) throws SimulaException {
- if (_elem instanceof Head) {
- this.precede(((Head) _elem).first());
- } else {
- if (_elem == null || ((Link) _elem).list() == null) {
- throw new SimulaException(SimulaException.LINK_TO_NULL);
- } else {
- // Make this object belong only to the same list as its successor
- this.out();
- list = ((Link) _elem).list();
-
- // Insert this object between the given element and his successor
- // Set links to and from successor
- suc = _elem.suc();
- if (suc == null) {
- // This element becomes the last of the list ( but not the least)
- list.last(this);
- } else {
- suc.pred(this);
- }
-
- // Set links to and from predecessor
- pred = _elem;
- _elem.suc(this);
- }
- }
- }
- /**
- * If <code>_list</code> refers to an existing linked list, this method
- * appends the object at the end of that list; otherwise it is removed
- * from any previous list it might have belonged to.
- *
- * @param _list the list where this object will be appended.
- * @exception SimulaException An error happened attempting to append this
- * object to the list.
- * @see follow
- */
- public void into(Head _list) throws SimulaException {
- // Extract from current list
- this.out();
- if (_list != null) {
- if (_list.empty()) {
- // Initialize list to contain this only object
- list = _list;
- _list.first(this);
- _list.last(this);
- } else {
- // Append this object to the list
- this.follow(_list.last());
- }
- }
- }
- //---------------------------- METHODS -------------------------
- /**
- * Returns the list which this object belongs to.
- * @return the list which this object belongs to.
- */
- protected Head list() { return list; }
- /**
- * If this object belongs to a linked list (it cannot belong to more than
- * one list), this method removes it from that list; otherwise it has no
- * effect.
- */
- public void out() {
- if (list != null) {
- if (list.first() == this) { //This was the first of the queue
- list.first((Link) suc);
- }
- if (list.last() == this) { //This was the last of the queue
- list.last((Link) pred);
- }
- list = null;
- }
-
- // Link predecessor with successor
- if (pred != null) {
- pred.suc(suc);
- }
- if (suc != null) {
- suc.pred(pred);
- }
- pred = null;
- suc = null;
- }
- /**
- * The actual parameter of this procedure may be either a <code>Link</code>
- * object or a <code>Head</code> object: if <code>_elem</code> is a
- * <code>Link</code> object, this method inserts this object into the same
- * linked list that <code>_elem</code> belongs to, in the position
- * immediately preceding <code>_elem</code> (if <code>_elem</code> does not
- * belong to a linked list, the result is the same as for
- * <a href=#out>out</a>; if <code>_elem</code> is a <code>Head</code>
- * object, this method is equivalent to <a href=#into>into</a>.
- *
- * @param _elem the element to be preceded by this object.
- * @exception SimulaException The argument is <code>Null</code> or
- * belongs to a <code>Null</code> list.
- * @see Head
- */
- public void precede(Linkage _elem) throws SimulaException {
- if (_elem instanceof Head) {
- // Preceding a list is equal to append to that list
- this.into((Head) _elem);
- } else {
- if (_elem == null || ((Link) _elem).list() == null) {
- throw new SimulaException(SimulaException.LINK_TO_NULL);
- } else {
- // Make this object belong only to the same list as its successor
- this.out();
- list = ((Link) _elem).list();
-
- // Insert this object between the given element and his predecessor
- // Set links to and from predecessor
- pred = _elem.pred();
- if (pred == null) {
- // This becomes the first in its list
- list.first(this);
- } else {
- pred.suc(this);
- }
-
- // Set links to and from successor
- suc = _elem;
- _elem.pred(this);
- }
- }
- }
- } // end of class Link