FileDocCategorySizeDatePackage
StringUtil.javaAPI DocGlassfish v2 API12395Fri May 04 22:31:06 BST 2007com.sun.appserv.management.util.misc

StringUtil

public final class StringUtil extends Object
Escapes/unescapes strings

Fields Summary
public static final char
QUOTE_CHAR
public static final String
QUOTE
public static final String
LS
Line separator as returned by System.getProperty()
private static String
NEWLINE_STR
private static final String
NANOS_FORMAT
private static final String
MICROS_FORMAT
private static final String
MILLIS_FORMAT
private static final String
SECONDS_FORMAT
Constructors Summary
private StringUtil()

Methods Summary
public static java.lang.StringNEWLINE()

	
	      
	
	
	    if ( NEWLINE_STR == null )
	    {
	        NEWLINE_STR = System.getProperty( "line.separator" );
	    }
	    return NEWLINE_STR;
	
public static java.lang.StringfindAndStripPrefix(java.util.Set prefixes, java.lang.String s)

return
the String after stripping the prefix
throws
IllegalArgumentException if no prefix found

        final String  prefix    = getPrefix( prefixes, s );
        if ( prefix == null )
        {
            throw new IllegalArgumentException( s );
        }
        
        return stripPrefix( s, prefix );
    
public static java.lang.StringgetMillisString(long nanos)

param
nanos elapsed nanoseconds
return
a String describing the elapsed duration in seconds

        return getTimingString( nanos, TimeUnit.MILLISECONDS );
    
public static java.lang.StringgetPrefix(java.util.Set prefixes, java.lang.String s)

return
the prefix found, or null if not found

        String  result  = null;
        for( final String prefix : prefixes )
        {
            if ( s.startsWith( prefix ) )
            {
                result  = prefix;
                break;
            }
        }
        return result;
    
public static java.lang.StringgetSecondsString(long nanos)

param
nanos elapsed nanoseconds
return
a String describing the elapsed duration in seconds

        return getTimingString( nanos, TimeUnit.SECONDS );
    
public static java.lang.StringgetTimingString(long nanos, java.util.concurrent.TimeUnit timeUnit)

    
         
    
               
            
    
        String result   = null;
        
        if ( timeUnit == TimeUnit.NANOSECONDS )
        {
            result = String.format( NANOS_FORMAT, nanos );
        }
        else if ( timeUnit == TimeUnit.MICROSECONDS )
        {
            result = String.format( MICROS_FORMAT, micros(nanos) );
        }
        else if ( timeUnit == TimeUnit.MILLISECONDS )
        {
            result = String.format( MILLIS_FORMAT, millis(nanos) );
        }
        else if ( timeUnit == TimeUnit.SECONDS )
        {
            result = String.format( SECONDS_FORMAT, seconds(nanos) );
        }

        return result;
    
public static java.lang.StringgetTimingString(long nanos)
Get a String representing the specified number of nanoseconds, choosing the appropriate units based on magnitude of the value.

param
nanos elapsed nanoseconds
return
a String describing the elapsed duration

        final long seconds     = nanos / (1000 * 1000 * 1000 );
        final long millis      = nanos % (1000 * 1000 * 1000 ) / ( 1000 * 1000);
        final long micros      = nanos % (1000 * 1000 ) / 1000;
        final long nanosMod    = nanos % 1000;
        
        String runTimeString = null;
        final long MICROSECOND = 1000;
        final long MILLISECOND = 1000 * MICROSECOND;
        if ( nanos < 10 * 1000 )
        {
            runTimeString   = nanos + " nanoseconds";
        }
        else if ( nanos < 1000 * 10 * 1000 )
        {
            runTimeString   = (nanos / MICROSECOND) + " microseconds";
        }
        else
        {
            runTimeString   = (nanos / MILLISECOND ) + " milliseconds";
        }
        
        return runTimeString;
    
private static doublemicros(long nanos)

        return (double)nanos / 1000;
    
private static doublemillis(long nanos)

        return (double)nanos / (1000 * 1000 );
    
public static java.lang.Stringquote(java.lang.Object o)

	
		  
	   
	
		return( quote( o, QUOTE_CHAR ) );
	
public static java.lang.Stringquote(java.lang.Object o, char leftHandChar)

		final String	s	= o == null ? "null" : SmartStringifier.toString( o );
		
		char	leftChar	= leftHandChar;
		char	rightChar	= leftHandChar;
		
		if ( leftHandChar == '(" )
		{
			rightChar	= ')";
		}
		else if ( leftHandChar == '{" )
		{
			rightChar	= '}";
		}
		else if ( leftHandChar == '[" )
		{
			rightChar	= ']";
		}
		else if ( leftHandChar == '<" )
		{
			rightChar	= '>";
		}
		else
		{
			// same char on both left and right
		}
		
		final String	out	= leftChar + s + rightChar;

		return( out );
	
public static java.lang.StringreplaceSuffix(java.lang.String s, java.lang.String fromSuffix, java.lang.String toSuffix)

		if ( ! s.endsWith( fromSuffix ) )
		{
			throw new IllegalArgumentException( fromSuffix );
		}

		return( stripSuffix( s, fromSuffix ) + toSuffix );
	
private static doubleseconds(long nanos)

        return (double)nanos / (1000 * 1000 * (long)1000 );
    
public static java.lang.StringstripPrefix(java.lang.String s, java.lang.String prefix)

		String	result	= s;
		
		if ( s.startsWith( prefix ) )
		{
			result	= s.substring( prefix.length(), s.length() );
		}
		
		return( result );
	
public static java.lang.StringstripPrefixAndSuffix(java.lang.String s, java.lang.String prefix, java.lang.String suffix)

		return stripPrefix( stripSuffix( s, suffix ), prefix );
	
public static java.lang.StringstripSuffix(java.lang.String s, java.lang.String suffix)

		String	result	= s;
		
		if ( s.endsWith( suffix ) )
		{
			result	= s.substring( 0, s.length() - suffix.length() );
		}
		
		return( result );
	
public static java.lang.StringtoHexString(byte theByte)

		String result	= Integer.toHexString( ((int)theByte) & 0x000000FF );
		if ( result.length() == 1 )
		{
			result	= "0" + result;
		}
		return( result );
	
public static java.lang.StringtoHexString(byte[] bytes)

		return( toHexString( bytes, null ) );
	
public static java.lang.StringtoHexString(byte[] bytes, java.lang.String delim)

		final StringBuffer	buf	= new StringBuffer();
		
		if ( bytes.length == 0 )
		{
			// nothing
		}
		else if ( delim == null || delim.length() == 0 )
		{
			for( int i = 0; i < bytes.length; ++i )
			{
				buf.append( toHexString( bytes[ i ] ) );
			}
		}
		else
		{
			for( int i = 0; i < bytes.length; ++i )
			{
				buf.append( toHexString( bytes[ i ] ) + delim );
			}
		
			// remove trailing delim
			buf.setLength( buf.length() - 1);
		}
		
		return( buf.toString() );
	
public static java.lang.StringtoString(java.lang.Object o)

        String  s   = null;
        
        if ( o instanceof String )
        {
            s   = (String)o;
        }
        else if ( o instanceof Throwable )
        {
            s   = ExceptionUtil.toString( (Throwable)o );
        }
        else if ( o instanceof ObjectName )
        {
            s   = JMXUtil.toString( (ObjectName)o );
        }
        else if ( o instanceof Attribute )
        {
            final Attribute a = (Attribute)o;
            s   = a.getName() + "=" + toString( a.getValue() );
        }
        else if ( o instanceof AttributeList )
        {
            final Map<String,Object>    items   = JMXUtil.attributeListToValueMap( (AttributeList)o );
            s   = "{" + MapUtil.toString( items ) + "}";
        }
        else if ( o instanceof byte[] )
        {
            final byte[]    b   = byte[].class.cast( o );
            s  = "byte[] of length " + b.length;
        }
        else if ( o == null )
        {
            s   = "null";
        }
        else if ( o instanceof Object[] )
        {
            s   = toString( ", ", (Object[])o );
        }
        else
        {
            s   = "" + o;
        }
        
        return s;
    
public static java.lang.StringtoString(java.lang.String[] args)

        return toString( ", ", args );
    
public static java.lang.StringtoString(java.lang.String delim, java.lang.String args)

        return toString( delim, (Object[])args );
    
public static java.lang.StringtoString(java.lang.String delim, java.lang.Object args)
Turn an array (or varargs) set of Objects into a String using the specified delimiter.

        String  result  = null;
        
        if ( args == null )
        {
            result  = "" + null;
        }
        else if ( args.length == 0 )
        {
            result  = "";
        }
        else if ( args.length == 1 )
        {
            result  = toString( args[ 0 ] );
        }
        else
        {
            final StringBuilder builder = new StringBuilder();
            
            for( int i = 0; i < args.length - 1; ++i )
            {
                builder.append( toString( args[ i ] ) );
                builder.append( delim );
            }
            builder.append( toString( args[ args.length - 1 ] ) );
            
            result  = builder.toString();
        }
        
        
        return result;
     
public static java.lang.StringupperCaseFirstLetter(java.lang.String s)

		String	result	= s;
		
		if ( s.length() >= 1 )
		{
			result	= s.substring( 0, 1 ).toUpperCase() + s.substring( 1, s.length() );
		}

		return( result );
	
private static java.lang.StringzeroPad(long num)

        String  result  = null;
        if ( num >= 100 )
            result  = "" + num;
        else if ( num >= 10 )
            result  = "0" + num;
        else
            result  = "00" + num;
        return result;