1 21 22 package org.dbunit.dataset; 23 24 import org.dbunit.DatabaseUnitRuntimeException; 25 import org.dbunit.dataset.datatype.DataType; 26 27 import java.util.Arrays ; 28 import java.util.Comparator ; 29 30 38 public class SortedTable extends AbstractTable 39 { 40 private final ITable _table; 41 private final Column[] _columns; 42 private Integer [] _indexes; 43 44 47 public SortedTable(ITable table, Column[] columns) 48 { 49 _table = table; 50 _columns = columns; 51 } 52 53 56 public SortedTable(ITable table, String [] columnNames) throws DataSetException 57 { 58 _table = table; 59 _columns = new Column[columnNames.length]; 60 61 Column[] columns = table.getTableMetaData().getColumns(); 62 for (int i = 0; i < columnNames.length; i++) 63 { 64 String columnName = columnNames[i]; 65 _columns[i] = DataSetUtils.getColumn(columnName, columns); 66 } 67 } 68 69 73 public SortedTable(ITable table, ITableMetaData metaData) throws DataSetException 74 { 75 this(table, metaData.getColumns()); 76 } 77 78 82 public SortedTable(ITable table) throws DataSetException 83 { 84 this(table, table.getTableMetaData()); 85 } 86 87 private int getOriginalRowIndex(int row) throws DataSetException 88 { 89 if (_indexes == null) 90 { 91 Integer [] indexes = new Integer [getRowCount()]; 92 for (int i = 0; i < indexes.length; i++) 93 { 94 indexes[i] = new Integer (i); 95 } 96 97 try 98 { 99 Arrays.sort(indexes, new RowComparator()); 100 } 101 catch (DatabaseUnitRuntimeException e) 102 { 103 throw (DataSetException)e.getException(); 104 } 105 106 _indexes = indexes; 107 } 108 109 return _indexes[row].intValue(); 110 } 111 112 115 public ITableMetaData getTableMetaData() 116 { 117 return _table.getTableMetaData(); 118 } 119 120 public int getRowCount() 121 { 122 return _table.getRowCount(); 123 } 124 125 public Object getValue(int row, String column) throws DataSetException 126 { 127 assertValidRowIndex(row); 128 129 return _table.getValue(getOriginalRowIndex(row), column); 130 } 131 132 135 private class RowComparator implements Comparator 136 { 137 public int compare(Object o1, Object o2) 138 { 139 Integer i1 = (Integer )o1; 140 Integer i2 = (Integer )o2; 141 142 try 143 { 144 for (int i = 0; i < _columns.length; i++) 145 { 146 String columnName = _columns[i].getColumnName(); 147 Object value1 = _table.getValue(i1.intValue(), columnName); 148 Object value2 = _table.getValue(i2.intValue(), columnName); 149 150 if (value1 == null && value2 == null) 151 { 152 continue; 153 } 154 155 if (value1 == null && value2 != null) 156 { 157 return -1; 158 } 159 160 if (value1 != null && value2 == null) 161 { 162 return 1; 163 } 164 165 String stringValue1 = DataType.asString(value1); 166 String stringValue2 = DataType.asString(value2); 167 int result = stringValue1.compareTo(stringValue2); 168 if (result != 0) 169 { 170 return result; 171 } 172 } 173 } 174 catch (DataSetException e) 175 { 176 throw new DatabaseUnitRuntimeException(e); 177 } 178 179 return 0; 180 } 181 } 182 } 183 184 185 186 187 188 | Popular Tags |