A data matrix implementation 2

We could add more functionalities, but in separate classes.

1. We may add a class for the following operations: sorting, querying, and merging.

java 代码
 
  1. import java.util.List;  
  2.   
  3. public class DataMatrixRowManipulator  
  4. {  
  5.     public DataMatrix query(String queryString, DataMatrix dataMatrix)  
  6.     {  
  7.         return null;  
  8.     }  
  9.   
  10.     public DataMatrix sortByColumn(int col)  
  11.     {  
  12.         return null;  
  13.     }  
  14.   
  15.     public DataMatrix sortByField(Field field)  
  16.     {  
  17.         return null;  
  18.     }  
  19.   
  20.     public List selectRow(DataMatrixRowKey rowKey, DataMatrix dataMatrix)  
  21.     {  
  22.         return null;  
  23.     }  
  24.   
  25.     public DataMatrix mergeOnKey(DataMatrixRowKey rowKey, DataMatrix dataMatrix, DataMatrix anotherDataMatrix)  
  26.     {  
  27.         return null;  
  28.     }  
  29. }  

Here the class DataMatrixRowKey is to guaranty the uniqueness of each row for the selection and merge purpose(This is corresponding to the composite keys).

java 代码
 
  1. import java.util.Set;  
  2. import java.util.Collection;  
  3. import java.util.HashSet;  
  4. import java.util.Iterator;  
  5.   
  6. public class DataMatrixRowKey  
  7. {  
  8.     private Set keyFieldSet;  
  9.   
  10.     public DataMatrixRowKey(Collection keyFieldSet)  
  11.     {  
  12.         this.keyFieldSet = new HashSet(keyFieldSet);  
  13.     }  
  14.   
  15.     public boolean equals(Object obj)  
  16.     {  
  17.         // equals only when all fields are the same.  
  18.         if (!(obj instanceof DataMatrixRowKey)) return false;  
  19.   
  20.         DataMatrixRowKey key = (DataMatrixRowKey)obj;  
  21.   
  22.         if (key.keyFieldSet.size() != this.keyFieldSet.size()) return false;  
  23.   
  24.         for (Iterator it=keyFieldSet.iterator(); it.hasNext(); )  
  25.         {  
  26.             if (!key.keyFieldSet.contains(it.next())) return false;  
  27.         }  
  28.   
  29.         return true;  
  30.     }  
  31.   
  32.     public int hashCode()  
  33.     {  
  34.         return keyFieldSet.hashCode();  
  35.     }  
  36. }  

And again, these operations don't use the specific knowledge of Field. The query operation could be complicated, in order to support a search language similar to SQL, we may use a language parser.

2. We could have a separate converter to convert a data matrix to XML, CSV, etc formats and vice versa.

3. Number crunching, e.g., the sum of all cells in a column, truncate float/double to int/long or to certain digits after period, or even a formula that computes from other columns. The formula parsing could get complicated when the Field class has more data fields, even with the help of ANTLR or JavaCC.

4. The hard problem is the equals() method since it has different meanings in different contexts. One way is that two data matrix are equal only when they match cell by cell, another way is by rows and ignore orders, and so on.

你可能感兴趣的:(java,sql,xml)