home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / SOURCE.BIN / MatrixEnumeration.java < prev    next >
Encoding:
Java Source  |  1997-06-19  |  4.2 KB  |  210 lines

  1. package symantec.itools.awt;
  2.  
  3.  
  4. import java.util.Enumeration;
  5.  
  6.  
  7. //     01/30/97    RKM    Integrated fix to hasMoreElements from flash@tansu.slab.tnr.sharp.co.jp (George Rome Borden IV)
  8. //     03/27/97    RKM    Made class public, so peole could use it
  9. //                    Made class final
  10. //                    Tried to make some sense of protection on methods & members
  11. //     04/17/97    LAB Added Don Haney's (haney@tiac.net) fix that addressed a NullPointerException in hasMoreElements().
  12.  
  13. /**
  14.  * This class is used to enumerate Matrix objects. Enumeration is used to
  15.  * sequentially scan through all the elements in the Matrix.
  16.  * @see symantec.itools.awt.Matrix
  17.  * @see java.util.Enumeration
  18.  *
  19.  * @version 1.0, Nov 26, 1996
  20.  *
  21.  * @author    Symantec
  22.  *
  23.  */
  24.  
  25. public final class MatrixEnumeration
  26.     implements Enumeration
  27. {
  28.     //--------------------------------------------------
  29.     // public constructors
  30.     //--------------------------------------------------
  31.  
  32.     /**
  33.      * Constructs a matrix enumerator.
  34.      */
  35.     public MatrixEnumeration(Matrix matrix)
  36.     {
  37.         m = matrix;
  38.         started = false;
  39.     }
  40.     
  41.     //--------------------------------------------------
  42.     // public members
  43.     //--------------------------------------------------
  44.     
  45.     /**    
  46.      * Returns true if more elements are available to enumerate.
  47.      * A standard Java Enumeration interface method.
  48.      * @return true if more elements available, false if done enumerating
  49.      */
  50.     public boolean hasMoreElements()
  51.     {
  52.         if (m == null)
  53.             return false;
  54.         if (started == false && m.o != null) return true;
  55.         if (m.nextElt != null) return true;
  56.         return hasMoreRows();
  57.     }
  58.  
  59.     /**
  60.      * Returns true if more rows are available to enumerate.
  61.      */
  62.     public boolean hasMoreRows()
  63.     {
  64.         if (m.nextRow == null)
  65.         {
  66.             return false;
  67.         }
  68.     
  69.         Matrix n = m.nextRow;
  70.     
  71.         //find next row
  72.         while(n.o == null && n.nextElt == null)
  73.         {
  74.             if ((n = n.nextRow) == null)  return false;
  75.         }
  76.     
  77.         return true;
  78.     }
  79.  
  80.     /**
  81.      * Returns the next element.
  82.      * A standard Java Enumeration interface method.
  83.      */
  84.     public Object nextElement()
  85.     {
  86.         if (!started)
  87.         {
  88.             started = true;
  89.     
  90.             if (m.o != null)
  91.             {
  92.                 return m.o;
  93.             }
  94.         }
  95.     
  96.         if (m == null)
  97.         {
  98.             return null;
  99.         }
  100.     
  101.         if (m.nextElt != null)
  102.         {
  103.             m = m.nextElt;
  104.             return m.o;
  105.         }
  106.     
  107.         if ((m = findNextRow()) == null)
  108.         {
  109.             return null;
  110.         }
  111.     
  112.         return m.o;
  113.     }
  114.  
  115.     /**
  116.      * Returns the row number of the current element.
  117.      */
  118.     public int currRow()
  119.     {
  120.         return m.row;
  121.     }
  122.     
  123.     /**
  124.      * Returns the column number of the current element.
  125.      */
  126.     public int currCol()
  127.     {
  128.         return m.col;
  129.     }
  130.     
  131.     /**
  132.      * Skips enumeration ahead to the start of the next row.
  133.      * @return the first element in the next row.
  134.      */
  135.     public Object nextRow()
  136.     {
  137.         m = findNextRow();
  138.         
  139.         return m.o;
  140.     }
  141.     
  142.     /**
  143.      * Skips enumeration ahead to the start of the given row.
  144.      * @param r the row to skip ahead to. Must be greater than the current row
  145.      * @return the first element in the given row
  146.      */
  147.     public Object advanceTo(int r)
  148.         throws IllegalArgumentException
  149.     {
  150.         started = true;
  151.         
  152.         if (r < m.row || r == m.row)
  153.         {
  154.             throw new
  155.                IllegalArgumentException("r must be greater than current row: r=" +
  156.                                          r + "current row=" + m.row);
  157.         }
  158.         
  159.         Matrix n = m;
  160.         
  161.         while (m.row < r)
  162.         {
  163.             m = findNextRow();
  164.         
  165.             if (m == null)
  166.             {
  167.                 m = n;
  168.                 throw new
  169.                    IllegalArgumentException("requested row too large: r=" + r);
  170.             }
  171.         }
  172.         
  173.         return m.o;
  174.     }
  175.     
  176.     //--------------------------------------------------
  177.     // private members
  178.     //--------------------------------------------------
  179.     
  180.     private Matrix findNextRow()
  181.     {
  182.         started = true;
  183.         Matrix n = m.nextRow;
  184.     
  185.         if (n == null)
  186.         {
  187.             return null;
  188.         }
  189.     
  190.         //find next row
  191.         while(n.o == null && n.nextElt == null)
  192.         {
  193.             if ((n = n.nextRow) == null)
  194.             {
  195.                 return null;
  196.             }
  197.         }
  198.     
  199.         n = n.o != null  ?n  :n.nextElt;
  200.         return n;
  201.     }
  202.     
  203.     //--------------------------------------------------
  204.     // private members
  205.     //--------------------------------------------------
  206.     
  207.     private Matrix        m;
  208.     private boolean    started;
  209. }
  210.