SmartStringifierpublic final class SmartStringifier extends Object implements StringifierStringifies an Object in the "best" possible way, using the
StringifierRegistry.DEFAULT registry and/or internal logic. |
Fields Summary |
---|
public static final SmartStringifier | DEFAULT | private final String | mMultiDelim | private final boolean | mEncloseArrays | protected StringifierRegistry | mRegistry | private static final Class[] | STRINGIFIER_REGISTRY_LOOKUPS |
Constructors Summary |
---|
public SmartStringifier()
this( "," );
| public SmartStringifier(String multiDelim)
this ( multiDelim, true );
| public SmartStringifier(String multiDelim, boolean encloseArrays)
this ( StringifierRegistryImpl.DEFAULT, multiDelim, encloseArrays );
| public SmartStringifier(StringifierRegistry registry, String multiDelim, boolean encloseArrays)
mMultiDelim = multiDelim;
mEncloseArrays = encloseArrays;
mRegistry = registry;
|
Methods Summary |
---|
private Stringifier | getStringifier(java.lang.Object target)
if ( target == null )
return( null );
final Class<?> targetClass = target.getClass();
Stringifier stringifier = mRegistry.lookup( targetClass );
if ( target instanceof javax.management.ObjectName )
{
assert( stringifier != null );
}
if ( stringifier == null )
{
// exact match failed...look for match in defined order
final int numLookups = STRINGIFIER_REGISTRY_LOOKUPS.length;
for( int i = 0; i < numLookups; ++i )
{
final Class<?> theClass = STRINGIFIER_REGISTRY_LOOKUPS[ i ];
stringifier = mRegistry.lookup( theClass );
if ( stringifier != null && theClass.isAssignableFrom( target.getClass() ) )
{
break;
}
}
}
if ( stringifier == null )
{
// see if there is a Stringifier for any superclass;
Class tempClass = targetClass;
while ( tempClass != Object.class )
{
stringifier = mRegistry.lookup( tempClass );
if ( stringifier != null )
{
break;
}
tempClass = tempClass.getSuperclass();
}
}
if ( stringifier == null )
{
final Class[] interfaces = targetClass.getInterfaces();
if ( interfaces.length != 0 )
{
stringifier = new InterfacesStringifier( interfaces );
}
}
return( stringifier );
| public void | setRegistry(StringifierRegistry registry)
mRegistry = registry;
| private java.lang.String | smartStringify(java.lang.Object target)
String result = null;
if ( ClassUtil.objectIsArray( target ) )
{
Object [] theArray = null;
final Class elementClass =
ClassUtil.getArrayElementClass( target.getClass() );
if ( ClassUtil.IsPrimitiveClass( elementClass ) )
{
theArray = ArrayConversion.toAppropriateType( target );
}
else
{
theArray = (Object [])target;
}
result = ArrayStringifier.stringify( theArray, mMultiDelim, this);
if ( mEncloseArrays )
{
result = "{" + result + "}";
}
}
else
{
Stringifier stringifier = getStringifier( target );
if ( stringifier != null && stringifier.getClass() == this.getClass() )
{
// avoid recursive call to self
stringifier = null;
}
if ( stringifier != null )
{
result = stringifier.stringify( target );
}
}
if ( result == null )
{
result = target.toString();
}
return( result );
| public java.lang.String | stringify(java.lang.Object target)
if ( target == null )
{
return( "<null>" );
}
return( smartStringify( target ) );
| public static java.lang.String | toString(java.lang.Object target)
return( DEFAULT.stringify( target ) );
|
|