@ -34,37 +34,36 @@ import org.springframework.util.ClassUtils;
public class ObjectIdentityImpl implements ObjectIdentity {
public class ObjectIdentityImpl implements ObjectIdentity {
//~ Instance fields ================================================================================================
//~ Instance fields ================================================================================================
private Class < ? > javaT ype;
private final String t ype;
private Serializable identifier ;
private Serializable identifier ;
//~ Constructors ===================================================================================================
//~ Constructors ===================================================================================================
public ObjectIdentityImpl ( String javaT ype, Serializable identifier ) {
public ObjectIdentityImpl ( String t ype, Serializable identifier ) {
Assert . hasText ( javaT ype, "Java Type required" ) ;
Assert . hasText ( t ype, "Type required" ) ;
Assert . notNull ( identifier , "identifier required" ) ;
Assert . notNull ( identifier , "identifier required" ) ;
try {
this . javaType = ClassUtils . forName ( javaType , ClassUtils . getDefaultClassLoader ( ) ) ;
} catch ( ClassNotFoundException e ) {
throw new IllegalStateException ( "Unable to load javaType: " + javaType , e ) ;
}
this . identifier = identifier ;
this . identifier = identifier ;
this . type = type ;
}
}
/ * *
* Constructor which uses the name of the supplied class as the < tt > type < / tt > property .
* /
public ObjectIdentityImpl ( Class < ? > javaType , Serializable identifier ) {
public ObjectIdentityImpl ( Class < ? > javaType , Serializable identifier ) {
Assert . notNull ( javaType , "Java Type required" ) ;
Assert . notNull ( javaType , "Java Type required" ) ;
Assert . notNull ( identifier , "identifier required" ) ;
Assert . notNull ( identifier , "identifier required" ) ;
this . javaT ype = javaType ;
this . t ype = javaType . getName ( ) ;
this . identifier = identifier ;
this . identifier = identifier ;
}
}
/ * *
/ * *
* Creates the < code > ObjectIdentityImpl < / code > based on the passed
* Creates the < code > ObjectIdentityImpl < / code > based on the passed
* object instance . The passed object must provide a < code > getId ( ) < / code >
* object instance . The passed object must provide a < code > getId ( ) < / code >
* method , otherwise an exception will be thrown . The object passed will
* method , otherwise an exception will be thrown .
* be considered the { @link # javaType } , so if more control is required ,
* < p >
* an alternate constructor should be used instead .
* The class name of the object passed will be considered the { @link # type } , so if more control is required ,
* a different constructor should be used .
*
*
* @param object the domain object instance to create an identity for .
* @param object the domain object instance to create an identity for .
*
*
@ -73,12 +72,13 @@ public class ObjectIdentityImpl implements ObjectIdentity {
public ObjectIdentityImpl ( Object object ) throws IdentityUnavailableException {
public ObjectIdentityImpl ( Object object ) throws IdentityUnavailableException {
Assert . notNull ( object , "object cannot be null" ) ;
Assert . notNull ( object , "object cannot be null" ) ;
this . javaType = ClassUtils . getUserClass ( object . getClass ( ) ) ;
Class < ? > typeClass = ClassUtils . getUserClass ( object . getClass ( ) ) ;
type = typeClass . getName ( ) ;
Object result ;
Object result ;
try {
try {
Method method = this . javaType . getMethod ( "getId" , new Class [ ] { } ) ;
Method method = typeClass . getMethod ( "getId" , new Class [ ] { } ) ;
result = method . invoke ( object , new Object [ ] { } ) ;
result = method . invoke ( object , new Object [ ] { } ) ;
} catch ( Exception e ) {
} catch ( Exception e ) {
throw new IdentityUnavailableException ( "Could not extract identity from object " + object , e ) ;
throw new IdentityUnavailableException ( "Could not extract identity from object " + object , e ) ;
@ -123,15 +123,15 @@ public class ObjectIdentityImpl implements ObjectIdentity {
}
}
}
}
return javaT ype. equals ( other . javaT ype) ;
return t ype. equals ( other . t ype) ;
}
}
public Serializable getIdentifier ( ) {
public Serializable getIdentifier ( ) {
return identifier ;
return identifier ;
}
}
public Class < ? > getJava Type( ) {
public String get Type( ) {
return javaT ype;
return t ype;
}
}
/ * *
/ * *
@ -141,7 +141,7 @@ public class ObjectIdentityImpl implements ObjectIdentity {
* /
* /
public int hashCode ( ) {
public int hashCode ( ) {
int code = 31 ;
int code = 31 ;
code ^ = this . javaT ype. hashCode ( ) ;
code ^ = this . t ype. hashCode ( ) ;
code ^ = this . identifier . hashCode ( ) ;
code ^ = this . identifier . hashCode ( ) ;
return code ;
return code ;
@ -150,7 +150,7 @@ public class ObjectIdentityImpl implements ObjectIdentity {
public String toString ( ) {
public String toString ( ) {
StringBuilder sb = new StringBuilder ( ) ;
StringBuilder sb = new StringBuilder ( ) ;
sb . append ( this . getClass ( ) . getName ( ) ) . append ( "[" ) ;
sb . append ( this . getClass ( ) . getName ( ) ) . append ( "[" ) ;
sb . append ( "Java Type: " ) . append ( this . javaType . getName ( ) ) ;
sb . append ( "Type: " ) . append ( this . type ) ;
sb . append ( "; Identifier: " ) . append ( this . identifier ) . append ( "]" ) ;
sb . append ( "; Identifier: " ) . append ( this . identifier ) . append ( "]" ) ;
return sb . toString ( ) ;
return sb . toString ( ) ;