1 23 package com.sun.appserv.management.util.jmx; 24 25 import java.util.HashMap ; 26 import java.util.Map ; 27 import java.util.Set ; 28 import java.util.Iterator ; 29 import java.util.HashSet ; 30 import java.util.Arrays ; 31 import java.util.Collections ; 32 33 import com.sun.appserv.management.util.misc.Output; 34 35 39 public class AttributeNameMapperImpl implements AttributeNameMapper 40 { 41 final Map <String ,String > mDerivedToOriginal; 42 final Map <String ,String > mOriginalToDerived; 43 final Set <String > mAllNames; 44 final AttributeNameMangler mMangler; 45 46 private Output mDebug; 47 48 51 public 52 AttributeNameMapperImpl() 53 { 54 this( null, null); 55 } 56 57 public void 58 setDebugOutput( final Output debugOutput ) 59 { 60 mDebug = debugOutput; 61 } 62 63 protected final void 64 debug(final Object o) 65 { 66 if ( mDebug != null ) 67 { 68 mDebug.println( o ); 69 } 70 } 71 72 public String 73 matchName( 74 final String derivedName, 75 final String [] candidates ) 76 { 77 throw new UnsupportedOperationException ( "matchName" ); 78 } 79 80 85 public 86 AttributeNameMapperImpl( final String [] originalNames ) 87 { 88 this( originalNames, new AttributeNameManglerImpl( true, null ) ); 89 } 90 91 95 public 96 AttributeNameMapperImpl( final AttributeNameMangler mangler ) 97 { 98 this( null, mangler ); 99 } 100 101 105 public 106 AttributeNameMapperImpl( 107 final String [] originalNames, 108 AttributeNameMangler mangler ) 109 { 110 mDebug = null; 111 mDerivedToOriginal = new HashMap <String ,String >(); 112 mOriginalToDerived = new HashMap <String ,String >(); 113 mAllNames = new HashSet <String >(); 114 mMangler = mangler == null ? new AttributeNameManglerImpl( true, null ) : mangler ; 115 116 if ( originalNames != null ) 117 { 118 deriveAll( originalNames ); 119 } 120 } 121 122 123 public void 124 deriveAll( final String [] originalNames ) 125 { 126 final Set <String > notRequired = new HashSet <String >(); 127 final Set <String > required = new HashSet <String >(); 128 129 for( final String originalName : originalNames ) 133 { 134 if ( mOriginalToDerived.containsKey( originalName ) ) 135 { 136 } 138 else if ( ! requiresMapping( originalName ) ) 139 { 140 notRequired.add( originalName ); 141 } 142 else 143 { 144 required.add( originalName ); 145 } 146 } 147 148 for ( final String name : notRequired ) 150 { 151 addMapping( name, name ); 152 } 153 154 for( final String originalName : required ) 156 { 157 final String preferredDerivation = originalToDerived( originalName ); 159 160 String derivedName = preferredDerivation; 162 int id = 0; 163 while ( mAllNames.contains( derivedName ) || 164 required.contains( derivedName ) ) 165 { 166 derivedName = preferredDerivation + "_" + id; 167 ++id; 168 } 169 addMapping( originalName, derivedName ); 170 } 171 } 172 173 174 public boolean 175 derivedSameAsOriginal( String derivedName ) 176 { 177 final String original = derivedToOriginal( derivedName ); 178 final boolean theSame = original != null && derivedName.equals( original ); 179 180 return( theSame ); 181 } 182 183 public void 184 addMapping( 185 final String originalName, 186 final String derivedName ) 187 { 188 mDerivedToOriginal.put( derivedName, originalName ); 189 mOriginalToDerived.put( originalName, derivedName ); 190 191 mAllNames.add( derivedName ); 192 } 193 194 public void 195 dontMap( final String originalName ) 196 { 197 debug( "dontMap: " + originalName ); 198 addMapping( originalName, originalName ); 199 } 200 201 public boolean 202 requiresMapping( final String originalName ) 203 { 204 boolean requiresMapping = true ; 205 206 final char firstChar = originalName.charAt( 0 ); 208 if ( Character.isUpperCase( firstChar ) ) 209 { 210 if ( Character.isJavaIdentifierStart( firstChar ) ) 211 { 212 final int length = originalName.length(); 213 214 requiresMapping = false; 215 for( int i = 1; i < length; ++i ) 216 { 217 if ( ! Character.isJavaIdentifierPart( originalName.charAt( i ) ) ) 218 { 219 requiresMapping = true; 220 break; 221 } 222 } 223 } 224 } 225 226 return( requiresMapping ); 227 } 228 229 230 public String 231 originalToDerived( String originalName ) 232 { 233 String derivedName = (String )mOriginalToDerived.get( originalName ); 234 235 if ( derivedName == null ) 236 { 237 derivedName = mMangler.mangleAttributeName( originalName ); 238 } 239 240 return( derivedName ); 241 } 242 243 public String 244 derivedToOriginal( String derivedName ) 245 { 246 String original = (String )mDerivedToOriginal.get( derivedName ); 247 248 if ( original == null ) 249 { 250 original = derivedName; 251 } 252 253 return( original ); 254 } 255 256 257 public Set <String > 258 getAttributeNames( ) 259 { 260 return( mAllNames ); 261 } 262 263 public String 264 toString() 265 { 266 final StringBuffer buf = new StringBuffer (); 267 268 buf.append( "AttributeNameMapperImpl mappings:\n" ); 269 final Iterator iter = getAttributeNames().iterator(); 270 while ( iter.hasNext() ) 271 { 272 final String name = (String )iter.next(); 273 buf.append( name ); 274 buf.append( " => " ); 275 buf.append( derivedToOriginal( name ) + "\n" ); 276 } 277 278 return( buf.toString() ); 279 } 280 } 281 | Popular Tags |