1 23 24 29 30 package com.sun.cli.util.stringifier; 31 32 import java.util.HashMap ; 33 import java.util.Iterator ; 34 import java.util.Collection ; 35 import java.lang.reflect.Array ; 36 37 import javax.management.*; 38 39 import com.sun.cli.util.*; 40 41 42 46 public final class SmartStringifier implements Stringifier 47 { 48 public static SmartStringifier DEFAULT = new SmartStringifier( "," ); 49 private final String mMultiDelim; 50 private final boolean mEncloseArrays; 51 protected StringifierRegistry mRegistry; 52 53 public 54 SmartStringifier( ) 55 { 56 this( "," ); 57 } 58 59 public 60 SmartStringifier( String multiDelim ) 61 { 62 this ( multiDelim, true ); 63 } 64 65 public 66 SmartStringifier( String multiDelim, boolean encloseArrays ) 67 { 68 this ( StringifierRegistry.DEFAULT, multiDelim, encloseArrays ); 69 } 70 71 public 72 SmartStringifier( StringifierRegistry registry, String multiDelim, boolean encloseArrays) 73 { 74 mMultiDelim = multiDelim; 75 mEncloseArrays = encloseArrays; 76 mRegistry = registry; 77 } 78 79 public void 80 setRegistry( StringifierRegistry registry ) 81 { 82 mRegistry = registry; 83 } 84 85 86 private final static Class [] STRINGIFIER_REGISTRY_LOOKUPS = 87 { 88 Iterator .class, 89 Collection .class, 90 HashMap .class 91 }; 92 private Stringifier 93 getStringifier( Object target ) 94 { 95 if ( target == null ) 96 return( null ); 97 98 final Class targetClass = target.getClass(); 99 100 Stringifier stringifier = mRegistry.lookup( targetClass ); 101 102 if ( stringifier == null ) 103 { 104 final int numLookups = STRINGIFIER_REGISTRY_LOOKUPS.length; 106 for( int i = 0; i < numLookups; ++i ) 107 { 108 final Class theClass = STRINGIFIER_REGISTRY_LOOKUPS[ i ]; 109 110 stringifier = mRegistry.lookup( theClass ); 111 if ( stringifier != null && theClass.isAssignableFrom( target.getClass() ) ) 112 { 113 break; 114 } 115 } 116 } 117 118 return( stringifier ); 119 } 120 121 122 private String 123 smartStringify( Object target ) 124 { 125 String result = null; 126 127 if ( ClassUtil.objectIsArray( target ) ) 128 { 129 Object [] theArray = null; 130 131 final Class elementClass = 132 ClassUtil.getArrayElementClass( target.getClass() ); 133 134 if ( ClassUtil.IsPrimitiveClass( elementClass ) ) 135 { 136 theArray = ArrayConversion.toAppropriateType( target ); 137 } 138 else 139 { 140 theArray = (Object [])target; 141 } 142 143 144 result = ArrayStringifier.stringify( theArray, mMultiDelim, this); 145 if ( mEncloseArrays ) 146 { 147 result = "{" + result + "}"; 148 } 149 } 150 else 151 { 152 Stringifier stringifier = getStringifier( target ); 153 154 if ( stringifier != null && stringifier.getClass() == this.getClass() ) 155 { 156 stringifier = null; 158 } 159 160 if ( stringifier != null ) 161 { 162 result = stringifier.stringify( target ); 163 } 164 } 165 166 if ( result == null ) 167 { 168 result = target.toString(); 169 } 170 171 return( result ); 172 } 173 174 public static String 175 toString( Object target ) 176 { 177 return( DEFAULT.stringify( target ) ); 178 } 179 180 public String 181 stringify( Object target ) 182 { 183 if ( target == null ) 184 { 185 return( "<null>" ); 186 } 187 188 return( smartStringify( target ) ); 189 } 190 } 191 192 | Popular Tags |