A data matrix implementation 2 博客分类: OO* JavaSQLXML
程序员文章站
2024-03-07 12:21:15
...
We could add more functionalities, but in separate classes.
1. We may add a class for the following operations: sorting, querying, and merging.
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).
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.
1. We may add a class for the following operations: sorting, querying, and merging.
java 代码
- import java.util.List;
- public class DataMatrixRowManipulator
- {
- public DataMatrix query(String queryString, DataMatrix dataMatrix)
- {
- return null;
- }
- public DataMatrix sortByColumn(int col)
- {
- return null;
- }
- public DataMatrix sortByField(Field field)
- {
- return null;
- }
- public List selectRow(DataMatrixRowKey rowKey, DataMatrix dataMatrix)
- {
- return null;
- }
- public DataMatrix mergeOnKey(DataMatrixRowKey rowKey, DataMatrix dataMatrix, DataMatrix anotherDataMatrix)
- {
- return null;
- }
- }
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 代码
- import java.util.Set;
- import java.util.Collection;
- import java.util.HashSet;
- import java.util.Iterator;
- public class DataMatrixRowKey
- {
- private Set keyFieldSet;
- public DataMatrixRowKey(Collection keyFieldSet)
- {
- this.keyFieldSet = new HashSet(keyFieldSet);
- }
- public boolean equals(Object obj)
- {
- // equals only when all fields are the same.
- if (!(obj instanceof DataMatrixRowKey)) return false;
- DataMatrixRowKey key = (DataMatrixRowKey)obj;
- if (key.keyFieldSet.size() != this.keyFieldSet.size()) return false;
- for (Iterator it=keyFieldSet.iterator(); it.hasNext(); )
- {
- if (!key.keyFieldSet.contains(it.next())) return false;
- }
- return true;
- }
- public int hashCode()
- {
- return keyFieldSet.hashCode();
- }
- }
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.