在JAVA实现DataTable对象(二)——DatColumn和DataRow对象实现

原文链接:http://www.cnblogs.com/hjzhang/archive/2011/05/21/2052787.html
   1:  public class DataColumn {
   2:      private boolean readOnly; // 只读
   3:   
   4:      private DataTable table; //dataTable的引用
   5:   
   6:      private String columnName; //列名
   7:   
   8:      private String captionName; //显示名称
   9:   
  10:      private int columnIndex;//列索引
  11:   
  12:      private int dataType;//列数据类型
  13:   
  14:      private String dataTypeName;//数据类型名称
  15:   
  16:      public DataColumn() {
  17:          this("default1");
  18:      }
  19:   
  20:      public DataColumn(int dataType) {
  21:          this("default1", dataType);
  22:      }
  23:   
  24:      public DataColumn(String columnName) {
  25:          this(columnName, 0);
  26:      }
  27:   
  28:      public DataColumn(String columnName, int dataType) {
  29:          this.setDataType(dataType);
  30:          this.columnName = columnName;
  31:      }
  32:   
  33:      public String getColumnName() {
  34:          return this.columnName;
  35:      }
  36:   
  37:      public void setColumnName(String columnName) {
  38:          this.columnName = columnName;
  39:      }
  40:   
  41:      public String getCaptionName() {
  42:          return captionName;
  43:      }
  44:   
  45:      public void setCaptionName(String captionName) {
  46:          this.captionName = captionName;
  47:      }
  48:   
  49:      public boolean isReadOnly() {
  50:          return this.readOnly;
  51:      }
  52:   
  53:      public void setReadOnly(boolean readOnly) {
  54:          this.readOnly = readOnly;
  55:      }
  56:   
  57:      public DataTable getTable() {
  58:          return this.table;
  59:      }
  60:   
  61:      public void setTable(DataTable table) {
  62:          this.table = table;
  63:      }
  64:   
  65:      /**  
  66:       * @param dataType
  67:       */
  68:      public void setDataType(int dataType) {
  69:          this.dataType = dataType;
  70:      }
  71:   
  72:      /**  
  73:       * @return  the dataType   
  74:      */
  75:      public int getDataType() {
  76:          return dataType;
  77:      }
  78:   
  79:      /**  
  80:       * @param columnIndex
  81:       */
  82:      public void setColumnIndex(int columnIndex) {
  83:          this.columnIndex = columnIndex;
  84:      }
  85:   
  86:      /**  
  87:       * @return  the columnIndex   
  88:      */
  89:      public int getColumnIndex() {
  90:          return columnIndex;
  91:      }
  92:   
  93:      public String getDataTypeName() {
  94:          return DataTypes.getDataTypeName(dataType);
  95:      }
  96:   
  97:      /**  
  98:       * 功能描述:  将输入数据转为当前列的数据类型返回
  99:       * @param

 103:       */
 104:      public Object convertTo(Object value) {
 105:          return value;
 106:      }
 107:      
 108:      @Override
 109:      public String toString(){
 110:          return this.columnName;
 111:      }
 112:  }
   
   1:  import java.util.LinkedHashMap;
   2:  import java.util.Map;
   3:   
   4:  public class DataRow {
   5:      //定义该行记录在table所处的行数
   6:      private int rowIndex = -1; 
   7:      private DataColumnCollection columns;
   8:      //table的一个引用
   9:      private DataTable table; 
  10:      //用于存储数据的Map对象,这里保存的对象不包括顺序信息,数据获取的索引通过行信息标识
  11:      private Map<String, Object> itemMap = new LinkedHashMap<String, Object>();
  12:   
  13:      public DataRow() {
  14:   
  15:      }
  16:   
  17:      public DataRow(DataTable table) {
  18:          this.table = table;
  19:      }
  20:   
  21:      /**  
  22:       * 功能描述:  获取当前行的行索引
  23:       * @param
  24:       * @return: int    
  25:       */
  26:      public int getRowIndex() {
  27:          return rowIndex;
  28:      }
  29:   
  30:      /**  
  31:       * 功能描述:  获取当前行所属数据表对象
  32:       * @param
  33:       * @return: DataTable    
  34:       */
  35:      public DataTable getTable() {
  36:          return this.table;
  37:      }
  38:   
  39:      /**  
  40:       * @param columns
  41:       */
  42:      public void setColumns(DataColumnCollection columns) {
  43:          this.columns = columns;
  44:      }
  45:   
  46:      /**  
  47:       * @return  the columns   
  48:      */
  49:      public DataColumnCollection getColumns() {
  50:          return columns;
  51:      }
  52:   
  53:      public void setValue(int index,
  54:              Object value) {
  55:          setValue(this.columns.get(index), value);
  56:      }
  57:   
  58:      public void setValue(String columnName,
  59:              Object value) {
  60:          setValue(this.columns.get(columnName), value);
  61:      }
  62:   
  63:      public void setValue(DataColumn column,
  64:              Object value) {
  65:          if (column != null) {
  66:              String lowerColumnName = column.getColumnName().toLowerCase();
  67:              if (getItemMap().containsKey(lowerColumnName))
  68:                  getItemMap().remove(lowerColumnName);
  69:              getItemMap().put(lowerColumnName, column.convertTo(value));
  70:          }
  71:      }
  72:   
  73:      public Object getValue(int index) {
  74:          String colName = this.columns.get(index).getColumnName().toLowerCase();
  75:          return this.getItemMap().get(colName);
  76:      }
  77:   
  78:      public Object getValue(String columnName) {
  79:          return this.getItemMap().get(columnName.toLowerCase());
  80:      }
  81:   
  82:      /**  
  83:       * @return  the itemMap   
  84:      */
  85:      public Map<String, Object> getItemMap() {
  86:          return itemMap;
  87:      }
  88:   
  89:      /**  
  90:       * @param rowIndex
  91:       */
  92:      public void setRowIndex(int rowIndex) {
  93:          this.rowIndex = rowIndex;
  94:      }
  95:   
  96:      public void copyFrom(DataRow row) {
  97:          this.itemMap.clear();//首先请客当前记录
  98:          for (Object c : this.columns) {
  99:              this.itemMap.put(c.toString().toLowerCase(), row.getValue(c.toString()));
 100:          }
 101:      }
 102:  }

转载于:https://www.cnblogs.com/hjzhang/archive/2011/05/21/2052787.html

上一篇:DataTable.Select("Distinct") Implementation (Citation)


下一篇:C# DataTable的詳細用法