litesoft
@ 851
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 |
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt package org.litesoft.core.util; import java.sql.*; import java.util.Date; import org.litesoft.core.simpletypes.*; import org.litesoft.core.simpletypes.currency.*; import org.litesoft.core.simpletypes.temporal.*; import org.litesoft.core.simpletypes.temporal.nonpublic.*; import org.litesoft.core.typeutils.*; public class TypeConverter { public static String to_String( Object pObject ) { return (pObject == null) ? null : pObject.toString(); } public static Boolean to_Boolean( Object pObject ) { if ( null == pObject ) { return null; } if ( pObject instanceof Boolean ) { return (Boolean) pObject; } Boolean rv = Booleans.fromString( pObject.toString() ); if ( rv != null ) { return rv; } throw new IllegalArgumentException( "Unable to convert to Boolean: " + pObject ); } public static boolean to_boolean( Object pObject ) { Boolean b = to_Boolean( pObject ); return (b != null) && b; } public static Long to_Long( Object pObject ) { Number zNumber = toNumber( pObject ); return (null == zNumber) ? null : zNumber.longValue(); } public static long to_long( Object pObject ) { Number zNumber = toNumber( pObject ); return (null == zNumber) ? 0 : zNumber.longValue(); } public static Integer to_Integer( Object pObject ) { Number zNumber = toNumber( pObject ); return (null == zNumber) ? null : zNumber.intValue(); } public static int to_int( Object pObject ) { Number zNumber = toNumber( pObject ); return (null == zNumber) ? 0 : zNumber.intValue(); } public static Short to_Short( Object pObject ) { Number zNumber = toNumber( pObject ); return (null == zNumber) ? null : zNumber.shortValue(); } public static short to_short( Object pObject ) { Number zNumber = toNumber( pObject ); return (null == zNumber) ? 0 : zNumber.shortValue(); } public static Double to_Double( Object pObject ) { Number zNumber = toNumber( pObject ); return (null == zNumber) ? null : zNumber.doubleValue(); } public static double to_double( Object pObject ) { Number zNumber = toNumber( pObject ); return (null == zNumber) ? 0 : zNumber.doubleValue(); } public static Float to_Float( Object pObject ) { Number zNumber = toNumber( pObject ); return (null == zNumber) ? null : zNumber.floatValue(); } public static float to_float( Object pObject ) { Number zNumber = toNumber( pObject ); return (null == zNumber) ? 0 : zNumber.floatValue(); } public static java.sql.Timestamp to_Timestamp( Object pObject ) { if ( null == pObject ) { return null; } if ( pObject instanceof java.sql.Timestamp ) { return (java.sql.Timestamp) pObject; } return new java.sql.Timestamp( to_UtilDate( pObject ).getTime() ); } public static java.sql.Time to_SqlTime( Object pObject ) { if ( null == pObject ) { return null; } if ( pObject instanceof java.sql.Time ) { return (java.sql.Time) pObject; } SimpleTime time = to_SimpleTime( TimeRes.ToSEC, pObject ); return new java.sql.Time( time.getSqlTimeOffset() ); } public static java.sql.Date to_SqlDate( Object pObject ) { if ( null == pObject ) { return null; } if ( pObject instanceof java.sql.Date ) { return (java.sql.Date) pObject; } SimpleDate date = to_SimpleDate( null, pObject ); return to_SqlDate( date ); } public static java.sql.Date to_SqlDate( SimpleDate pDate ) { return new java.sql.Date( pDate.toUtilDate().getTime() ); } public static Date to_UtilDate( Object pObject ) { if ( null == pObject ) { return null; } if ( pObject instanceof Date ) { return (Date) pObject; } if ( pObject instanceof Number ) { return new Date( ((Number) pObject).longValue() ); } if ( pObject instanceof SimpleDate ) { return ((SimpleDate) pObject).toUtilDate(); } if ( pObject instanceof SimpleTimestamp ) { return ((SimpleTimestamp) pObject).getDate(); } if ( pObject instanceof SimpleTime ) { return new Date( ((SimpleTime) pObject).getSqlTimeOffset() ); } String s = pObject.toString(); try { return new Date( Long.parseLong( s ) ); } catch ( NumberFormatException e ) { // Ignore } try { return UtilDateAdaptor.fromUtilDateToString( s ).getWallDate(); } catch ( IllegalArgumentException e ) { // Ignore } try { return new Date( java.sql.Date.valueOf( s ).getTime() ); } catch ( IllegalArgumentException e ) { // Ignore } try { return new Date( java.sql.Time.valueOf( s ).getTime() ); } catch ( IllegalArgumentException e ) { // Ignore } return SimpleTimestamp.fromString( s ).getDate(); } public static byte[] to_bytes( Object pObject ) { if ( null == pObject ) { return null; } if ( pObject instanceof byte[] ) { return (byte[]) pObject; } throw new IllegalArgumentException( "Unable to convert to byte[]: " + pObject ); } private static Number toNumber( Object pObject ) { if ( (pObject == null) || (pObject instanceof Number) ) { return (Number) pObject; } String s = pObject.toString().trim(); if ( s.length() == 0 ) { return null; } try { return Long.parseLong( s ); } catch ( NumberFormatException e ) { // Ignore } return Double.parseDouble( s ); // Will blow up... } public static SimpleMoney to_SimpleMoney( SimpleCurrency pSC, Object pObject ) { Objects.assertNotNull( "SimpleCurrency", pSC ); if ( null == pObject ) { return null; } if ( pObject instanceof SimpleMoney ) { SimpleMoney zMoney = (SimpleMoney) pObject; SimpleCurrency zNewSC = zMoney.getCurrency(); if ( zNewSC.equals( pSC ) ) { return zMoney; } throw new IllegalArgumentException( "Currency convertion ('" + zNewSC + "'->'" + pSC + "') is not supported" ); } return SimpleMoney.fromString( pSC, pObject.toString() ); } public static SimpleFixedPointLong to_SimpleFixedPointLong( int pDecimalPlaces, // NumericFormatControl pFormatControl, Object pObject ) { if ( pDecimalPlaces < 0 ) { throw new IllegalArgumentException( "Decimal places not allowed to be negative" ); } Objects.assertNotNull( "NumericFormatControl", pFormatControl ); if ( null == pObject ) { return null; } if ( pObject instanceof SimpleFixedPointLong ) { return SimpleFixedPointLong.getInstance( pDecimalPlaces, pFormatControl, (SimpleFixedPointLong) pObject ); } return SimpleFixedPointLong.fromString( pDecimalPlaces, pFormatControl, pObject.toString() ); } public static SimpleFloatingPointLong to_SimpleFloatingPointLong( NumericFormatControl pFormatControl, Object pObject ) { Objects.assertNotNull( "NumericFormatControl", pFormatControl ); if ( null == pObject ) { return null; } if ( pObject instanceof SimpleFloatingPointLong ) { return SimpleFloatingPointLong.getInstance( pFormatControl, (SimpleFloatingPointLong) pObject ); } return SimpleFloatingPointLong.fromString( pFormatControl, pObject.toString() ); } public static SimpleTime to_SimpleTime( Object pObject ) { return to_SimpleTime( TimeRes.ToMIN, pObject ); } public static SimpleTime to_SimpleTime( TimeRes pTimeRes, Object pObject ) { Objects.assertNotNull( "TimeRes", pTimeRes ); if ( null == pObject ) { return null; } SimpleTime temporal = (pObject instanceof SimpleTime) ? // (SimpleTime) pObject : // SimpleTime.fromString( pObject.toString() ); if ( pTimeRes.getIndex() == temporal.getResolutionIndex() ) { return temporal; } return temporal.copyWithUpdatedResolution( pTimeRes ); } public static SimpleTimestamp to_SimpleTimestamp( Object pObject ) { return to_SimpleTimestamp( TimeRes.ToMSEC, pObject ); } public static SimpleTimestamp to_SimpleTimestamp( TimeRes pTimeRes, Object pObject ) { Objects.assertNotNull( "TimeRes", pTimeRes ); if ( null == pObject ) { return null; } if ( !(pObject instanceof SimpleTimestamp) ) { Date date = to_UtilDate( pObject ); return new SimpleTimestamp( pTimeRes, date ); } SimpleTimestamp temporal = (SimpleTimestamp) pObject; if ( pTimeRes.getIndex() == temporal.getResolutionIndex() ) { return temporal; } return temporal.copyWithUpdatedResolution( pTimeRes ); } public static SimpleDate to_SimpleDate( String pFormat, Object pObject ) { if ( null == pObject ) { return null; } // TODO: XXX // if ( pObject instanceof SimpleDate ) // { // SimpleDate temporal = (SimpleDate) pObject; // if ( temporal.getFormat().equals( pFormat ) ) // { // return temporal; // } // // Fall thru to 'toString' version... // } if ( pObject instanceof Time ) { throw new IllegalArgumentException( "No convertion supported from SQL Time to Simple Date" ); } // TODO: XXX // if ( pObject instanceof Date ) // { // return new SimpleDate( pFormat, (Date) pObject ); // } // if ( pObject instanceof Number ) // { // return new SimpleDate( pFormat, new Date( ((Number) pObject).longValue() ) ); // } // if ( pObject instanceof SimpleTimestamp ) // { // return new SimpleDate( pFormat, ((SimpleTimestamp) pObject).getDate() ); // } String s = pObject.toString(); // TODO: XXX // try // { // return new SimpleDate( pFormat, new Date( Long.parseLong( s ) ) ); // } // catch ( NumberFormatException e ) // { // // Ignore // } // try // { // return new SimpleDate( pFormat, UtilDateAdaptor.fromUtilDateToString( s ).getWallDate() ); // } // catch ( IllegalArgumentException e ) // { // // Ignore // } // try // { // return new SimpleDate( pFormat, java.sql.Date.valueOf( s ) ); // } // catch ( IllegalArgumentException e ) // { // // Ignore // } // try // { // return SimpleDate.fromYMD( pFormat, s ); // } // catch ( IllegalArgumentException e ) // { // // Ignore // } // try // { // return new SimpleDate( pFormat, SimpleTimestamp.fromString( s ).getDate() ); // } // catch ( IllegalArgumentException e ) // { // // Ignore // } TemporalParser parser = new TemporalParser( new DateFormat( pFormat ).getFieldOrder() ); return parser.parseDate( pFormat, s, null ); } public static TextLines to_TextLines( Object pObject ) { if ( null == pObject ) { return null; } if ( pObject instanceof TextLines ) { return (TextLines) pObject; } if ( pObject instanceof TextLine ) { return new TextLines( (TextLine) pObject ); } if ( pObject instanceof String[] ) { return new TextLines( (String[]) pObject ); } if ( pObject instanceof TextLine[] ) { return new TextLines( (TextLine[]) pObject ); } if ( pObject instanceof Object[] ) { return new TextLines( Strings.toArray( (Object[]) pObject ) ); } if ( pObject instanceof java.util.Collection ) { return new TextLines( Strings.toArray( (java.util.Collection) pObject ) ); } return new TextLines( Strings.splitLines( pObject.toString() ) ); } } |
Commits for litesoft/trunk/Java/core/Anywhere/src/org/litesoft/core/util/TypeConverter.java
Revision | Author | Commited | Message |
---|---|---|---|
851 Diff | GeorgeS | Mon 08 Oct, 2012 00:05:32 +0000 | Breaking the code as Temporal changes are implemented... |
821 Diff | GeorgeS | Sun 19 Aug, 2012 00:08:41 +0000 | |
819 Diff | GeorgeS | Sat 18 Aug, 2012 18:09:40 +0000 | |
804 Diff | GeorgeS | Wed 15 Aug, 2012 12:48:51 +0000 | |
198 Diff | GeorgeS | Fri 06 May, 2011 23:15:51 +0000 | |
49 Diff | GeorgeS | Mon 12 Apr, 2010 02:59:10 +0000 | License Text |
2 | GeorgeS | Sun 07 Feb, 2010 12:50:58 +0000 |