BooleanMatrix.java

package edu.hawaii.ics.yucheng;

/**
 * Implementation of a matrix with elements of type boolean.
 */
class BooleanMatrix {

    /**
     * Returns true if the specified value is outside the range.
     * 
     * @param value The value to check.
     * 
     * @param low The low value (inclusive).
     * 
     * @param high The high value (inclusive).
     * 
     * @return True indicates the value is outside of the range.
     */
    private static boolean exceeds(final int value, final int low, final int high) {
        return value < low || value > high;
    }

    /** The number of columns in the matrix. */
    public final int        columns;

    /** The array of boolean values. */
    private final boolean[] myArray;

    /** The number of rows in the matrix. */
    public final int        rows;


    /**
     * Initializes a new instance of the class.
     * 
     * @param rows The number of rows in the matrix.
     * 
     * @param columns The number of columns in the matrix.
     */
    public BooleanMatrix(final int rows, final int columns) {

        if (columns <= 0)
            throw new IllegalArgumentException("columns");
        if (rows <= 0)
            throw new IllegalArgumentException("rows");

        this.columns = columns;
        this.rows = rows;
        myArray = new boolean[columns * rows];
    }


    /**
     * Returns the array index of an element in the matrix.
     * 
     * @param row The row.
     * 
     * @param column The column.
     * 
     * @return The index in the array.
     */
    private int indexOf(final int row, final int column) {

        if (exceeds(column, 0, columns - 1))
            throw new IndexOutOfBoundsException("x");
        if (exceeds(row, 0, rows - 1))
            throw new IndexOutOfBoundsException("y");

        return columns * row + column;
    }


    /**
     * Returns true if an element in the matrix is marked true.
     * 
     * @param row The row.
     * 
     * @param column The column.
     * 
     * @return True indicates the element is marked true.
     */
    public boolean isMarked(final int row, final int column) {
        return myArray[indexOf(row, column)];
    }


    /**
     * Marks an element in the matrix as true.
     * 
     * @param row The row.
     * 
     * @param column The column.
     */
    public void mark(final int row, final int column) {
        myArray[indexOf(row, column)] = true;
    }


    /**
     * Returns the string representation of the class data.
     * 
     * @return The string representation of the class data.
     */
    @Override
    public String toString() {

        final StringBuilder builder = new StringBuilder();

        for (int j = 0; j < rows; j++) {
            for (int i = 0; i < columns; i++)
                builder.append(isMarked(j, i) ? "1" : "0");
            builder.append("\n");
        }

        return builder.toString();
    }


    /**
     * Marks an element in the matrix as false.
     * 
     * @param row The row.
     * 
     * @param column The column.
     */
    public void unmark(final int row, final int column) {
        myArray[indexOf(row, column)] = false;
    }
}
Valid HTML 4.01 Valid CSS