home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / VCafe / prosrc.bin / MatrixEnumeration.java < prev    next >
Encoding:
Java Source  |  1998-03-18  |  5.3 KB  |  187 lines

  1. /*
  2.  * Copyright (c) 1996 Krumel & Associates, Inc. All Rights Reserved.
  3.  *
  4.  * www.krumel.com - controls@krumel.com
  5.  *
  6.  * Permission is given to the buyer of this package for one software
  7.  * developer to use this software on one CPU (one workstation) and to make
  8.  * one backup copy.  You may uitilize and/or midfy this class for use in your
  9.  * projects.  You may distribute or sell any executable which results from
  10.  * using this code in yur application, except a utility or class of similar
  11.  * nature to this product.  You may distribute this this product in compiled
  12.  * form only, but soley to be used with your cmpiled executable product
  13.  * for the puposes of dynamic loading. You may NOT redistribute the source
  14.  * code in any form or make it accessible through a network or other
  15.  * distribution media to others. Please refer to the file "copyright.html"
  16.  * for further important copyright and licensing information.
  17.  *
  18.  * The source code is the confidential and proprietary information
  19.  * of Krumel & Associates, Inc. ("Confidential Information").  You shall
  20.  * not disclose such Confidential Information and shall use it only in
  21.  * accordance with the terms of the license agreement you entered into
  22.  * with Krumel & Associates, Inc..
  23.  
  24.  * KRUMEL & ASSOCIATES MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
  25.  * SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT
  26.  * NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  27.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. KRUMEL & ASSOCIATES SHALL NOT
  28.  * BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
  29.  * MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  30.  */
  31.  
  32. package symantec.itools.db.awt.genutil;
  33.  
  34. import java.util.Enumeration;
  35.  
  36. /**
  37.  * An enumeration for the Matrix class.
  38.  *
  39.  * @version 1.1   June 2, 1997
  40.  * @author  Andy Krumel
  41.  */
  42. public class MatrixEnumeration implements Enumeration {
  43.     Matrix m ;
  44.     int row = -1;
  45.     MatrixElement elt;
  46.  
  47.     /**
  48.      * Creates an enumeration for a matrix.
  49.      */
  50.     public MatrixEnumeration(Matrix matrix) {
  51.         m = (Matrix)matrix.clone();
  52.     }
  53.  
  54.     /**
  55.      * Determines if more data exists in matrix.
  56.      */
  57.     public final boolean hasMoreElements() {
  58.         if (m==null) {
  59.             //already iterated
  60.             return false;
  61.         } else if (row==-1) {
  62.             //have not gotten first element so see if any in matrix
  63.             int size = m.rows();
  64.             int currRow = 0;
  65.             while(!m.containsRow(currRow)) {
  66.                 currRow++;
  67.                 if (size<currRow) { return false; }
  68.             }
  69.  
  70.             return true;
  71.         }
  72.  
  73.         return elt!=null;
  74.     }
  75.  
  76.     /**
  77.      * Determines if more valid rows exist in the matrix.
  78.      */
  79.     public final boolean hasMoreRows() {
  80.         int size = m.rows();
  81.         int nextRow = row+1;
  82.  
  83.         while(!m.containsRow(nextRow)) {
  84.             nextRow++;
  85.             if (size < nextRow) {
  86.                 return false;
  87.             }
  88.         }
  89.  
  90.         return true;
  91.     }
  92.  
  93.     /**
  94.      * Gets the next object.
  95.      */
  96.     public final Object nextElement() {
  97.         if (row==-1 || elt.next==null) {
  98.             findNextRow();
  99.         } else {
  100.             elt = elt.next;
  101.         }
  102.  
  103.         return elt.obj;
  104.     }
  105.  
  106.     private MatrixElement findNextRow() {
  107.         int size = m.rows();
  108.         row++;
  109.         while(!m.containsRow(row)) {
  110.             row++;
  111.             if (size<row) {
  112.                 //no data left in matrix so we are done
  113.                 m = null;
  114.                 return null;
  115.             }
  116.         }
  117.  
  118.         return elt = m.firstRowElement(row);
  119.     }
  120.  
  121.     /**
  122.      * Gets the row number for the current element.
  123.      */
  124.     public final int currRow() { return row; }
  125.  
  126.     /**
  127.      * Gets the column number for the current element.
  128.      */
  129.     public final int currCol() {
  130.         if (elt==null) {
  131.             return -1;
  132.         } else {
  133.             return elt.col;
  134.         }
  135.     }
  136.  
  137.     /**
  138.      * Moves the matrix element pointer to the next valid row and returns the
  139.      * first element in that row.
  140.      */
  141.     public final Object nextRow() {
  142.         findNextRow();
  143.  
  144.         if (elt==null) {
  145.             return null;
  146.         }
  147.  
  148.         return elt.obj;
  149.     }
  150.  
  151.     /**
  152.      * Moves the matrix element pointer to the specified row. If no data exists
  153.      * for that row, the pointer is moved to the next row with data.
  154.      * @return The first object at that row (or next valid row) or null if no more
  155.      *      valid rows exist.
  156.      */
  157.     public final Object advanceTo(int r) throws IllegalArgumentException {
  158.         if (r < row || r == row)
  159.             throw new
  160.                IllegalArgumentException("r must be greater than current row: r=" +
  161.                                          r + " current row=" + row);
  162.  
  163.         if (r > m.rows()) {
  164.             throw new IllegalArgumentException("requested row too large: r=" + r);
  165.         }
  166.  
  167.         int size = m.rows();
  168.         row = r;
  169.         while(!m.containsRow(row)) {
  170.             row++;
  171.             if (size < row) {
  172.                 //no data left in matrix we are done
  173.                 m = null;
  174.                 return null;
  175.             }
  176.         }
  177.  
  178.         elt = m.firstRowElement(row);
  179.  
  180.         return elt.obj;
  181.     }
  182.  
  183.     public final String toString() {
  184.         return "MatrixEnumeration: row="+row+" col="+ (elt!=null ?(""+elt.col) :"-");
  185.     }
  186. }
  187.