1 2 24 25 30 31 package com.sun.appserv.management.util.stringifier; 32 33 import java.lang.reflect.Array ; 34 import java.util.Arrays ; 35 import java.util.Iterator ; 36 37 40 41 public final class ArrayStringifier implements Stringifier 42 { 43 final String mDelim; 44 final Stringifier mElementStringifier; 45 boolean mAddBraces; 46 47 static final char LEFT_BRACE = '{'; 48 static final char RIGHT_BRACE = '}'; 49 static final String DEFAULT_DELIM=","; 50 51 public 52 ArrayStringifier() 53 { 54 this( SmartStringifier.DEFAULT ); 55 mAddBraces = false; 56 } 57 public 58 ArrayStringifier( boolean addBraces ) 59 { 60 this( DEFAULT_DELIM, SmartStringifier.DEFAULT, addBraces ); 61 } 62 63 public 64 ArrayStringifier( String delim ) 65 { 66 this( delim, false ); 67 } 68 69 public 70 ArrayStringifier( String delim, boolean addBraces) 71 { 72 this( delim, SmartStringifier.DEFAULT, addBraces ); 73 } 74 75 public 76 ArrayStringifier( Stringifier elementStringifier ) 77 { 78 this( DEFAULT_DELIM, elementStringifier ); 79 } 80 81 public 82 ArrayStringifier( String delim, Stringifier elementStringifier ) 83 { 84 this( delim, elementStringifier, false ); 85 } 86 87 public 88 ArrayStringifier( String delim, Stringifier elementStringifier, boolean addBraces) 89 { 90 mDelim = delim; 91 mElementStringifier = elementStringifier; 92 mAddBraces = addBraces; 93 } 94 95 static String 96 addBraces( boolean add, String s ) 97 { 98 String out = s; 99 if ( add ) 100 { 101 out = LEFT_BRACE + s + RIGHT_BRACE; 102 } 103 return( out ); 104 } 105 106 public String 107 stringify( Object o ) 108 { 109 final String s = this.stringify( (Object [])o, mDelim, mElementStringifier ); 110 111 return( addBraces( mAddBraces, s ) ); 112 } 113 114 117 public static String 118 stringify( Object [] o, String delim, Stringifier stringifier ) 119 { 120 final Iterator iter = Arrays.asList( o ).iterator(); 121 final IteratorStringifier iterStringifier = new IteratorStringifier( delim, stringifier ); 122 123 final String s = iterStringifier.stringify( iter ); 124 return( s ); 125 } 126 127 130 public static String 131 stringify( Object [] o, String delim ) 132 { 133 return( stringify( o, delim, SmartStringifier.DEFAULT ) ); 134 } 135 136 public final static ArrayStringifier DEFAULT = new ArrayStringifier( "," ); 137 } 138 139 | Popular Tags |