1 21 22 package org.dbunit.dataset.datatype; 23 24 import java.sql.PreparedStatement ; 25 import java.sql.ResultSet ; 26 import java.sql.SQLException ; 27 28 33 public abstract class AbstractDataType extends DataType 34 { 35 private final String _name; 36 private final int _sqlType; 37 private final Class _classType; 38 private final boolean _isNumber; 39 40 public AbstractDataType(String name, int sqlType, Class classType, 41 boolean isNumber) 42 { 43 _sqlType = sqlType; 44 _name = name; 45 _classType = classType; 46 _isNumber = isNumber; 47 } 48 49 52 public int compare(Object o1, Object o2) throws TypeCastException 53 { 54 try 55 { 56 Comparable value1 = (Comparable )typeCast(o1); 57 Comparable value2 = (Comparable )typeCast(o2); 58 59 if (value1 == null && value2 == null) 60 { 61 return 0; 62 } 63 64 if (value1 == null && value2 != null) 65 { 66 return -1; 67 } 68 69 if (value1 != null && value2 == null) 70 { 71 return 1; 72 } 73 74 return value1.compareTo(value2); 75 } 76 catch (ClassCastException e) 77 { 78 throw new TypeCastException(e); 79 } 80 } 81 82 public int getSqlType() 83 { 84 return _sqlType; 85 } 86 87 public Class getTypeClass() 88 { 89 return _classType; 90 } 91 92 public boolean isNumber() 93 { 94 return _isNumber; 95 } 96 97 public boolean isDateTime() 98 { 99 return false; 100 } 101 102 public Object getSqlValue(int column, ResultSet resultSet) 103 throws SQLException , TypeCastException 104 { 105 Object value = resultSet.getObject(column); 106 if (value == null || resultSet.wasNull()) 107 { 108 return null; 109 } 110 return value; 111 } 112 113 public void setSqlValue(Object value, int column, PreparedStatement statement) 114 throws SQLException , TypeCastException 115 { 116 statement.setObject(column, typeCast(value), getSqlType()); 117 } 118 119 122 public String toString() 123 { 124 return _name; 125 } 126 } 127 128 129 130 | Popular Tags |