home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-06-19 | 4.2 KB | 210 lines |
- package symantec.itools.awt;
-
-
- import java.util.Enumeration;
-
-
- // 01/30/97 RKM Integrated fix to hasMoreElements from flash@tansu.slab.tnr.sharp.co.jp (George Rome Borden IV)
- // 03/27/97 RKM Made class public, so peole could use it
- // Made class final
- // Tried to make some sense of protection on methods & members
- // 04/17/97 LAB Added Don Haney's (haney@tiac.net) fix that addressed a NullPointerException in hasMoreElements().
-
- /**
- * This class is used to enumerate Matrix objects. Enumeration is used to
- * sequentially scan through all the elements in the Matrix.
- * @see symantec.itools.awt.Matrix
- * @see java.util.Enumeration
- *
- * @version 1.0, Nov 26, 1996
- *
- * @author Symantec
- *
- */
-
- public final class MatrixEnumeration
- implements Enumeration
- {
- //--------------------------------------------------
- // public constructors
- //--------------------------------------------------
-
- /**
- * Constructs a matrix enumerator.
- */
- public MatrixEnumeration(Matrix matrix)
- {
- m = matrix;
- started = false;
- }
-
- //--------------------------------------------------
- // public members
- //--------------------------------------------------
-
- /**
- * Returns true if more elements are available to enumerate.
- * A standard Java Enumeration interface method.
- * @return true if more elements available, false if done enumerating
- */
- public boolean hasMoreElements()
- {
- if (m == null)
- return false;
- if (started == false && m.o != null) return true;
- if (m.nextElt != null) return true;
- return hasMoreRows();
- }
-
- /**
- * Returns true if more rows are available to enumerate.
- */
- public boolean hasMoreRows()
- {
- if (m.nextRow == null)
- {
- return false;
- }
-
- Matrix n = m.nextRow;
-
- //find next row
- while(n.o == null && n.nextElt == null)
- {
- if ((n = n.nextRow) == null) return false;
- }
-
- return true;
- }
-
- /**
- * Returns the next element.
- * A standard Java Enumeration interface method.
- */
- public Object nextElement()
- {
- if (!started)
- {
- started = true;
-
- if (m.o != null)
- {
- return m.o;
- }
- }
-
- if (m == null)
- {
- return null;
- }
-
- if (m.nextElt != null)
- {
- m = m.nextElt;
- return m.o;
- }
-
- if ((m = findNextRow()) == null)
- {
- return null;
- }
-
- return m.o;
- }
-
- /**
- * Returns the row number of the current element.
- */
- public int currRow()
- {
- return m.row;
- }
-
- /**
- * Returns the column number of the current element.
- */
- public int currCol()
- {
- return m.col;
- }
-
- /**
- * Skips enumeration ahead to the start of the next row.
- * @return the first element in the next row.
- */
- public Object nextRow()
- {
- m = findNextRow();
-
- return m.o;
- }
-
- /**
- * Skips enumeration ahead to the start of the given row.
- * @param r the row to skip ahead to. Must be greater than the current row
- * @return the first element in the given row
- */
- public Object advanceTo(int r)
- throws IllegalArgumentException
- {
- started = true;
-
- if (r < m.row || r == m.row)
- {
- throw new
- IllegalArgumentException("r must be greater than current row: r=" +
- r + "current row=" + m.row);
- }
-
- Matrix n = m;
-
- while (m.row < r)
- {
- m = findNextRow();
-
- if (m == null)
- {
- m = n;
- throw new
- IllegalArgumentException("requested row too large: r=" + r);
- }
- }
-
- return m.o;
- }
-
- //--------------------------------------------------
- // private members
- //--------------------------------------------------
-
- private Matrix findNextRow()
- {
- started = true;
- Matrix n = m.nextRow;
-
- if (n == null)
- {
- return null;
- }
-
- //find next row
- while(n.o == null && n.nextElt == null)
- {
- if ((n = n.nextRow) == null)
- {
- return null;
- }
- }
-
- n = n.o != null ?n :n.nextElt;
- return n;
- }
-
- //--------------------------------------------------
- // private members
- //--------------------------------------------------
-
- private Matrix m;
- private boolean started;
- }
-