latest quantum sources 2.3.2
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / util / StringMatrix.java
1 /*
2  * Created on 8/04/2003
3  *
4  */
5 package com.quantum.util;
6
7 import java.util.Vector;
8
9 /**
10  * @author jparrai
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
14  */
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;
19         
20         private Vector header = new Vector(DEFAULT_COLUMNS,DEFAULT_INCREMENT);
21         private Vector matrix = new Vector(DEFAULT_ROWS,DEFAULT_INCREMENT);
22         
23         /**
24          * Adds a String to the end of the header keys
25          * @param header : The string to be added
26          */
27         public void addHeader(String header) {
28                         this.header.add(header);
29         }
30         /**
31          * Adds a whole vector to the header
32          * @param header
33          */
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);                   
39                         }
40         }
41         /**
42          * Adds a whole matrix to the header
43          * @param header
44          */
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);                       
49                         }
50         }
51         /**
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 
55          */
56         public void add(String value, int row) {
57                 grow(row);
58                 Vector rowVector = (Vector) matrix.get(row);
59                 rowVector.add(value);
60         }
61         /**
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 
65          */
66         public void add(StringMatrix value) {
67                 int row = matrix.size();
68                 for (int i = 0; i < value.size(); i++){
69                         grow(row);
70                         for (int j = 0; j < value.getNumColumns(); j++){
71                                 String header = value.getHeaderColumn(j); 
72                                 addAt(header, value.get(header,i), row);
73                         }
74                         row++;
75                 }
76                 Vector rowVector = (Vector) matrix.get(row);
77                 rowVector.add(value);
78         }
79         
80         /**
81          * Converts the StringMatrix into a copy of the given
82          * @param origin - The StringMatrix to be copied
83          */
84         public void copy(StringMatrix origin) {
85                 clear();
86                 add(origin);
87         }
88         
89         /**
90          * Clears (emtpies) the StringMatrix
91          */
92         public void clear(){
93                 header.clear();
94                 matrix.clear();
95         }
96         /**
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 
100         */
101         public void addAt(String key, String value, int row) {
102                 grow(row);
103                 Vector rowVector = (Vector) matrix.get(row);
104                 int ind = header.indexOf(key);
105                 if (ind < 0) return;
106                 if (rowVector.size() < ind+1) rowVector.setSize(ind);
107                 rowVector.add(ind, value);
108         }
109         /**
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 
113          */
114         private void addVector(Vector value, int row){
115                 grow(row);
116                 Vector rowVector = (Vector) matrix.get(row);
117                 rowVector.addAll(value);
118         }
119         
120         /**
121          * Tells if you have that particular key in your StringMatrix
122          * @param key
123          * @return 
124          */
125         public boolean contains(String key){
126                 return (findKey(key) >= 0);
127         }
128         /**
129          * Gets a String value from the row indicated, from the column that matches the key
130          * @param key
131          * @param row
132          * @return
133          */
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);
141         }
142         /**
143          * Finds the key in the header
144          * @param key
145          * @return
146          */
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;
151                 }
152                 return -1;
153         }
154         /**
155          * @param key: selects the column
156          * @return a Vector with all the values in the selected column; null if empty
157          */
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));
163                 }
164                 return result;
165         }
166         /**
167          * @param key: selects the column
168          * @return a Vector with all the values in the selected column, dropping duplicates; null if empty
169          */
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));
175                 }
176                 return result;
177         }
178         /**
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
182          */
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));
188                 }
189                 return result;
190         }
191         /**
192          * Deletes all the rows that matches the value for the key
193          * @param key: selects the column
194          */
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);
198                 }
199         }
200         /**
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
205          */
206         public StringMatrix select(String key, String value){
207                 StringMatrix result = new StringMatrix();
208                 result.addVectorHeader(header);
209                 int j = 0;
210                 for (int i = 0; i < size(); i++){
211                         if (get(key, i).equals(value)) {
212                                 result.addVector((Vector)matrix.get(i), j);
213                                 j++;
214                         } 
215                 }
216                 return result;
217         }
218
219         // Private functions
220         /**
221          * Grows the StringMatrix till it's able to hold "row" rows.
222          * @param row : The number of rows that must hold
223          */
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));
228                         }
229         }
230         /**
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
234          * @return
235          */
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);
241         }
242         
243         // Generic interfaces
244         /**
245          * @param i
246          * @return : a StringMatrix with only one row, the selected by i
247          */
248         public StringMatrix rowMatrix(int i){
249                 StringMatrix result = new StringMatrix();
250                 result.addVectorHeader(header);
251                 result.addVector((Vector)matrix.get(i),0);
252                 return result;
253         }
254         
255         /**
256          * Resturns a String[] with the selected row
257          * @param i The index of the row
258          * @return
259          */
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); 
265                         return result;
266                 }
267                 return null;
268                 
269         }
270
271
272         /**
273          * @return the number of rows
274          */
275         public int size() {
276                 return matrix.size();
277         }
278         public int getNumColumns() {
279                 return header.size();
280         }
281         public String getHeaderColumn(int i){
282                 return (String) header.get(i);
283         }
284         public void deleteRow(int i){
285                 matrix.remove(i);
286         }
287
288
289         // Common Object interface
290         /* (non-Javadoc)
291          * @see java.lang.Object#clone()
292          */
293         public Object clone() {
294                 // Deep clone
295                 StringMatrix matrix = new StringMatrix();
296                 matrix.copy(this);
297                 return matrix;
298         }
299
300         /* (non-Javadoc)
301          * @see java.lang.Object#toString()
302          */
303         public String toString() {
304                 String result = "";
305                 for (int j = 0; j < header.size(); j++){
306                         result += (String) header.get(j);
307                         result += "\t";
308                 }
309                 result += "\n";
310                 // Write the lines
311                 for (int i = 0; i < matrix.size(); i++){
312                         for (int j = 0; j < header.size(); j++){
313                                 result += get(j,i);
314                                 result += "\t";
315                         }
316                         result += "\n";
317                 }
318                 return result;
319         }
320
321         /* (non-Javadoc)
322          * @see java.lang.Object#equals(java.lang.Object)
323          */
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;
333                 }
334                 return true;
335         }
336
337         /**
338          * @return
339          */
340         public Vector getMatrix() {
341                 return matrix;
342         }
343         /**
344          * @return
345          */
346         public String[] getHeader() {
347                 String[] result = new String[header.size()];
348                 header.toArray(result);
349                 return result;
350         }
351
352 }