FileDocCategorySizeDatePackage
SmartStringifier.javaAPI DocGlassfish v2 API5349Fri May 04 22:25:02 BST 2007com.sun.cli.util.stringifier

SmartStringifier

public final class SmartStringifier extends Object implements Stringifier

Fields Summary
public static 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 ( StringifierRegistry.DEFAULT, multiDelim, encloseArrays );
	
public SmartStringifier(StringifierRegistry registry, String multiDelim, boolean encloseArrays)

		mMultiDelim		= multiDelim;
		mEncloseArrays	= encloseArrays;
		mRegistry		= registry;
	
Methods Summary
private StringifiergetStringifier(java.lang.Object target)

		 
	   
	
		if ( target == null )
			return( null );
			
		final Class targetClass	= target.getClass();
		
		Stringifier	stringifier	= mRegistry.lookup( targetClass );
		
		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;
				}
			}
		}
	
		return( stringifier );
	
public voidsetRegistry(StringifierRegistry registry)

		mRegistry	= registry;
	
private java.lang.StringsmartStringify(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.Stringstringify(java.lang.Object target)

		if ( target == null )
		{
			return( "<null>" );
		}
		
		return( smartStringify( target ) );
	
public static java.lang.StringtoString(java.lang.Object target)

		return( DEFAULT.stringify( target ) );