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.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值