📄 grid2d.java
字号:
package org.jahia.layout;/** * This class represents a dynamic 2D grid in which we can store objects. This * is an alternative to using arrays, allowing us to be able to add and remove * rows at columns from the grid. These grids are always rectangular, meaning * that every line has the same length. * * This class may also be used to build size-limited grids, that are defined * by using the second constructor. This is recommended in cases where we want * to be sure we never build a grid that is bigger than a certain size (also * useful for debugging purposes :)) * * This implementation uses Vectors of Vectors to represent the grid. If * performance becomes an issue we might want to look into other ways of * representing the grid. It is anticipated though that we will be adding rows * and columns often, and there for this solution seems to be better fitted than * using two dimensionnal arrays, which would need to be copied in order to be * enlarged. * * Title: Jahia * Description: Jahia Portal Server * Copyright: Copyright (c) 2002 * Company: Jahia Ltd * @author Serge Huber * @version 1.0 */import java.util.*;public class Grid2D { /** * @associates Vector */ private Vector rowList = new Vector(); private int rowCount = 0; private int columnCount = 0; // The following values are used to limit the maximum size of the grid if // they are set to positive values. private int maxRows = -1; private int maxColumns = -1; /** * This inner class is only used to tag grid entries so that we may * recognize them as empty */ private class DummyGridEmptyObject { } /** * Not much to say here, just a standard constructor */ public Grid2D() { } /** * This constructor allows a construction of a grid with limited maximum * size. * @param maxRowCount specifies the maximum row size of the grid * @param maxColumnCount specifies the maximum column size of the grid */ public Grid2D(int maxRowCount, int maxColumnCount) { maxRows = maxRowCount; maxColumns = maxColumnCount; } /** * Returns the number of rows in the grid * @returns an integer representing the number of rows in the grid :) */ public int getRowCount() { return rowCount; } /** * Returns the number of columns in the grid * @returns an integer representing the number of columns in the grid :) */ public int getColumnCount() { return columnCount; } /** * Adds a number of empty rows to the grid. This is designed for internal * use only. * @param numberOfRows an integer specifying the number of rows to add to * the function. * @exception ArrayIndexOutOfBoundsException thrown if the number of rows to * be added would make the grid cross the maximum number of rows boundary. * In this case NO rows are added at all. */ private void addRows(int numberOfRows) throws ArrayIndexOutOfBoundsException { if (maxRows >= 0) { if (rowCount + numberOfRows > maxRows) { throw new ArrayIndexOutOfBoundsException("Grid2D.addRows : number of rows to add would exceed maximum number of rows !"); } } for (int i=0; i < numberOfRows; i++) { Vector curVector = new Vector(); // we insert a simple object, to be replaced later by a real value. for (int j=0; j < columnCount; j++) { curVector.add(new DummyGridEmptyObject()); } rowList.add(curVector); } rowCount += numberOfRows; } /** * Adds a number of empty columns to the grid. This is designed for internal * use only. * @param numberOfColumns an integer specifying the number of columns to be * added. * @exception ArrayIndexOutOfBoundsException thrown if the number of columns * to be added would make the grid cross the maximum number of columns * boundary. In this case NO rows are added at all. */ private void addColumns(int numberOfColumns) throws ArrayIndexOutOfBoundsException { if (maxColumns >= 0) { if (columnCount + numberOfColumns > maxColumns) { throw new ArrayIndexOutOfBoundsException("Grid2D.addColumns : number of columns to add would exceed maximum number of columns !"); } } // for each row, let's add an entry to the vector in it. for (int i=0; i < rowCount; i++) { Vector curVector = (Vector) rowList.elementAt(i); // we insert a simple object, to be replaced later by a real value. for (int j=0; j < numberOfColumns; j++) { curVector.add(new DummyGridEmptyObject()); } } columnCount += numberOfColumns; } /** * Returns the object at the specified grid position. * @param row an integer (0-based) representing the row at which we want to * recuperate the object * @param column an integer (0-based) representing the column at which we * want to recuperate the object * @returns the object at the specified coordinates. If there is no object, * null is returned. * @exception ArrayIndexOutOfBoundsException thrown if the row or column * coordinates are invalid */ public Object getElementAt(int row, int column) throws ArrayIndexOutOfBoundsException { if ((row < 0) || (row >= rowCount) ) { throw new ArrayIndexOutOfBoundsException ("Grid2D.elementAt : row value " + Integer.toString(row) + " is invalid ! (rowCount=" + Integer.toString(rowCount) + ")"); } if ((column < 0) || (column >= columnCount) ) { throw new ArrayIndexOutOfBoundsException ("Grid2D.elementAt : column value " + Integer.toString(column) + " is invalid ! (rowCount=" + Integer.toString(rowCount) + ")"); } Vector rowVector = (Vector) rowList.elementAt(row); Object theObject = rowVector.elementAt(column); if (theObject instanceof DummyGridEmptyObject) { return null; } else { return theObject; } } /** * Set the grid position at the specified coordinates at the value specified * by the object. If the value of the coordinates are out of the current * grid size and are positive, the size of the grid is extended to allow * this position to be valid. * WARNING : if invalid positions or random values are passed here, this * data structure may become dangerously large ! * * @param row an integer (0-based) specifying the row at which to set the * object * @param column an integer (0-based) specifying the column at which to set * the object * @param theObject the actual value to be set at the specified coordinates * @exception ArrayIndexOutOfBoundsException thrown if the coordinates are * larger than the maximum number of rows and columns allowed. In this case * the grid is not modified at all. */ public void setElementAt(int row, int column, Object theObject) throws ArrayIndexOutOfBoundsException { // first let's test if we must enlarge the grid... if ( (row+1) > rowCount ) { addRows(row+1-rowCount); } if ( (column+1) > columnCount ) { addColumns(column+1-columnCount); } // we may now set the value of the object. Vector rowVector = (Vector) rowList.elementAt(row); rowVector.setElementAt(theObject, column); } /** * Sets an empty element at the position specified, creating the new * rows and columns if necessary. * @param row * @param column * * @see setElementAt */ public void setEmptyElementAt(int row, int column) { setElementAt(row, column, new DummyGridEmptyObject() ); } /** * Empties the grid of all it's contents. */ public void clear() { rowList.clear(); rowList = null; rowList = new Vector(); rowCount = 0; columnCount = 0; } /** * Converts the internal structure to a grid. This is done by calling the * toString method of each object, and insertion newline characters after * each row in the grid... * @returns a String containing the grid's output seperated by newline * characters... */ public String toString() { StringBuffer result = new StringBuffer(""); for (int i = 0; i < getRowCount() ; i++) { for (int j = 0; j < getColumnCount(); j++) { Object curObject = getElementAt(i, j); if (curObject == null) { result.append("<null>"); } else { result.append(curObject.toString()); } result.append(" "); } result.append('\n'); } return result.toString(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -