Java Code Examples for org.hibernate.type.descriptor.WrapperOptions

The following examples show how to use org.hibernate.type.descriptor.WrapperOptions. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: lams   Source File: LongTypeDescriptor.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public <X> Long wrap(X value, WrapperOptions options) {
	if ( value == null ) {
		return null;
	}
	if ( Long.class.isInstance( value ) ) {
		return (Long) value;
	}
	if ( Number.class.isInstance( value ) ) {
		return ( (Number) value ).longValue();
	}
	else if ( String.class.isInstance( value ) ) {
		return Long.valueOf( ( (String) value ) );
	}
	throw unknownWrap( value.getClass() );
}
 
Example 2
@SuppressWarnings({ "unchecked" })
public <X> X unwrap(char[] value, Class<X> type, WrapperOptions options) {
	if ( value == null ) {
		return null;
	}
	if ( char[].class.isAssignableFrom( type ) ) {
		return (X) value;
	}
	if ( String.class.isAssignableFrom( type ) ) {
		return (X) new String( value );
	}
	if ( Clob.class.isAssignableFrom( type ) ) {
		return (X) options.getLobCreator().createClob( new String( value ) );
	}
	if ( Reader.class.isAssignableFrom( type ) ) {
		return (X) new StringReader( new String( value ) );
	}
	if ( CharacterStream.class.isAssignableFrom( type ) ) {
		return (X) new CharacterStreamImpl( new String( value ) );
	}
	throw unknownUnwrap( type );
}
 
Example 3
Source Project: lams   Source File: BooleanTypeDescriptor.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public <X> Boolean wrap(X value, WrapperOptions options) {
	if ( value == null ) {
		return null;
	}
	if ( Boolean.class.isInstance( value ) ) {
		return (Boolean) value;
	}
	if ( Number.class.isInstance( value ) ) {
		final int intValue = ( (Number) value ).intValue();
		return intValue == 0 ? FALSE : TRUE;
	}
	if ( Character.class.isInstance( value ) ) {
		return isTrue( (Character) value ) ? TRUE : FALSE;
	}
	if ( String.class.isInstance( value ) ) {
		return isTrue((String) value) ? TRUE : FALSE;
	}
	throw unknownWrap( value.getClass() );
}
 
Example 4
Source Project: lams   Source File: ShortTypeDescriptor.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public <X> Short wrap(X value, WrapperOptions options) {
	if ( value == null ) {
		return null;
	}
	if ( Short.class.isInstance( value ) ) {
		return (Short) value;
	}
	if ( Number.class.isInstance( value ) ) {
		return ( (Number) value ).shortValue();
	}
	if ( String.class.isInstance( value ) ) {
		return Short.valueOf( ( (String) value ) );
	}
	throw unknownWrap( value.getClass() );
}
 
Example 5
@SuppressWarnings({ "unchecked" })
@Override
public <X> X unwrap(Character[] value, Class<X> type, WrapperOptions options) {
	if ( value == null ) {
		return null;
	}
	if ( Character[].class.isAssignableFrom( type ) ) {
		return (X) value;
	}
	if ( String.class.isAssignableFrom( type ) ) {
		return (X) new String( unwrapChars( value ) );
	}
	if ( Clob.class.isAssignableFrom( type ) ) {
		return (X) options.getLobCreator().createClob( new String( unwrapChars( value ) ) );
	}
	if ( Reader.class.isAssignableFrom( type ) ) {
		return (X) new StringReader( new String( unwrapChars( value ) ) );
	}
	if ( CharacterStream.class.isAssignableFrom( type ) ) {
		return (X) new CharacterStreamImpl( new String( unwrapChars( value ) ) );
	}
	throw unknownUnwrap( type );
}
 
Example 6
@Override
public <X> Character[] wrap(X value, WrapperOptions options) {
	if ( value == null ) {
		return null;
	}
	if ( Character[].class.isInstance( value ) ) {
		return (Character[]) value;
	}
	if ( String.class.isInstance( value ) ) {
		return wrapChars( ( (String) value ).toCharArray() );
	}
	if ( Clob.class.isInstance( value ) ) {
		return wrapChars( DataHelper.extractString( ( (Clob) value ) ).toCharArray() );
	}
	if ( Reader.class.isInstance( value ) ) {
		return wrapChars( DataHelper.extractString( (Reader) value ).toCharArray() );
	}
	throw unknownWrap( value.getClass() );
}
 
Example 7
Source Project: lams   Source File: JdbcDateTypeDescriptor.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public <X> Date wrap(X value, WrapperOptions options) {
	if ( value == null ) {
		return null;
	}
	if ( java.sql.Date.class.isInstance( value ) ) {
		return (Date) value;
	}

	if ( Long.class.isInstance( value ) ) {
		return new java.sql.Date( ( (Long) value ).longValue() );
	}

	if ( Calendar.class.isInstance( value ) ) {
		return new java.sql.Date( ( (Calendar) value ).getTimeInMillis() );
	}

	if ( Date.class.isInstance( value ) ) {
		return new java.sql.Date( ( (Date) value ).getTime() );
	}

	throw unknownWrap( value.getClass() );
}
 
Example 8
public <X> char[] wrap(X value, WrapperOptions options) {
	if ( value == null ) {
		return null;
	}
	if ( char[].class.isInstance( value ) ) {
		return (char[]) value;
	}
	if ( String.class.isInstance( value ) ) {
		return ( (String) value ).toCharArray();
	}
	if ( Clob.class.isInstance( value ) ) {
		return DataHelper.extractString( ( (Clob) value ) ).toCharArray();
	}
	if ( Reader.class.isInstance( value ) ) {
		return DataHelper.extractString( ( (Reader) value ) ).toCharArray();
	}
	throw unknownWrap( value.getClass() );
}
 
Example 9
@SuppressWarnings({ "unchecked" })
public <X> X unwrap(Calendar value, Class<X> type, WrapperOptions options) {
	if ( value == null ) {
		return null;
	}
	if ( Calendar.class.isAssignableFrom( type ) ) {
		return (X) value;
	}
	if ( java.sql.Date.class.isAssignableFrom( type ) ) {
		return (X) new java.sql.Date( value.getTimeInMillis() );
	}
	if ( java.sql.Time.class.isAssignableFrom( type ) ) {
		return (X) new java.sql.Time( value.getTimeInMillis() );
	}
	if ( java.sql.Timestamp.class.isAssignableFrom( type ) ) {
		return (X) new java.sql.Timestamp( value.getTimeInMillis() );
	}
	if ( Date.class.isAssignableFrom( type ) ) {
		return (X) new  Date( value.getTimeInMillis() );
	}
	throw unknownUnwrap( type );
}
 
Example 10
Source Project: hibernate-types   Source File: YearMonthTypeDescriptor.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public <X> YearMonth wrap(X value, WrapperOptions options) {
    if (value == null) {
        return null;
    }
    if (value instanceof String) {
        return fromString((String) value);
    }
    if (value instanceof Number) {
        int numericValue = ((Number) (value)).intValue();
        int year = numericValue / 100;
        int month = numericValue % 100;
        return YearMonth.of(year, month);
    }
    if (value instanceof Date) {
        Date date = (Date) value;
        return YearMonth.from(Instant.ofEpochMilli(date.getTime())
                .atZone(ZoneId.systemDefault())
                .toLocalDate());
    }
    throw unknownWrap(value.getClass());
}
 
Example 11
Source Project: lams   Source File: ByteArrayTypeDescriptor.java    License: GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings({ "unchecked" })
@Override
public <X> X unwrap(Byte[] value, Class<X> type, WrapperOptions options) {
	if ( value == null ) {
		return null;
	}
	if ( Byte[].class.isAssignableFrom( type ) ) {
		return (X) value;
	}
	if ( byte[].class.isAssignableFrom( type ) ) {
		return (X) unwrapBytes( value );
	}
	if ( InputStream.class.isAssignableFrom( type ) ) {
		return (X) new ByteArrayInputStream( unwrapBytes( value ) );
	}
	if ( BinaryStream.class.isAssignableFrom( type ) ) {
		return (X) new BinaryStreamImpl( unwrapBytes( value ) );
	}
	if ( Blob.class.isAssignableFrom( type ) ) {
		return (X) options.getLobCreator().createBlob( unwrapBytes( value ) );
	}

	throw unknownUnwrap( type );
}
 
Example 12
Source Project: lams   Source File: UUIDTypeDescriptor.java    License: GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings({ "unchecked" })
public <X> X unwrap(UUID value, Class<X> type, WrapperOptions options) {
	if ( value == null ) {
		return null;
	}
	if ( UUID.class.isAssignableFrom( type ) ) {
		return (X) PassThroughTransformer.INSTANCE.transform( value );
	}
	if ( String.class.isAssignableFrom( type ) ) {
		return (X) ToStringTransformer.INSTANCE.transform( value );
	}
	if ( byte[].class.isAssignableFrom( type ) ) {
		return (X) ToBytesTransformer.INSTANCE.transform( value );
	}
	throw unknownUnwrap( type );
}
 
Example 13
Source Project: hibernate-types   Source File: YearMonthTypeDescriptor.java    License: Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({"unchecked"})
@Override
public <X> X unwrap(YearMonth value, Class<X> type, WrapperOptions options) {
    if (value == null) {
        return null;
    }
    if (String.class.isAssignableFrom(type)) {
        return (X) toString(value);
    }
    if (Number.class.isAssignableFrom(type)) {
        Integer numericValue = (value.getYear() * 100) + value.getMonth().getValue();
        return (X) (numericValue);
    }
    if (Timestamp.class.isAssignableFrom(type)) {
        return (X) java.sql.Timestamp.valueOf(value.atDay(1).atStartOfDay());
    }
    if (Date.class.isAssignableFrom(type)) {
        return (X) java.sql.Date.valueOf(value.atDay(1));
    }
    throw unknownUnwrap(type);
}
 
Example 14
@Override
public <X> Date wrap(X value, WrapperOptions options) {
	if ( value == null ) {
		return null;
	}
	if ( Timestamp.class.isInstance( value ) ) {
		return (Timestamp) value;
	}

	if ( Long.class.isInstance( value ) ) {
		return new Timestamp( (Long) value );
	}

	if ( Calendar.class.isInstance( value ) ) {
		return new Timestamp( ( (Calendar) value ).getTimeInMillis() );
	}

	if ( Date.class.isInstance( value ) ) {
		return new Timestamp( ( (Date) value ).getTime() );
	}

	throw unknownWrap( value.getClass() );
}
 
Example 15
@Override
@SuppressWarnings("unchecked")
public <X> T wrap(
        X value,
        WrapperOptions options
) {
    if (value instanceof Array) {
        Array array = (Array) value;
        try {
            return ArrayUtil.unwrapArray(
                    (Object[]) array.getArray(),
                    arrayObjectClass
            );
        } catch (SQLException e) {
            throw new IllegalArgumentException(e);
        }
    }
    return (T) value;
}
 
Example 16
Source Project: lams   Source File: DurationJavaDescriptor.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public <X> Duration wrap(X value, WrapperOptions options) {
	if ( value == null ) {
		return null;
	}

	if ( Duration.class.isInstance( value ) ) {
		return (Duration) value;
	}

	if ( Long.class.isInstance( value ) ) {
		return Duration.ofNanos( (Long) value );
	}

	if ( String.class.isInstance( value ) ) {
		return Duration.parse( (String) value );
	}

	throw unknownWrap( value.getClass() );
}
 
Example 17
Source Project: lams   Source File: BlobTypeDescriptor.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public <X> Blob wrap(X value, WrapperOptions options) {
	if ( value == null ) {
		return null;
	}

	// Support multiple return types from
	// org.hibernate.type.descriptor.sql.BlobTypeDescriptor
	if ( Blob.class.isAssignableFrom( value.getClass() ) ) {
		return options.getLobCreator().wrap( (Blob) value );
	}
	else if ( byte[].class.isAssignableFrom( value.getClass() ) ) {
		return options.getLobCreator().createBlob( ( byte[] ) value);
	}
	else if ( InputStream.class.isAssignableFrom( value.getClass() ) ) {
		InputStream inputStream = ( InputStream ) value;
		try {
			return options.getLobCreator().createBlob( inputStream, inputStream.available() );
		}
		catch ( IOException e ) {
			throw unknownWrap( value.getClass() );
		}
	}

	throw unknownWrap( value.getClass() );
}
 
Example 18
@SuppressWarnings({ "unchecked" })
public <X> X unwrap(byte[] value, Class<X> type, WrapperOptions options) {
	if ( value == null ) {
		return null;
	}
	if ( byte[].class.isAssignableFrom( type ) ) {
		return (X) value;
	}
	if ( InputStream.class.isAssignableFrom( type ) ) {
		return (X) new ByteArrayInputStream( value );
	}
	if ( BinaryStream.class.isAssignableFrom( type ) ) {
		return (X) new BinaryStreamImpl( value );
	}
	if ( Blob.class.isAssignableFrom( type ) ) {
		return (X) options.getLobCreator().createBlob( value );
	}

	throw unknownUnwrap( type );
}
 
Example 19
Source Project: lams   Source File: BasicExtractor.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public J extract(CallableStatement statement, int index, WrapperOptions options) throws SQLException {
	final J value = doExtract( statement, index, options );
	final boolean traceEnabled = log.isTraceEnabled();
	if ( value == null || statement.wasNull() ) {
		if ( traceEnabled ) {
			log.tracef(
					"extracted procedure output  parameter ([%s] : [%s]) - [null]",
					index,
					JdbcTypeNameMapper.getTypeName( getSqlDescriptor().getSqlType() )
			);
		}
		return null;
	}
	else {
		if ( traceEnabled ) {
			log.tracef(
					"extracted procedure output  parameter ([%s] : [%s]) - [%s]",
					index,
					JdbcTypeNameMapper.getTypeName( getSqlDescriptor().getSqlType() ),
					getJavaDescriptor().extractLoggableRepresentation( value )
			);
		}
		return value;
	}
}
 
Example 20
Source Project: hibernate-types   Source File: JsonNodeTypeDescriptor.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"unchecked"})
@Override
public <X> X unwrap(JsonNode value, Class<X> type, WrapperOptions options) {
    if (value == null) {
        return null;
    }
    if (String.class.isAssignableFrom(type)) {
        return (X) toString(value);
    }
    if (JsonNode.class.isAssignableFrom(type)) {
        return (X) objectMapperWrapper.toJsonNode(toString(value));
    }
    throw unknownUnwrap(type);
}
 
Example 21
public <X> X unwrap(Gender value, Class<X> type, WrapperOptions options) {
    return CharacterTypeDescriptor.INSTANCE.unwrap(
        value == null ? null : value.getCode(),
        type,
        options
    );
}
 
Example 22
Source Project: lams   Source File: BasicBinder.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public final void bind(PreparedStatement st, J value, int index, WrapperOptions options) throws SQLException {
	final boolean traceEnabled = log.isTraceEnabled();
	if ( value == null ) {
		if ( traceEnabled ) {
			log.trace(
					String.format(
							NULL_BIND_MSG_TEMPLATE,
							index,
							JdbcTypeNameMapper.getTypeName( getSqlDescriptor().getSqlType() )
					)
			);
		}
		st.setNull( index, sqlDescriptor.getSqlType() );
	}
	else {
		if ( traceEnabled ) {
			log.trace(
					String.format(
							BIND_MSG_TEMPLATE,
							index,
							JdbcTypeNameMapper.getTypeName( sqlDescriptor.getSqlType() ),
							getJavaDescriptor().extractLoggableRepresentation( value )
					)
			);
		}
		doBind( st, value, index, options );
	}
}
 
Example 23
Source Project: iotplatform   Source File: JsonTypeDescriptor.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public <X> Object wrap(X value, WrapperOptions options) {
    if ( value == null ) {
        return null;
    }
    return fromString(value.toString());
}
 
Example 24
public <X> BitSet wrap(X value, WrapperOptions options) {
    if ( value == null ) {
        return null;
    }
    if ( String.class.isInstance( value ) ) {
        return fromString( (String) value );
    }
    if ( BitSet.class.isInstance( value ) ) {
        return (BitSet) value;
    }
    throw unknownWrap( value.getClass() );
}
 
Example 25
Source Project: hibernate-types   Source File: YearTypeDescriptor.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"unchecked"})
@Override
public <X> X unwrap(Year value, Class<X> type, WrapperOptions options) {
    if (value == null) {
        return null;
    }
    if (String.class.isAssignableFrom(type)) {
        return (X) toString(value);
    }
    if (Number.class.isAssignableFrom(type)) {
        Short numericValue = (short) value.getValue();
        return (X) (numericValue);
    }
    throw unknownUnwrap(type);
}
 
Example 26
@Override
public <X> T wrap(X value, WrapperOptions options) {
    if (value instanceof Array) {
        Array array = (Array) value;
        try {
            return ArrayUtil.unwrapArray((Object[]) array.getArray(), arrayObjectClass);
        } catch (SQLException e) {
            throw new IllegalArgumentException(e);
        }
    }
    return (T) value;
}
 
Example 27
Source Project: cosmo   Source File: CosmoCalendarTypeDescriptor.java    License: Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public <X> X unwrap(Calendar value, Class<X> type, WrapperOptions options) {
    if (value == null) {
        return null;
    }
    if (Calendar.class.isAssignableFrom(type)) {
        return (X) value;
    }
    if (CharacterStream.class.isAssignableFrom(type)) {
            return (X) new CharacterStreamImpl(value.toString());
    }
    throw unknownUnwrap(type);
}
 
Example 28
@Override
@SuppressWarnings({ "unchecked" })
public <X> X unwrap(BigInteger value, Class<X> type, WrapperOptions options) {
	if ( value == null ) {
		return null;
	}
	if ( BigInteger.class.isAssignableFrom( type ) ) {
		return (X) value;
	}
	if ( BigDecimal.class.isAssignableFrom( type ) ) {
		return (X) new BigDecimal( value );
	}
	if ( Byte.class.isAssignableFrom( type ) ) {
		return (X) Byte.valueOf( value.byteValue() );
	}
	if ( Short.class.isAssignableFrom( type ) ) {
		return (X) Short.valueOf( value.shortValue() );
	}
	if ( Integer.class.isAssignableFrom( type ) ) {
		return (X) Integer.valueOf( value.intValue() );
	}
	if ( Long.class.isAssignableFrom( type ) ) {
		return (X) Long.valueOf( value.longValue() );
	}
	if ( Double.class.isAssignableFrom( type ) ) {
		return (X) Double.valueOf( value.doubleValue() );
	}
	if ( Float.class.isAssignableFrom( type ) ) {
		return (X) Float.valueOf( value.floatValue() );
	}
	throw unknownUnwrap( type );
}
 
Example 29
@Override
public <X> Object wrap(X value, WrapperOptions options) {
    if (value == null) {
        return null;
    }
    return fromString(value.toString());
}
 
Example 30
Source Project: lams   Source File: InstantJavaDescriptor.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public <X> X unwrap(Instant instant, Class<X> type, WrapperOptions options) {
	if ( instant == null ) {
		return null;
	}

	if ( Instant.class.isAssignableFrom( type ) ) {
		return (X) instant;
	}

	if ( Calendar.class.isAssignableFrom( type ) ) {
		final ZoneId zoneId = ZoneId.ofOffset( "UTC", ZoneOffset.UTC );
		return (X) GregorianCalendar.from( instant.atZone( zoneId ) );
	}

	if ( java.sql.Timestamp.class.isAssignableFrom( type ) ) {
		return (X) Timestamp.from( instant );
	}

	if ( java.sql.Date.class.isAssignableFrom( type ) ) {
		return (X) java.sql.Date.from( instant );
	}

	if ( java.sql.Time.class.isAssignableFrom( type ) ) {
		return (X) java.sql.Time.from( instant );
	}

	if ( java.util.Date.class.isAssignableFrom( type ) ) {
		return (X) Date.from( instant );
	}

	if ( Long.class.isAssignableFrom( type ) ) {
		return (X) Long.valueOf( instant.toEpochMilli() );
	}

	throw unknownUnwrap( type );
}