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 Vector "line" is supposed to have headers to the values of the rest.
13 * Those headers will always be case insensitive
15 public class StringMatrix {
16 private final int DEFAULT_COLUMNS = 10;
17 private final int DEFAULT_ROWS = 10;
18 private final int DEFAULT_INCREMENT = 10;
20 private Vector header = new Vector(DEFAULT_COLUMNS,DEFAULT_INCREMENT);
21 private Vector matrix = new Vector(DEFAULT_ROWS,DEFAULT_INCREMENT);
24 * Adds a String to the end of the header keys
25 * @param header : The string to be added
27 public void addHeader(String header) {
28 this.header.add(header);
31 * Adds a whole vector to the header
34 private void addVectorHeader(Vector header){
35 this.header.addAll(header);
36 for (int i = 0; i < this.header.size(); i++) {
37 String element = (String) this.header.get(i);
38 this.header.setElementAt(element, i);
42 * Adds a whole matrix to the header
45 public void addMatrixHeader(String header[]){
46 for (int i = 0; i < header.length; i++) {
47 String element = header[i];
48 this.header.add(element);
52 * Adds a String to the end of the row indicated
53 * @param value : The string to be added
54 * @param row : The row to
56 public void add(String value, int row) {
58 Vector rowVector = (Vector) matrix.get(row);
62 * Adds a StringMatrix to the end of the row indicated
63 * @param value : The string to be added
64 * @param row : The row to
66 public void add(StringMatrix value) {
67 int row = matrix.size();
68 for (int i = 0; i < value.size(); i++){
70 for (int j = 0; j < value.getNumColumns(); j++){
71 String header = value.getHeaderColumn(j);
72 addAt(header, value.get(header,i), row);
76 Vector rowVector = (Vector) matrix.get(row);
81 * Converts the StringMatrix into a copy of the given
82 * @param origin - The StringMatrix to be copied
84 public void copy(StringMatrix origin) {
90 * Clears (emtpies) the StringMatrix
97 * Adds a String to the row indicated, to the column that matches the key
98 * @param value : The string to be added
99 * @param row : The row to
101 public void addAt(String key, String value, int row) {
103 Vector rowVector = (Vector) matrix.get(row);
104 int ind = header.indexOf(key);
106 if (rowVector.size() < ind+1) rowVector.setSize(ind);
107 rowVector.add(ind, value);
110 * Adds a whole vector to the end of the row indicated
111 * @param value : The vector to be added
112 * @param row : The row to
114 private void addVector(Vector value, int row){
116 Vector rowVector = (Vector) matrix.get(row);
117 rowVector.addAll(value);
121 * Tells if you have that particular key in your StringMatrix
125 public boolean contains(String key){
126 return (findKey(key) >= 0);
129 * Gets a String value from the row indicated, from the column that matches the key
134 public String get(String key, int row){
135 if (matrix.size() <= row) return null;
136 int col = findKey(key);
137 if (col < 0) return null;
138 Vector rowVector = (Vector) matrix.get(row);
139 if (rowVector == null) return null;
140 return (String) rowVector.get(col);
143 * Finds the key in the header
147 private int findKey(String key) {
148 for (int i = 0; i < header.size(); i++) {
149 String element = (String) header.get(i);
150 if (element.equalsIgnoreCase(key)) return i;
155 * @param key: selects the column
156 * @return a Vector with all the values in the selected column; null if empty
158 public Vector getColumn(String key){
159 if (size() < 1 ) return null;
160 Vector result = new Vector(size(),1);
161 for (int i = 0; i < size(); i++){
162 result.add(get(key, i));
167 * @param key: selects the column
168 * @return a Vector with all the values in the selected column, dropping duplicates; null if empty
170 public Vector getUniqueColumn(String key){
171 if (size() < 1 ) return null;
172 Vector result = new Vector(size(),1);
173 for (int i = 0; i < size(); i++){
174 if (!result.contains(get(key, i))) result.add(get(key, i));
179 * @param key: selects the column
180 * @return a Vector of Integers with all the indexes of the rows
181 * matching the selected column, dropping duplicates; null if empty
183 public Vector getIndexes(String key, String value){
184 Vector result = new Vector();
185 for (int i = 0; i < size(); i++){
186 if (get(key, i).equals(value))
187 result.add(new Integer(i));
192 * Deletes all the rows that matches the value for the key
193 * @param key: selects the column
195 public void dropMatching(String key, String value){
196 for (int i = 0; i < size(); i++){
197 if (get(key, i).equals(value)) deleteRow(i);
201 * Returns a StringMatrix with all the complete rows that match the key - value pair
202 * @param key The column key
203 * @param value The value to match
204 * @return a StringMatrix with only the rows where the key equals the value
206 public StringMatrix select(String key, String value){
207 StringMatrix result = new StringMatrix();
208 result.addVectorHeader(header);
210 for (int i = 0; i < size(); i++){
211 if (get(key, i).equals(value)) {
212 result.addVector((Vector)matrix.get(i), j);
221 * Grows the StringMatrix till it's able to hold "row" rows.
222 * @param row : The number of rows that must hold
224 private void grow(int row) {
225 if (matrix.size() <= row)
226 for (int i = matrix.size(); i <= row; i++) {
227 matrix.add(new Vector(header.size(), 1));
231 * Gets a String value from the row indicated , from the column number
232 * @param col : 0-index column
233 * @param row : 0-index column
236 private String get(int col, int row){
237 if (col < 0 || row < 0) return null;
238 Vector rowVector = (Vector) matrix.get(row);
239 if (rowVector == null) return null;
240 return (String) rowVector.get(col);
243 // Generic interfaces
246 * @return : a StringMatrix with only one row, the selected by i
248 public StringMatrix rowMatrix(int i){
249 StringMatrix result = new StringMatrix();
250 result.addVectorHeader(header);
251 result.addVector((Vector)matrix.get(i),0);
256 * Resturns a String[] with the selected row
257 * @param i The index of the row
260 public String[] getRow(int i) {
261 if (i < matrix.size()) {
262 String[] result = new String[header.size()];
263 Vector resVector = (Vector)matrix.get(i);
264 resVector.toArray(result);
273 * @return the number of rows
276 return matrix.size();
278 public int getNumColumns() {
279 return header.size();
281 public String getHeaderColumn(int i){
282 return (String) header.get(i);
284 public void deleteRow(int i){
289 // Common Object interface
291 * @see java.lang.Object#clone()
293 public Object clone() {
295 StringMatrix matrix = new StringMatrix();
301 * @see java.lang.Object#toString()
303 public String toString() {
305 for (int j = 0; j < header.size(); j++){
306 result += (String) header.get(j);
311 for (int i = 0; i < matrix.size(); i++){
312 for (int j = 0; j < header.size(); j++){
322 * @see java.lang.Object#equals(java.lang.Object)
324 public boolean equals(Object obj) {
325 if (!(obj instanceof StringMatrix)) return false;
326 StringMatrix sm = (StringMatrix) obj;
327 if (!header.equals(sm.header)) return false;
328 if (matrix.size() != sm.matrix.size()) return false;
329 for (int i = 0; i < matrix.size(); i++){
330 Vector row = (Vector) matrix.get(i);
331 Vector smRow = (Vector) sm.matrix.get(i);
332 if (!(row.equals(smRow))) return false;
340 public Vector getMatrix() {
346 public String[] getHeader() {
347 String[] result = new String[header.size()];
348 header.toArray(result);