Coverage Summary for Class: JavaType (net.sf.persism)

Class Class, % Method, % Line, %
JavaType 100% (1/1) 100% (9/9) 100% (86/86)


1 package net.sf.persism; 2  3 import java.lang.reflect.Array; 4 import java.math.BigDecimal; 5 import java.math.BigInteger; 6 import java.sql.Blob; 7 import java.sql.Clob; 8 import java.sql.Time; 9 import java.sql.Timestamp; 10 import java.time.LocalTime; 11 import java.util.UUID; 12  13 /** 14  * Type mapper used for fast switch statements 15  * 16  * @author Dan Howard 17  * @since 10/8/11 5:36 PM 18  */ 19 enum JavaType { 20  21  booleanType(boolean.class), 22  BooleanType(Boolean.class), 23  byteType(byte.class), 24  ByteType(Byte.class), 25  shortType(short.class), 26  ShortType(Short.class), 27  integerType(int.class), 28  IntegerType(Integer.class), 29  longType(long.class), 30  LongType(Long.class), 31  floatType(float.class), 32  FloatType(Float.class), 33  doubleType(double.class), 34  DoubleType(Double.class), 35  BigDecimalType(BigDecimal.class), 36  BigIntegerType(BigInteger.class), 37  StringType(String.class), 38  characterType(char.class), 39  CharacterType(Character.class), 40  UtilDateType(java.util.Date.class), 41  SQLDateType(java.sql.Date.class), 42  TimeType(Time.class), 43  LocalTimeType(LocalTime.class), 44  TimestampType(Timestamp.class), 45  LocalDateType(java.time.LocalDate.class), 46  LocalDateTimeType(java.time.LocalDateTime.class), 47  InstantType(java.time.Instant.class), 48  OffsetDateTimeType(java.time.OffsetDateTime.class), 49  ZonedDateTimeType(java.time.ZonedDateTime.class), 50  byteArrayType(byte[].class), 51  ByteArrayType(Byte[].class), 52  ClobType(Clob.class), 53  BlobType(Blob.class), 54  EnumType(Enum.class), 55  UUIDType(UUID.class), 56  ObjectType(Object.class); 57  58  private static final Log log = Log.getLogger(JavaType.class); 59  60  private Class<?> type; 61  62  <T> JavaType(Class<T> type) { 63  init(type); 64  } 65  66  private <T> void init(Class<T> type) { 67  this.type = type; 68  } 69  70  public static <T> JavaType getType(Class<T> type) { 71  if (type.isEnum()) { 72  return EnumType; 73  } 74  75  for (JavaType t : values()) { 76  if (t.type.equals(type)) { 77  return t; 78  } 79  } 80  return null; 81  } 82  83  public static JavaType convert(int sqlType) { 84  JavaType result = null; 85  86  switch (sqlType) { 87  case java.sql.Types.CHAR: 88  case java.sql.Types.NCHAR: 89  case java.sql.Types.VARCHAR: 90  case java.sql.Types.NVARCHAR: 91  case java.sql.Types.LONGVARCHAR: 92  case java.sql.Types.LONGNVARCHAR: 93  result = StringType; 94  break; 95  96  case java.sql.Types.NUMERIC: 97  case java.sql.Types.DECIMAL: 98  result = BigDecimalType; 99  break; 100  101  case java.sql.Types.BIT: 102  case java.sql.Types.BOOLEAN: 103  result = BooleanType; 104  break; 105  106  case java.sql.Types.TINYINT: 107  result = ByteType; 108  break; 109  110  case java.sql.Types.SMALLINT: 111  result = ShortType; 112  break; 113  114  case java.sql.Types.INTEGER: 115  result = IntegerType; 116  break; 117  118  case java.sql.Types.BIGINT: 119  result = LongType; 120  break; 121  122  case java.sql.Types.FLOAT: 123  result = FloatType; 124  break; 125  126  case java.sql.Types.DOUBLE: 127  case java.sql.Types.REAL: 128  result = DoubleType; 129  break; 130  131  case java.sql.Types.BINARY: 132  case java.sql.Types.VARBINARY: 133  result = ByteArrayType; 134  break; 135  136  case java.sql.Types.LONGVARBINARY: 137  case java.sql.Types.BLOB: 138  result = BlobType; 139  break; 140  141  case java.sql.Types.DATE: 142  result = SQLDateType; 143  break; 144  145  case java.sql.Types.TIME: 146  result = TimeType; 147  break; 148  149  case java.sql.Types.TIMESTAMP: 150  result = TimestampType; 151  break; 152  153  case java.sql.Types.CLOB: 154  case java.sql.Types.NCLOB: 155  result = ClobType; 156  break; 157  158  case java.sql.Types.OTHER: 159  result = ObjectType; 160  } 161  162  if (result == null) { 163  log.warnNoDuplicates(Message.UnknownSQLType.message(sqlType)); 164  } 165  166  return result; 167  } 168  169  public Class<?> getJavaType() { 170  return type; 171  } 172  173  public boolean isEligibleForAutoinc() { 174  // Oracle returns BigDecimalType for INT 175  // PUBS has SmallInt -> short 176  return this == IntegerType || this == integerType || this == LongType || this == longType 177  || this == ShortType || this == shortType || this == BigDecimalType || this == BigIntegerType; 178  } 179  180  // https://stackoverflow.com/questions/2891970/getting-default-value-for-primitive-types 181  public static <T> T getDefaultValue(Class<T> clazz) { 182  return (T) Array.get(Array.newInstance(clazz, 1), 0); 183  } 184 } 185  186