Skip to content

Commit

Permalink
HHH-7777 - Deprecate XmlRepresentableType
Browse files Browse the repository at this point in the history
(cherry picked from commit 5ef8a66)
  • Loading branch information
sebersole committed Nov 12, 2012
1 parent 83e1227 commit 26a42cb
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 24 deletions.
56 changes: 43 additions & 13 deletions hibernate-core/src/main/java/org/hibernate/type/CustomType.java
Expand Up @@ -54,7 +54,10 @@
* @author Gavin King
* @author Steve Ebersole
*/
public class CustomType extends AbstractType implements IdentifierType, DiscriminatorType, VersionType, BasicType {
public class CustomType
extends AbstractType
implements IdentifierType, DiscriminatorType, VersionType, BasicType, StringRepresentableType {

private final UserType userType;
private final String name;
private final int[] types;
Expand Down Expand Up @@ -146,7 +149,7 @@ public Object replace(
SessionImplementor session,
Object owner,
Map copyCache) throws HibernateException {
return userType.replace(original, target, owner);
return userType.replace( original, target, owner );
}

public void nullSafeSet(PreparedStatement st, Object value, int index, boolean[] settable, SessionImplementor session)
Expand All @@ -163,20 +166,12 @@ public void nullSafeSet(PreparedStatement st, Object value, int index, SessionIm

@SuppressWarnings({ "UnusedDeclaration" })
public String toXMLString(Object value, SessionFactoryImplementor factory) {
if ( value == null ) {
return null;
}
if ( userType instanceof EnhancedUserType ) {
return ( (EnhancedUserType) userType ).toXMLString( value );
}
else {
return value.toString();
}
return toString( value );
}

@SuppressWarnings({ "UnusedDeclaration" })
public Object fromXMLString(String xml, Mapping factory) {
return ( (EnhancedUserType) userType ).fromXMLString(xml);
return fromStringValue( xml );
}

public String getName() {
Expand All @@ -193,7 +188,7 @@ public boolean isMutable() {
}

public Object stringToObject(String xml) {
return ( (EnhancedUserType) userType ).fromXMLString(xml);
return fromStringValue( xml );
}

public String objectToSQLString(Object value, Dialect dialect) throws Exception {
Expand Down Expand Up @@ -246,4 +241,39 @@ public boolean isDirty(Object old, Object current, boolean[] checkable, SessionI
throws HibernateException {
return checkable[0] && isDirty(old, current, session);
}

@Override
@SuppressWarnings("unchecked")
public String toString(Object value) throws HibernateException {
if ( StringRepresentableType.class.isInstance( userType ) ) {
return ( (StringRepresentableType) userType ).toString( value );
}
if ( value == null ) {
return null;
}
if ( EnhancedUserType.class.isInstance( userType ) ) {
//noinspection deprecation
return ( (EnhancedUserType) userType ).toXMLString( value );
}
return value.toString();
}

@Override
public Object fromStringValue(String string) throws HibernateException {
if ( StringRepresentableType.class.isInstance( userType ) ) {
return ( (StringRepresentableType) userType ).fromStringValue( string );
}
if ( EnhancedUserType.class.isInstance( userType ) ) {
//noinspection deprecation
return ( (EnhancedUserType) userType ).fromXMLString( string );
}
throw new HibernateException(
String.format(
"Could not process #fromStringValue, UserType class [%s] did not implement %s or %s",
name,
StringRepresentableType.class.getName(),
EnhancedUserType.class.getName()
)
);
}
}
@@ -1,10 +1,10 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
* Copyright (c) 2008, 2012, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Middleware LLC.
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
Expand All @@ -20,15 +20,11 @@
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*
*/
package org.hibernate.usertype;


/**
* A custom type that may function as an identifier or
* discriminator type, or may be marshalled to and from
* an XML document
* A custom type that may function as an identifier or discriminator type
*
* @author Gavin King
*/
Expand All @@ -39,13 +35,21 @@ public interface EnhancedUserType extends UserType {
public String objectToSQLString(Object value);

/**
* Return a string representation of this value, as it
* should appear in an XML document
* Return a string representation of this value, as it should appear in an XML document
*
* @deprecated To be removed in 5. Implement {@link org.hibernate.type.StringRepresentableType#toString(Object)}
* instead. See <a href="https://hibernate.onjira.com/browse/HHH-7776">HHH-7776</a> for details
*/
@Deprecated
public String toXMLString(Object value);

/**
* Parse a string representation of this value, as it
* appears in an XML document
* Parse a string representation of this value, as it appears in an XML document
*
* @deprecated To be removed in 5. Implement
* {@link org.hibernate.type.StringRepresentableType#fromStringValue(String)} instead.
* See <a href="https://hibernate.onjira.com/browse/HHH-7776">HHH-7776</a> for details
*/
@Deprecated
public Object fromXMLString(String xmlValue);
}

0 comments on commit 26a42cb

Please sign in to comment.