FileDocCategorySizeDatePackage
AttributeNameMapperImpl.javaAPI DocGlassfish v2 API7780Fri May 04 22:31:04 BST 2007com.sun.appserv.management.util.jmx

AttributeNameMapperImpl

public class AttributeNameMapperImpl extends Object implements AttributeNameMapper
Maps Attribute names to legal Java identifiers, so that they can be exposed in a proxy with get/set routines

Fields Summary
final Map
mDerivedToOriginal
final Map
mOriginalToDerived
final Set
mAllNames
final AttributeNameMangler
mMangler
private com.sun.appserv.management.util.misc.Output
mDebug
Constructors Summary
public AttributeNameMapperImpl()
Create a new instance which will map nothing by default.

		this( null, null);
	
public AttributeNameMapperImpl(String[] originalNames)
Create a new instance which will map (as necessary) the specified Attribute names. Same as AttributeNameMapperImpl( originalNames, new AttributeNameManglerImpl() )

		this( originalNames, new AttributeNameManglerImpl( true, null ) );
	
public AttributeNameMapperImpl(AttributeNameMangler mangler)
Create a new instance which will map (as necessary) the specified Attribute names

		this( null, mangler );
	
public AttributeNameMapperImpl(String[] originalNames, AttributeNameMangler mangler)
Create a new instance which will map (as necessary) the specified Attribute names

		mDebug  = null;
		mDerivedToOriginal	= new HashMap<String,String>();
		mOriginalToDerived	= new HashMap<String,String>();
		mAllNames		= new HashSet<String>();
		mMangler		= mangler == null ? new AttributeNameManglerImpl( true, null ) : mangler ;
		
		if ( originalNames != null )
		{
			deriveAll( originalNames );
		}
	
Methods Summary
public voidaddMapping(java.lang.String originalName, java.lang.String derivedName)

		mDerivedToOriginal.put( derivedName, originalName );
		mOriginalToDerived.put( originalName, derivedName );
		
		mAllNames.add( derivedName );
	
protected final voiddebug(java.lang.Object o)

	    if ( mDebug != null )
	    {
            mDebug.println( o );
	    }
	
public voidderiveAll(java.lang.String[] originalNames)

		final Set<String>	notRequired	= new HashSet<String>();
		final Set<String>	required	= new HashSet<String>();
		
		// first determine all names we (a) need to map and (b) don't need to map.
		// we must not generate any mapped names that conflict with names
		// that don't require mapping
		for( final String originalName : originalNames )
		{
			if ( mOriginalToDerived.containsKey( originalName ) )
			{
			    // continue; already present (explicit mapping)
			}
			else if ( ! requiresMapping( originalName ) )
			{
				notRequired.add( originalName );
			}
			else
			{
				required.add( originalName );
			}
		}
		
		// add all ones that don't require mapping to map to themselves
		for ( final String name : notRequired )
		{
			addMapping( name, name );
		}
		
		// now create a mapped name for each one that requires it.
		for( final String originalName : required )
		{
			// our preferred derivation
			final String	preferredDerivation	= originalToDerived( originalName );
			
			// ensure that the derived name is not already used
			String	derivedName	= preferredDerivation;
			int id	= 0;
			while ( mAllNames.contains( derivedName ) ||
					required.contains( derivedName ) )
			{
				derivedName	= preferredDerivation + "_" + id;
				++id;
			}
			addMapping( originalName, derivedName );
		}
	
public booleanderivedSameAsOriginal(java.lang.String derivedName)

		final String	original	= derivedToOriginal( derivedName );
		final boolean	theSame		= original != null && derivedName.equals( original );
			
		return( theSame );
	
public java.lang.StringderivedToOriginal(java.lang.String derivedName)

		String	original	= (String)mDerivedToOriginal.get( derivedName );
		
		if ( original == null )
		{
			original	= derivedName;
		}
		
		return( original );
	
public voiddontMap(java.lang.String originalName)

	    debug( "dontMap: " + originalName );
		addMapping( originalName, originalName );
	
public java.util.SetgetAttributeNames()

		return( mAllNames );
	
public java.lang.StringmatchName(java.lang.String derivedName, java.lang.String[] candidates)

	    throw new UnsupportedOperationException( "matchName" );
	
public java.lang.StringoriginalToDerived(java.lang.String originalName)

		String	derivedName	= (String)mOriginalToDerived.get( originalName );
		
		if ( derivedName == null )
		{
			derivedName	= mMangler.mangleAttributeName( originalName );
		}
		
		return( derivedName );
	
public booleanrequiresMapping(java.lang.String originalName)

		boolean	requiresMapping	= true ;
		
		// must start with upper-case first-letter
		final char	firstChar	= originalName.charAt( 0 );
		if ( Character.isUpperCase( firstChar ) )
		{
			if ( Character.isJavaIdentifierStart( firstChar ) )
			{
				final int	length	= originalName.length();
				
				requiresMapping	= false;
				for( int i = 1; i < length; ++i )
				{
					if ( ! Character.isJavaIdentifierPart( originalName.charAt( i ) ) )
					{
						requiresMapping	= true;
						break;
					}
				}
			}
		}
		
		return( requiresMapping );
	
public voidsetDebugOutput(com.sun.appserv.management.util.misc.Output debugOutput)

	    mDebug  = debugOutput;
	
public java.lang.StringtoString()

		final StringBuffer	buf	= new StringBuffer();
		
		buf.append( "AttributeNameMapperImpl mappings:\n" );
		final Iterator iter	= getAttributeNames().iterator();
		while ( iter.hasNext() )
		{
			final String	name	= (String)iter.next();
			buf.append( name );
			buf.append( " => " );
			buf.append( derivedToOriginal( name ) + "\n" );
		}
		
		return( buf.toString() );