| 1 package com.daffodilwoods.daffodildb.utils; 2 3 import com.daffodilwoods.daffodildb.server.datasystem.interfaces.Datatype; 4 import com.daffodilwoods.database.resource.DException; 5 import com.daffodilwoods.daffodildb.utils.comparator.*; 6 import com.daffodilwoods.daffodildb.utils.byteconverter.CCzufDpowfsufs; 7 import com.daffodilwoods.daffodildb.utils.field.*; 8 import java.util.GregorianCalendar ; 9 import java.util.Calendar ; 10 import java.util.Date ; 11 import java.text.ParseException ; 12 import java.text.ParsePosition ; 13 import java.sql.Time ; 14 import java.sql.Timestamp ; 15 16 25 public class TypeValidityHandler { 26 static final FieldBase byte_Min = new FieldByte(new BufferRange(CCzufDpowfsufs. 27 getBytes(new Byte (Byte.MIN_VALUE))), Datatype.BYTE); 28 static final FieldBase byte_Max = new FieldByte(new BufferRange(CCzufDpowfsufs. 29 getBytes(new Byte (Byte.MAX_VALUE))), Datatype.BYTE); 30 static final FieldBase short_Min = new FieldShort(new BufferRange( 31 CCzufDpowfsufs.getBytes(new Short (Short.MIN_VALUE))), Datatype.SHORT); 32 static final FieldBase short_Max = new FieldShort(new BufferRange( 33 CCzufDpowfsufs.getBytes(new Short (Short.MAX_VALUE))), Datatype.SHORT); 34 static final FieldBase integer_Min = new FieldInteger(new BufferRange( 35 CCzufDpowfsufs.getBytes(new Integer (Integer.MIN_VALUE))), Datatype.INTEGER); 36 static final FieldBase integer_Max = new FieldInteger(new BufferRange( 37 CCzufDpowfsufs.getBytes(new Integer (Integer.MAX_VALUE))), Datatype.INTEGER); 38 static final FieldBase long_Min = new FieldLong(new BufferRange(CCzufDpowfsufs. 39 getBytes(new Long (Long.MIN_VALUE))), Datatype.LONG); 40 static final FieldBase long_Max = new FieldLong(new BufferRange(CCzufDpowfsufs. 41 getBytes(new Long (Long.MAX_VALUE))), Datatype.LONG); 42 static final FieldBase double_Min = new FieldDouble(new BufferRange( 43 CCzufDpowfsufs.getBytes(new Double (Double.NEGATIVE_INFINITY))), 44 Datatype.DOUBLE); 45 static final FieldBase double_Max = new FieldDouble(new BufferRange( 46 CCzufDpowfsufs.getBytes(new Double (Double.POSITIVE_INFINITY))), 47 Datatype.DOUBLE); 48 static final FieldBase float_Min = new FieldReal(new BufferRange( 49 CCzufDpowfsufs.getBytes(new Float (Float.NEGATIVE_INFINITY))), 50 Datatype.REAL); 51 static final FieldBase float_Max = new FieldReal(new BufferRange( 52 CCzufDpowfsufs.getBytes(new Float (Float.POSITIVE_INFINITY))), 53 Datatype.REAL); 54 55 static COpoEfdjnbmOpoEfdjnbmDpnqbsbups nonDecimalNonDecimalComparator = new 56 COpoEfdjnbmOpoEfdjnbmDpnqbsbups(); 57 static CTbnfUzqfDpnqbsbups sameComparator = new CTbnfUzqfDpnqbsbups(); 58 static CPckfduDpnqbsbups objectComparator = new CPckfduDpnqbsbups(); 59 static CGmpbuOpoEfdjnbmDpnqbsbups floatNonDecimalComparator = new 60 CGmpbuOpoEfdjnbmDpnqbsbups(); 61 static CEpvcmfOpoEfdjnbmDpnqbsbups doubleNonDecimalComparator = new 62 CEpvcmfOpoEfdjnbmDpnqbsbups(); 63 static CEpvcmfGmpbuDpnqbsbups doubleFloatComparator = new 64 CEpvcmfGmpbuDpnqbsbups(); 65 static CSfwfstfDpnqbsbups floatDoubleComparator = new CSfwfstfDpnqbsbups( 66 doubleFloatComparator); 67 static CSfwfstfDpnqbsbups nonDecimalFloatComparator = new CSfwfstfDpnqbsbups( 68 floatNonDecimalComparator); 69 static CSfwfstfDpnqbsbups nonDecimalDoubleComparator = new CSfwfstfDpnqbsbups( 70 doubleNonDecimalComparator); 71 72 static java.text.SimpleDateFormat dateFormat = new java.text.SimpleDateFormat ( 73 "yyyy-M-d"); 74 static java.text.SimpleDateFormat timeFormat = new java.text.SimpleDateFormat ( 75 "H:m:s"); 76 static java.text.SimpleDateFormat timestampFormat = new java.text. 77 SimpleDateFormat("yyyy-M-d H:m:s.S"); 78 static java.text.SimpleDateFormat timestampFormat1 = new java.text. 79 SimpleDateFormat("yyyy-M-d H:m:s"); 80 81 static final Calendar cal = new GregorianCalendar (); 82 83 static { 84 dateFormat.setLenient(false); 85 timeFormat.setLenient(false); 86 timestampFormat.setLenient(false); 87 timestampFormat1.setLenient(false); 88 } 89 90 public static void checkByteWithNonDecimal(FieldBase object) throws 91 DException { 92 if (nonDecimalNonDecimalComparator.compare( (_DComparator) byte_Min, 93 (_DComparator) object) > 0 94 || 95 nonDecimalNonDecimalComparator.compare( (_DComparator) object, 96 (_DComparator) byte_Max) > 0) 97 throw new DException("DSE5545", new Object [] {object.getObject(), "Byte"}); 98 } 99 100 public static void checkByteWithDouble(FieldBase object) throws DException { 101 if (nonDecimalDoubleComparator.compare( (_DComparator) byte_Min, 102 (_DComparator) object) > 0 103 || 104 doubleNonDecimalComparator.compare( (_DComparator) object, 105 (_DComparator) byte_Max) > 0) 106 throw new DException("DSE5545", new Object [] {object.getObject(), "Byte"}); 107 } 108 109 public static void checkByteWithFloat(FieldBase object) throws DException { 110 if (nonDecimalFloatComparator.compare( (_DComparator) byte_Min, 111 (_DComparator) object) > 0 112 || 113 floatNonDecimalComparator.compare( (_DComparator) object, 114 (_DComparator) byte_Max) > 0) 115 throw new DException("DSE5545", new Object [] {object.getObject(), "Byte"}); 116 } 117 118 public static void checkByteWithBigDecimal(FieldBase object) throws 119 DException { 120 if (objectComparator.compare( (_DComparator) byte_Min, 121 (_DComparator) object) > 0 122 || 123 objectComparator.compare( (_DComparator) object, (_DComparator) byte_Max) > 124 0) 125 throw new DException("DSE5545", new Object [] {object.getObject(), "Byte"}); 126 } 127 128 public static void checkShortWithNonDecimal(FieldBase object) throws 129 DException { 130 if (nonDecimalNonDecimalComparator.compare( (_DComparator) short_Min, 131 (_DComparator) object) > 0 132 || 133 nonDecimalNonDecimalComparator.compare( (_DComparator) object, 134 (_DComparator) short_Max) > 0) 135 throw new DException("DSE5545", new Object [] {object.getObject(), "Short"}); 136 } 137 138 public static void checkShortWithDouble(FieldBase object) throws DException { 139 if (nonDecimalDoubleComparator.compare( (_DComparator) short_Min, 140 (_DComparator) object) > 0 141 || 142 doubleNonDecimalComparator.compare( (_DComparator) object, 143 (_DComparator) short_Max) > 0) 144 throw new DException("DSE5545", new Object [] {object.getObject(), "Short"}); 145 } 146 147 public static void checkShortWithFloat(FieldBase object) throws DException { 148 if (nonDecimalFloatComparator.compare( (_DComparator) short_Min, 149 (_DComparator) object) > 0 150 || 151 floatNonDecimalComparator.compare( (_DComparator) object, 152 (_DComparator) short_Max) > 0) 153 throw new DException("DSE5545", new Object [] {object.getObject(), "Short"}); 154 } 155 156 public static void checkShortWithBigDecimal(FieldBase object) throws 157 DException { 158 if (objectComparator.compare( (_DComparator) short_Min, 159 (_DComparator) object) > 0 160 || 161 objectComparator.compare( (_DComparator) object, (_DComparator) short_Max) > 162 0) 163 throw new DException("DSE5545", new Object [] {object.getObject(), "Short"}); 164 } 165 166 public static void checkIntegerWithNonDecimal(FieldBase object) throws 167 DException { 168 if (nonDecimalNonDecimalComparator.compare( (_DComparator) integer_Min, 169 object) > 0 170 || 171 nonDecimalNonDecimalComparator.compare( (_DComparator) object, 172 (_DComparator) integer_Max) > 0) 173 throw new DException("DSE5545", new Object [] {object.getObject(), 174 "Integer"}); 175 } 176 177 public static void checkIntegerWithDouble(FieldBase object) throws DException { 178 if (nonDecimalDoubleComparator.compare( (_DComparator) integer_Min, 179 (_DComparator) object) > 0 180 || 181 doubleNonDecimalComparator.compare( (_DComparator) object, 182 (_DComparator) integer_Max) > 0) 183 throw new DException("DSE5545", new Object [] {object.getObject(), 184 "Integer"}); 185 } 186 187 public static void checkIntegerWithFloat(FieldBase object) throws DException { 188 if (nonDecimalFloatComparator.compare( (_DComparator) integer_Min, 189 (_DComparator) object) > 0 190 || 191 floatNonDecimalComparator.compare( (_DComparator) object, 192 (_DComparator) integer_Max) > 0) 193 throw new DException("DSE5545", new Object [] {object.getObject(), 194 "Integer"}); 195 } 196 197 public static void checkIntegerWithBigDecimal(FieldBase object) throws 198 DException { 199 if (objectComparator.compare( (_DComparator) integer_Min, 200 (_DComparator) object) > 0 201 || 202 objectComparator.compare( (_DComparator) object, (_DComparator) integer_Max) > 203 0) 204 throw new DException("DSE5545", new Object [] {object.getObject(), 205 "Integer"}); 206 } 207 208 public static void checkLongWithNonDecimal(FieldBase object) throws 209 DException { 210 if (nonDecimalNonDecimalComparator.compare( (_DComparator) long_Min, 211 (_DComparator) object) > 0 212 || 213 nonDecimalNonDecimalComparator.compare( (_DComparator) object, 214 (_DComparator) long_Max) > 0) 215 throw new DException("DSE5545", new Object [] {object.getObject(), "Long"}); 216 } 217 218 public static void checkLongWithDouble(FieldBase object) throws DException { 219 if (nonDecimalDoubleComparator.compare( (_DComparator) long_Min, 220 (_DComparator) object) > 0 221 || 222 doubleNonDecimalComparator.compare( (_DComparator) object, 223 (_DComparator) long_Max) > 0) 224 throw new DException("DSE5545", new Object [] {object.getObject(), "Long"}); 225 } 226 227 public static void checkLongWithFloat(FieldBase object) throws DException { 228 if (nonDecimalFloatComparator.compare( (_DComparator) long_Min, 229 (_DComparator) object) > 0 230 || 231 floatNonDecimalComparator.compare( (_DComparator) object, 232 (_DComparator) long_Max) > 0) 233 throw new DException("DSE5545", new Object [] {object.getObject(), "Long"}); 234 } 235 236 public static void checkLongWithBigDecimal(FieldBase object) throws 237 DException { 238 if (objectComparator.compare( (_DComparator) long_Min, 239 (_DComparator) object) > 0 240 || 241 objectComparator.compare( (_DComparator) object, (_DComparator) long_Max) > 242 0) 243 throw new DException("DSE5545", new Object [] {object.getObject(), "Long"}); 244 } 245 246 public static void checkDoubleWithFloat(FieldBase object) throws DException { 247 if (doubleFloatComparator.compare( (_DComparator) double_Min, 248 (_DComparator) object) > 0 249 || 250 floatDoubleComparator.compare( (_DComparator) object, (_DComparator) double_Max) > 251 0) 252 throw new DException("DSE5545", new Object [] {object.getObject(), 253 "Double"}); 254 } 255 256 public static void checkDoubleWithBigDecimal(FieldBase object) throws 257 DException { 258 if (objectComparator.compare( (_DComparator) double_Min, 259 (_DComparator) object) > 0 260 || 261 objectComparator.compare( (_DComparator) object, (_DComparator) double_Max) > 262 0) { 263 throw new DException("DSE5545", new Object [] {object.getObject(), 264 "Double"}); 265 } 266 } 267 268 public static void checkFloatWithDouble(FieldBase object) throws DException { 269 if (floatDoubleComparator.compare( (_DComparator) float_Min, 270 (_DComparator) object) > 0 271 || 272 doubleFloatComparator.compare( (_DComparator) object, (_DComparator) float_Max) > 273 0) 274 throw new DException("DSE5545", new Object [] {object.getObject(), "Float"}); 275 } 276 277 public static void checkFloatWithBigDecimal(FieldBase object) throws 278 DException { 279 if (objectComparator.compare( (_DComparator) float_Min, 280 (_DComparator) object) > 0 281 || 282 objectComparator.compare( (_DComparator) object, (_DComparator) float_Max) > 283 0) 284 throw new DException("DSE5545", new Object [] {object.getObject(), "Float"}); 285 } 286 287 public static DBDate getDBDate(Timestamp timestamp) throws DException { 288 cal.setTime(timestamp); 289 cal.set(Calendar.HOUR, 0); 290 cal.set(Calendar.MINUTE, 0); 291 cal.set(Calendar.SECOND, 0); 292 return new DBDate(cal.getTime().getTime()); 293 } 294 295 public static DBDate getDBDateFromDateTimestamp(String dateTimestamp) throws 296 DException { 297 checkYearFromDate(dateTimestamp); 298 try { 299 return new DBDate(getParsedDate(dateTimestamp).getTime()); 300 } 301 catch (DException ex1) { 302 try { 303 return getDBDate(getTimestamp(dateTimestamp)); 304 } 305 catch (DException ex2) { 306 throw new DException("DSE8024", null); 307 } 308 } 309 } 310 311 public static Time getTime(Timestamp timestamp) throws DException { 312 cal.setTime(timestamp); 313 cal.set(Calendar.YEAR, 0); 314 cal.set(Calendar.MONTH, 0); 315 cal.set(Calendar.DATE, 0); 316 return new Time (cal.getTime().getTime()); 317 } 318 319 public static Time getTimeFromTimeTimeStamp(String timeTimestamp) throws 320 DException { 321 try { 322 return getTime(timeTimestamp); 323 } 324 catch (DException ex1) { 325 try { 326 return getTime(getTimestamp(timeTimestamp)); 327 } 328 catch (DException ex2) { 329 throw new DException("DSE8023", null); 330 } 331 } 332 } 333 334 public static Timestamp getTimeStampFromTimestampDate(String dateTimestamp) throws 335 DException { 336 checkYearFromTimestamp(dateTimestamp); 337 if (dateTimestamp.indexOf(".") != -1) { 338 if (dateTimestamp.length() > 23) { 339 throw new DException("DSE8022", null); 340 } 341 try { 342 return getJDKCompatibleTimestamp(timestampFormat.parse(dateTimestamp). 343 getTime()); 344 } 345 catch (ParseException ex) { 346 throw new DException("DSE8022", null); 347 } 348 } 349 else { 350 352 if (dateTimestamp.length() > 19) { 353 throw new DException("DSE8022", null); 354 } 355 356 try { 357 return getJDKCompatibleTimestamp(timestampFormat1.parse(dateTimestamp). 358 getTime()); 359 } 360 catch (ParseException ex1) { 361 try { 362 Timestamp ts = getJDKCompatibleTimestamp(getParsedDate(dateTimestamp). 363 getTime()); 364 return ts; 365 } 366 catch (DException ex) { 367 throw new DException("DSE8022", null); 368 } 369 } 370 } 371 } 372 373 379 private static Date getParsedDate(String dateValue) throws DException { 380 try { 381 ParsePosition pp = new ParsePosition (0); 382 Date date = dateFormat.parse(dateValue, pp); 383 if (pp.getIndex() != dateValue.length()) 384 throw new DException("DSE8024", null); 385 return date; 386 } 387 catch (Exception ex2) { 388 throw new DException("DSE8024", null); 389 } 390 } 391 392 public static Timestamp getTimestamp(String timestamp) throws DException { 393 checkYearFromTimestamp(timestamp); 394 try { 395 if (timestamp.indexOf(".") != -1) { 396 if (timestamp.length() > 23) { 397 throw new DException("DSE8022", null); 398 } 399 return getJDKCompatibleTimestamp(timestampFormat.parse(timestamp). 400 getTime()); 401 } 402 else { 403 if (timestamp.length() > 19) { 404 throw new DException("DSE8022", null); 405 } 406 return getJDKCompatibleTimestamp(timestampFormat1.parse(timestamp). 407 getTime()); 408 } 409 } 410 catch (ParseException ex1) { 411 throw new DException("DSE8022", null); 412 } 413 } 414 415 423 private static Timestamp getJDKCompatibleTimestamp(long 424 timestampWithNanoSeconds) throws DException { 425 Timestamp ts = new Timestamp (timestampWithNanoSeconds); 426 String javaVersion = System.getProperty("java.runtime.version").substring(0, 427 3); 428 try { 429 float version = Float.parseFloat(javaVersion); 430 if (version >= 1.4f) 431 return ts; 432 DTimeStamp dtimestamp = new DTimeStamp(ts.getTime() + 433 ts.getNanos() / 1000000); 434 return dtimestamp; 435 } 436 catch (NumberFormatException ex) { 437 return ts; 438 } 439 } 440 441 455 456 public static Time getTime(String time) throws DException { 457 try { 458 return new Time (timeFormat.parse(time).getTime()); 459 } 460 catch (ParseException ex) { 461 throw new DException("DSE8023", null); 462 } 463 } 464 465 public static DBDate getDBDate(String date) throws DException { 466 checkYearFromDate(date); 467 return new DBDate(getParsedDate(date).getTime()); 468 } 469 470 476 private static void checkYearFromDate(String date) throws DException { 477 if (date.indexOf("-") > 8 || date.equalsIgnoreCase("")) 478 throw new DException("DSE8024", null); 479 } 480 481 487 private static void checkYearFromTimestamp(String timestamp) throws 488 DException { 489 if (timestamp.indexOf("-") > 8 || timestamp.equalsIgnoreCase("")) 490 throw new DException("DSE8022", null); 491 } 492 493 499 } 500 | Popular Tags |