/* * Created on 8/04/2003 * */ package net.sourceforge.phpdt.sql.sql.metadata; import java.util.Vector; /** * @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. */ public class StringMatrix { private Vector header = new Vector(10,10); private Vector matrix = new Vector(10,10); /** * Adds a String to the end of the header keys * @param header : The string to be added */ public void addHeader(String header){ this.header.add(header); } /** * Adds a whole vector to the header * @param header */ private void addVectorHeader(Vector header){ this.header.addAll(header); } /** * Adds a String to the end of the row indicated * @param value : The string to be added * @param row : The row to */ public void add(String value, int row) { grow(row); Vector rowVector = (Vector) matrix.get(row); rowVector.add(value); } private void grow(int row) { if (matrix.size() <= row) for (int i = matrix.size(); i <= row; i++) { matrix.add(new Vector(header.size(), 1)); } } /** * Adds a StringMatrix to the end of the row indicated * @param value : The string to be added * @param row : The row to */ public void add(StringMatrix value) { int row = matrix.size(); for (int i = 0; i < value.size(); i++){ grow(row); for (int j = 0; j < value.getNumColumns(); j++){ String header = value.getHeaderColumn(j); addAt(header, value.get(header,i), row); } row++; } Vector rowVector = (Vector) matrix.get(row); rowVector.add(value); } /** * Adds a String to the row indicated, to the column that matches the key * @param value : The string to be added * @param row : The row to */ 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); rowVector.add(ind, value); } /** * Adds a whole vector to the end of the row indicated * @param value : The vector to be added * @param row : The row to */ private void addVector(Vector value, int row){ grow(row); Vector rowVector = (Vector) matrix.get(row); rowVector.addAll(value); } public boolean contains(String key){ return header.contains(key); } /** * Gets a String value from the row indicated, from the column that matches the key * @param key * @param row * @return */ public String get(String key, int row){ if (matrix.size() <= row) return null; int col = header.indexOf(key); if (col < 0) return null; Vector rowVector = (Vector) matrix.get(row); if (rowVector == null) return null; return (String) rowVector.get(col); } /** * @param key: selects the column * @return a Vector with all the values in the selected column; null if empty */ public Vector getColumn(String key){ if (size() < 1 ) return null; Vector result = new Vector(size(),1); for (int i = 0; i < size(); i++){ result.add(get(key, i)); } return result; } /** * @param key: selects the column * @return a Vector with all the values in the selected column, dropping duplicates; null if empty */ public Vector getUniqueColumn(String key){ if (size() < 1 ) return null; Vector result = new Vector(size(),1); for (int i = 0; i < size(); i++){ if (!result.contains(get(key, i))) result.add(get(key, i)); } return result; } /** * @param key: selects the column * @return a Vector of Integers with all the indexes of the rows * matching the selected column, dropping duplicates; null if empty */ public Vector getIndexes(String key, String value){ Vector result = new Vector(); for (int i = 0; i < size(); i++){ if (get(key, i).equals(value)) result.add(new Integer(i)); } return result; } /** * Deletes all the rows that matches the value for the key * @param key: selects the column */ public void dropMatching(String key, String value){ for (int i = 0; i < size(); i++){ if (get(key, i).equals(value)) deleteRow(i); } } /** * Returns a StringMatrix with all the complete rows that match the key - value pair * @param key The column key * @param value The value to match * @return a StringMatrix with only the rows where the key equals the value */ public StringMatrix select(String key, String value){ StringMatrix result = new StringMatrix(); result.addVectorHeader(header); int j = 0; for (int i = 0; i < size(); i++){ if (get(key, i).equals(value)) { result.addVector((Vector)matrix.get(i), j); j++; } } return result; } /** * @param i * @return : a StringMatrix with only one row, the selected by i */ public StringMatrix rowMatrix(int i){ StringMatrix result = new StringMatrix(); result.addVectorHeader(header); result.addVector((Vector)matrix.get(i),0); return result; } /** * @return the number of rows */ public int size() { return matrix.size(); } public int getNumColumns() { return header.size(); } public String getHeaderColumn(int i){ return (String) header.get(i); } public void deleteRow(int i){ matrix.remove(i); } }