home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 21 / IOPROG_21.ISO / SOFT / JSL.ZIP / JSL20 / simula / simset / Link.java < prev    next >
Encoding:
Java Source  |  1998-02-20  |  4.9 KB  |  166 lines

  1.  
  2. package simula.simset;
  3.  
  4. import simula.SimulaException;
  5.  
  6. /**
  7.  * This class implements the elementary object in a double linked list.
  8.  *
  9.  * @see Linkage
  10.  * @see Head
  11.  */
  12. public class Link extends Linkage
  13. {
  14.     //---------------------------- VARIABLES -------------------------
  15.     /**
  16.      * This is a reference to the list which this object belongs to.
  17.      */
  18.     protected Head list;
  19.  
  20.  
  21.     //---------------------------- CONSTRUCTOR -------------------------
  22.     /**
  23.      * The new object doesn't belong to any list.
  24.      */
  25.     public Link() {
  26.         list = null;
  27.     }
  28.     /**
  29.      * This has a similar action to <a href=#precede>precede</a>, except that
  30.      * this method inserts this object after the parameter if this is a
  31.      * <code>Link</code> object, otherwise places it as the first object in
  32.      * the given list.
  33.      *
  34.      * @param _elem the element to be followed by this object.
  35.      * @exception SimulaException The argument is <code>Null</code> or
  36.      *                belongs to a <code>Null</code> list.
  37.      * @see Head
  38.      */
  39.     public void follow(Linkage _elem) throws SimulaException {
  40.         if (_elem instanceof Head) {
  41.             this.precede(((Head) _elem).first());
  42.         } else {
  43.             if (_elem == null || ((Link) _elem).list() == null) {
  44.                 throw new SimulaException(SimulaException.LINK_TO_NULL);
  45.             } else {
  46.                 // Make this object belong only to the same list as its successor
  47.                 this.out();
  48.                 list = ((Link) _elem).list();
  49.  
  50.                 // Insert this object between the given element and his successor
  51.                 // Set links to and from successor
  52.                 suc = _elem.suc();
  53.                 if (suc == null) {
  54.                     // This element becomes the last of the list ( but not the least)
  55.                     list.last(this);
  56.                 } else {
  57.                     suc.pred(this);
  58.                 }
  59.  
  60.                 // Set links to and from predecessor
  61.                 pred = _elem;
  62.                 _elem.suc(this);
  63.             }
  64.         }
  65.     }
  66.     /**
  67.      * If <code>_list</code> refers to an existing linked list, this method
  68.      * appends the object at the end of that list; otherwise it is removed
  69.      * from any previous list it might have belonged to.
  70.      *
  71.      * @param _list the list where this object will be appended.
  72.      * @exception SimulaException An error happened attempting to append this
  73.      *            object to the list.
  74.      *    @see follow
  75.      */
  76.     public void into(Head _list) throws SimulaException {
  77.         // Extract from current list
  78.         this.out();
  79.         if (_list != null) {
  80.             if (_list.empty()) {
  81.                 // Initialize list to contain this only object
  82.                 list = _list;
  83.                 _list.first(this);
  84.                 _list.last(this);
  85.             } else {
  86.                 // Append this object to the list
  87.                 this.follow(_list.last());
  88.             }
  89.         }
  90.     }
  91.     //---------------------------- METHODS -------------------------
  92.     /**
  93.      * Returns the list which this object belongs to.
  94.      * @return the list which this object belongs to.
  95.      */
  96.     protected Head list() { return list; }
  97.     /**
  98.      * If this object belongs to a linked list (it cannot belong to more than
  99.      * one list), this method removes it from that list; otherwise it has no
  100.      * effect.
  101.      */
  102.     public void out() {
  103.         if (list != null) {
  104.             if (list.first() == this) { //This was the first of the queue
  105.                 list.first((Link) suc);
  106.             }
  107.             if (list.last() == this) {  //This was the last of the queue
  108.                 list.last((Link) pred);
  109.             }
  110.             list = null;
  111.         }
  112.  
  113.         // Link predecessor with successor
  114.         if (pred != null) {
  115.             pred.suc(suc);
  116.         }
  117.         if (suc != null) {
  118.             suc.pred(pred);
  119.         }
  120.         pred = null;
  121.         suc = null;
  122.     }
  123.     /**
  124.      * The actual parameter of this procedure may be either a <code>Link</code>
  125.      * object or a <code>Head</code> object: if <code>_elem</code> is a 
  126.      * <code>Link</code> object, this method inserts this object into the same
  127.      * linked list that <code>_elem</code> belongs to, in the position
  128.      * immediately preceding <code>_elem</code> (if <code>_elem</code> does not
  129.      * belong to a linked list, the result is the same as for
  130.      * <a href=#out>out</a>; if <code>_elem</code> is a <code>Head</code>
  131.      * object, this method is equivalent to <a href=#into>into</a>.
  132.      *
  133.      * @param _elem the element to be preceded by this object.
  134.      * @exception SimulaException The argument is <code>Null</code> or
  135.      *                belongs to a <code>Null</code> list.
  136.      * @see Head
  137.      */
  138.     public void precede(Linkage _elem) throws SimulaException {
  139.         if (_elem instanceof Head) {
  140.             // Preceding a list is equal to append to that list
  141.             this.into((Head) _elem);
  142.         } else {
  143.             if (_elem == null || ((Link) _elem).list() == null) {
  144.                 throw new SimulaException(SimulaException.LINK_TO_NULL);
  145.             } else {
  146.                 // Make this object belong only to the same list as its successor
  147.                 this.out();
  148.                 list = ((Link) _elem).list();
  149.  
  150.                 // Insert this object between the given element and his predecessor
  151.                 // Set links to and from predecessor
  152.                 pred = _elem.pred();
  153.                 if (pred == null) {
  154.                     // This becomes the first in its list
  155.                     list.first(this);
  156.                 } else {
  157.                     pred.suc(this);
  158.                 }
  159.  
  160.                 // Set links to and from successor
  161.                 suc = _elem;
  162.                 _elem.pred(this);
  163.             }
  164.         }
  165.     }
  166. } // end of class Link