public static String[] typeNames( Type[] types ) { String[] names = new String[ types.length ]; for( int i = 0; i < types.length; i++ ) names[ i ] = getTypeName( types[ i ] ); return names; }
@Override public String toString() { final StringBuilder sb = new StringBuilder(); sb.append( "Column" ); sb.append( "{name='" ).append( name ).append( '\'' ); sb.append( ", type=" ).append( Util.getTypeName( type ) ); sb.append( '}' ); return sb.toString(); } }
private String getSafeMessage( Object object, int i ) { try { return "field " + sourceFields.get( i ) + " cannot be coerced from : " + object + " to: " + Util.getTypeName( types[ i ] ); } catch( Throwable throwable ) { // you may get an exception while composing the message (e.g. ArrayIndexOutOfBoundsException) // use a generic string return "field pos " + i + " cannot be coerced from: " + object + ", pos has no corresponding field name or coercion type"; } }
private static CoercibleType getInstance( Class<CoercibleType> typeClass ) { try { return typeClass.newInstance(); } catch( Exception exception ) { throw new CascadeException( "unable to instantiate class: " + Util.getTypeName( typeClass ) ); } }
@Override public Long canonical( Object value ) { if( value == null ) return null; Class from = value.getClass(); if( from == String.class ) return parse( (String) value ).getTime(); if( from == Date.class ) return ( (Date) value ).getTime(); // in UTC if( from == Long.class || from == long.class ) return (Long) value; throw new CascadingException( "unknown type coercion requested from: " + Util.getTypeName( from ) ); }
@Override public Object coerce( Object value, Type to ) { if( value == null ) return null; Class from = value.getClass(); if( from != Long.class ) throw new IllegalStateException( "was not normalized" ); // no coercion, or already in canonical form if( to == Long.class || to == long.class || to == Object.class || DateType.class == to.getClass() ) return value; if( to == String.class ) { Calendar calendar = getCalendar(); calendar.setTimeInMillis( (Long) value ); return getDateFormat().format( calendar.getTime() ); } throw new CascadingException( "unknown type coercion requested, from: " + Util.getTypeName( from ) + " to: " + Util.getTypeName( to ) ); }
@Override public Object canonical( Object value ) { if( value == null ) return null; Class from = value.getClass(); if( from == String.class ) return (int) ( parse( (String) value ).getTimeValue() ); if( Date.class.isAssignableFrom( from ) ) return (int) ( ( (Date) value ).getTime() % MILLIS_PER_DAY ); // in UTC if( from == Integer.class || from == int.class ) return value; if( from == Long.class || from == long.class ) return ( (Long) value ).intValue(); throw new CascadingException( "unknown type coercion requested from: " + Util.getTypeName( from ) ); }
@Override public Object canonical( Object value ) { if( value == null ) return null; Class from = value.getClass(); if( from == String.class ) return parse( (String) value ).getJdbcTimestamp( DateTimeUtil.defaultZone ); if( Date.class.isAssignableFrom( from ) ) return ( (Date) value ).getTime(); // in UTC if( from == Long.class || from == long.class ) return value; throw new CascadingException( "unknown type coercion requested from: " + Util.getTypeName( from ) ); }
@Override public Object canonical( Object value ) { if( value == null ) return null; Class from = value.getClass(); if( from == String.class ) { Calendar calendar = DateTimeUtil.parseDateFormat( (String) value, DateTimeUtil.DateFormatStr, DateTimeUtil.defaultZone ); return truncateDate( calendar.getTimeInMillis() ); } if( Date.class.isAssignableFrom( from ) ) { Date fromDate = (Date) value; return truncateDate( fromDate.getTime() ); } if( from == Integer.class || from == int.class ) return value; if( from == Long.class || from == long.class ) return ( (Long) value ).intValue(); throw new CascadingException( "unknown type coercion requested from: " + Util.getTypeName( from ) ); }
@Override public Object coerce( Object value, Type to ) { if( value == null ) return null; Class from = value.getClass(); if( from != Integer.class ) throw new IllegalStateException( "was not normalized" ); // no coercion, or already in canonical form if( to == Integer.class || to == int.class || to == Object.class ) return value; if( to == Long.class ) return ( (Integer) value ).intValue(); // offset by the current timezone so we get only yyyy-mm-hh precision per the spec for DATE // Even with a "cast" of DATE to TIMESTAMP the value should be considered "yyyy-mm-hh 00:00:00" Calendar calendar = Calendar.getInstance(); int timezoneOffset = calendar.get( Calendar.ZONE_OFFSET ) + calendar.get( Calendar.DST_OFFSET ); long shiftedTime = ( ( (Integer) value ).longValue() * MILLIS_PER_DAY ) + timezoneOffset; ZonelessDatetime date = createInstance(); date.setZonedTime( shiftedTime, DateTimeUtil.defaultZone ); if( to == String.class ) return date.toString(); if( to == java.sql.Date.class ) return new java.sql.Date( date.getJdbcDate( DateTimeUtil.defaultZone ) ); if( to == java.sql.Timestamp.class ) return new java.sql.Timestamp( date.getJdbcTimestamp( DateTimeUtil.defaultZone ) ); if( to == java.sql.Time.class ) return new java.sql.Time( date.getJdbcTime( DateTimeUtil.defaultZone ) ); throw new CascadingException( "unknown type coercion requested, from: " + Util.getTypeName( from ) + " to: " + Util.getTypeName( to ) ); }
@Override public Object coerce( Object value, Type to ) { if( value == null ) return null; Class from = value.getClass(); if( from != Long.class ) throw new IllegalStateException( "was not normalized" ); // TIMESTAMP has no timezone precision so set this for the current timezone long timezoneOffset = TimeZone.getDefault().getOffset( (Long) value ); value = ( (Long) value ) + timezoneOffset; // no coercion, or already in canonical form if( to == Long.class || to == long.class || to == Object.class ) return value; if( to == Integer.class ) return ( (Long) value ).intValue(); ZonelessDatetime date = createInstance(); date.setZonelessTime( (Long) value ); if( to == String.class ) return date.toString(); if( to == java.sql.Date.class ) return new java.sql.Date( date.getJdbcDate( DateTimeUtil.defaultZone ) ); if( to == java.sql.Timestamp.class ) return new java.sql.Timestamp( date.getJdbcTimestamp( DateTimeUtil.defaultZone ) ); if( to == java.sql.Time.class ) return new java.sql.Time( date.getJdbcTime( DateTimeUtil.defaultZone ) ); throw new CascadingException( "unknown type coercion requested, from: " + Util.getTypeName( from ) + " to: " + Util.getTypeName( to ) ); }
@Override public Object coerce( Object value, Type to ) { if( value == null ) return null; Class from = value.getClass(); if( from != Integer.class ) throw new IllegalStateException( "was not normalized" ); // no coercion, or already in canonical form if( to == Integer.class || to == int.class || to == Object.class ) return value; if( to == Long.class ) return ( (Integer) value ).intValue(); ZonelessDatetime date = createInstance(); date.setZonelessTime( (Integer) value % MILLIS_PER_DAY ); if( to == String.class ) return date.toString(); if( to == java.sql.Date.class ) return new java.sql.Date( date.getJdbcDate( DateTimeUtil.defaultZone ) ); if( to == java.sql.Timestamp.class ) return new java.sql.Timestamp( date.getJdbcTimestamp( DateTimeUtil.defaultZone ) ); if( to == java.sql.Time.class ) return new java.sql.Time( date.getJdbcTime( DateTimeUtil.defaultZone ) ); throw new CascadingException( "unknown type coercion requested, from: " + Util.getTypeName( from ) + " to: " + Util.getTypeName( to ) ); } }
return (Coerce) convert( value, (Class) to ); throw new CascadingException( "unknown type coercion requested, from: " + Util.getTypeName( from ) + " to: " + Util.getTypeName( to ) );