home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 1998 November
/
Chip_1998-11_cd.bin
/
tema
/
Cafe
/
Source.bin
/
MatrixEnumeration.java
< prev
next >
Wrap
Text File
|
1998-03-18
|
5KB
|
221 lines
package symantec.itools.awt;
import java.util.Enumeration;
import java.text.MessageFormat;
import java.util.ResourceBundle;
// 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 elements. Enumeration is used to
* sequentially scan through all the elements in a Matrix one at a time.
* @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 for the given Matrix.
*/
public MatrixEnumeration(Matrix matrix)
{
errors = ResourceBundle.getBundle("symantec.itools.resources.ErrorsBundle");
m = matrix;
started = false;
}
//--------------------------------------------------
// public members
//--------------------------------------------------
/**
* Returns true if more elements are available to enumerate.
* This is 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.
* This is 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;
} else {
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
* @exception IllegalArgumentException if r is less than or equal to the
* current row
*/
public Object advanceTo(int r)
throws IllegalArgumentException
{
started = true;
if (r < m.row || r == m.row)
{
Object[] args = { new Integer(r), new Integer(m.row) };
throw new
IllegalArgumentException(MessageFormat.format(errors.getString("MustBeGreaterThanCurrentRow"), args));
}
Matrix n = m;
while (m.row < r)
{
m = findNextRow();
if (m == null)
{
m = n;
Object[] args = { new Integer(r) };
throw new
IllegalArgumentException(MessageFormat.format(errors.getString("RowTooLarge"), args));
}
}
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;
}
/**
* Error strings.
*/
transient protected ResourceBundle errors;
//--------------------------------------------------
// private members
//--------------------------------------------------
private Matrix m;
private boolean started;
}