5 package com.quantum.util;
7 import java.util.Vector;
11 * Generic class to hold a Matrix of Strings, that is a Vector of Vectors of Strings.
12 * The first StringMatrix "line" is supposed to have headers to the values of the rest.
13 * Those headers will always be case insensitive
16 public class StringMatrix {
17 private final int DEFAULT_COLUMNS = 10;
18 private final int DEFAULT_ROWS = 10;
19 private final int DEFAULT_INCREMENT = 10;
21 private Vector header = new Vector(DEFAULT_COLUMNS,DEFAULT_INCREMENT);
22 private Vector matrix = new Vector(DEFAULT_ROWS,DEFAULT_INCREMENT);
25 * Adds a String to the end of the header keys
26 * @param header : The string to be added
28 public void addHeader(String header) {
29 this.header.add(header);
32 * Adds a whole vector to the header
35 private void addVectorHeader(Vector header){
36 this.header.addAll(header);
37 for (int i = 0; i < this.header.size(); i++) {
38 String element = (String) this.header.get(i);
39 this.header.setElementAt(element, i);
43 * Adds a whole matrix to the header
46 public void addMatrixHeader(String header[]){
47 for (int i = 0; i < header.length; i++) {
48 String element = header[i];
49 this.header.add(element);
53 * Adds a String to the end of the row indicated
54 * @param value : The string to be added
55 * @param row : The row to
57 public void add(String value, int row) {
59 Vector rowVector = (Vector) matrix.get(row);
63 * Adds a StringMatrix to the end of the row indicated
64 * @param value : The string to be added
65 * @param row : The row to
67 public void add(StringMatrix value) {
68 int row = matrix.size();
69 for (int i = 0; i < value.size(); i++){
71 for (int j = 0; j < value.getNumColumns(); j++){
72 String header = value.getHeaderColumn(j);
73 addAt(header, value.get(header,i), row);
77 Vector rowVector = (Vector) matrix.get(row);
82 * Converts the StringMatrix into a copy of the given
83 * @param origin - The StringMatrix to be copied
85 public void copy(StringMatrix origin) {
91 * Clears (emtpies) the StringMatrix
98 * Adds a String to the row indicated, to the column that matches the key
99 * The matrix will grow to acomodate the indexes, so be careful
100 * @param value : The string to be added
101 * @param row : The row to update
103 public void addAt(String key, String value, int row) {
105 Vector rowVector = (Vector) matrix.get(row);
106 int ind = header.indexOf(key);
108 if (rowVector.size() <= ind) rowVector.setSize(ind);
109 rowVector.add(ind, value);
112 * Adds a String in the location specified.
113 * The matrix will grow to acomodate the indexes, so be careful
114 * @param column Column selected
115 * @param row row selected
116 * @param value value to add
118 public void addAt(int column, int row, String value) {
120 Vector rowVector = (Vector) matrix.get(row);
121 if (column >= rowVector.size()) rowVector.setSize(column);
122 rowVector.add(column, value);
125 * Adds a whole vector to the end of the row indicated
126 * @param value : The vector to be added
127 * @param row : The row to
129 private void addVector(Vector value, int row){
131 Vector rowVector = (Vector) matrix.get(row);
132 rowVector.addAll(value);
136 * Tells if you have that particular key in your StringMatrix
140 public boolean contains(String key){
141 return (findKey(key) >= 0);
144 * Gets a String value from the row indicated, from the column that matches the key
149 public String get(String key, int row){
150 if (matrix.size() <= row) return null;
151 int col = findKey(key);
152 if (col < 0) return null;
153 Vector rowVector = (Vector) matrix.get(row);
154 if (rowVector == null) return null;
155 return (String) rowVector.get(col);
158 * Finds the key in the header
162 private int findKey(String key) {
163 for (int i = 0; i < header.size(); i++) {
164 String element = (String) header.get(i);
165 if (element.equalsIgnoreCase(key)) return i;
170 * @param key: selects the column
171 * @return a Vector with all the values in the selected column; null if empty
173 public Vector getColumn(String key){
174 if (size() < 1 ) return null;
175 Vector result = new Vector(size(),1);
176 for (int i = 0; i < size(); i++){
177 result.add(get(key, i));
182 * @param key: selects the column
183 * @return a Vector with all the values in the selected column, dropping duplicates; null if empty
185 public Vector getUniqueColumn(String key){
186 if (size() < 1 ) return null;
187 Vector result = new Vector(size(),1);
188 for (int i = 0; i < size(); i++){
189 if (!result.contains(get(key, i))) result.add(get(key, i));
194 * @param key: selects the column
195 * @return a Vector of Integers with all the indexes of the rows
196 * matching the selected column, dropping duplicates; null if empty
198 public Vector getIndexes(String key, String value){
199 Vector result = new Vector();
200 for (int i = 0; i < size(); i++){
201 if (get(key, i).equals(value))
202 result.add(new Integer(i));
207 * Deletes all the rows that matches the value for the key
208 * @param key: selects the column
210 public void dropMatching(String key, String value){
211 for (int i = 0; i < size(); i++){
212 if (get(key, i).equals(value)) deleteRow(i);
216 * Returns a StringMatrix with all the complete rows that match the key - value pair
217 * @param key The column key
218 * @param value The value to match
219 * @return a StringMatrix with only the rows where the key equals the value
221 public StringMatrix select(String key, String value){
222 StringMatrix result = new StringMatrix();
223 result.addVectorHeader(header);
225 for (int i = 0; i < size(); i++){
226 if (get(key, i).equals(value)) {
227 result.addVector((Vector)matrix.get(i), j);
236 * Grows the StringMatrix till it's able to hold "row" rows.
237 * @param row : The number of rows that must hold
239 private void grow(int row) {
240 if (matrix.size() <= row)
241 for (int i = matrix.size(); i <= row; i++) {
242 matrix.add(new Vector(header.size(), 1));
246 * Gets a String value from the row indicated , from the column number
247 * @param col : 0-index column
248 * @param row : 0-index column
251 public String get(int col, int row){
252 if (col < 0 || row < 0) return null;
253 Vector rowVector = (Vector) matrix.get(row);
254 if (rowVector == null) return null;
255 if (col < rowVector.size())
256 return (String) rowVector.get(col);
260 // Generic interfaces
263 * @return : a StringMatrix with only one row, the selected by i
265 public StringMatrix rowMatrix(int i){
266 StringMatrix result = new StringMatrix();
267 result.addVectorHeader(header);
268 result.addVector((Vector)matrix.get(i),0);
273 * Resturns a String[] with the selected row
274 * @param i The index of the row
277 public String[] getRow(int i) {
278 if (i < matrix.size()) {
279 String[] result = new String[header.size()];
280 Vector resVector = (Vector)matrix.get(i);
281 resVector.toArray(result);
290 * @return the number of rows
293 return matrix.size();
296 * @return The number of columns of the StringMatrix
298 public int getNumColumns() {
299 return header.size();
303 * @return The name of the header column in the "i" position. Null if no such position.
305 public String getHeaderColumn(int i){
306 if (i < header.size())
307 return (String) header.get(i);
311 * Deletes the row number i
314 public void deleteRow(int i){
315 if (i < matrix.size())
320 // Common Object interface
322 * @see java.lang.Object#clone()
324 public Object clone() {
326 StringMatrix matrix = new StringMatrix();
332 * @see java.lang.Object#toString()
334 public String toString() {
336 for (int j = 0; j < header.size(); j++){
337 result += (String) header.get(j);
342 for (int i = 0; i < matrix.size(); i++){
343 for (int j = 0; j < header.size(); j++){
353 * @see java.lang.Object#equals(java.lang.Object)
355 public boolean equals(Object obj) {
356 if (!(obj instanceof StringMatrix)) return false;
357 StringMatrix sm = (StringMatrix) obj;
358 if (!header.equals(sm.header)) return false;
359 if (matrix.size() != sm.matrix.size()) return false;
360 for (int i = 0; i < matrix.size(); i++){
361 Vector row = (Vector) matrix.get(i);
362 Vector smRow = (Vector) sm.matrix.get(i);
363 if (!(row.equals(smRow))) return false;
371 public Vector getMatrix() {
377 public String[] getHeader() {
378 String[] result = new String[header.size()];
379 header.toArray(result);