/**
* @author jparrai
* Generic class to hold a Matrix of Strings, that is a Vector of Vectors of Strings.
- * The first Vector "line" is supposed to have headers to the values of the rest.
+ * The first StringMatrix "line" is supposed to have headers to the values of the rest.
* Those headers will always be case insensitive
+ * Rows start at 0
*/
public class StringMatrix {
private final int DEFAULT_COLUMNS = 10;
matrix.clear();
}
/**
- * Adds a String to the row indicated, to the column that matches the key
+ * Adds a String to the row indicated, to the column that matches the key
+ * The matrix will grow to acomodate the indexes, so be careful
* @param value : The string to be added
- * @param row : The row to
+ * @param row : The row to update
*/
public void addAt(String key, String value, int row) {
grow(row);
Vector rowVector = (Vector) matrix.get(row);
int ind = header.indexOf(key);
if (ind < 0) return;
- if (rowVector.size() < ind+1) rowVector.setSize(ind);
+ if (rowVector.size() <= ind) rowVector.setSize(ind);
rowVector.add(ind, value);
}
/**
+ * Adds a String in the location specified.
+ * The matrix will grow to acomodate the indexes, so be careful
+ * @param column Column selected
+ * @param row row selected
+ * @param value value to add
+ */
+ public void addAt(int column, int row, String value) {
+ grow(row);
+ Vector rowVector = (Vector) matrix.get(row);
+ if (column >= rowVector.size()) rowVector.setSize(column);
+ rowVector.add(column, value);
+ }
+ /**
* Adds a whole vector to the end of the row indicated
* @param value : The vector to be added
* @param row : The row to
* @param row : 0-index column
* @return
*/
- private String get(int col, int row){
+ public String get(int col, int row){
if (col < 0 || row < 0) return null;
Vector rowVector = (Vector) matrix.get(row);
if (rowVector == null) return null;
- return (String) rowVector.get(col);
+ if (col < rowVector.size())
+ return (String) rowVector.get(col);
+ else return null;
}
// Generic interfaces
public int size() {
return matrix.size();
}
+ /**
+ * @return The number of columns of the StringMatrix
+ */
public int getNumColumns() {
return header.size();
}
+ /**
+ * @param i
+ * @return The name of the header column in the "i" position. Null if no such position.
+ */
public String getHeaderColumn(int i){
- return (String) header.get(i);
+ if (i < header.size())
+ return (String) header.get(i);
+ return null;
}
+ /**
+ * Deletes the row number i
+ * @param i
+ */
public void deleteRow(int i){
- matrix.remove(i);
+ if (i < matrix.size())
+ matrix.remove(i);
}