FileDocCategorySizeDatePackage
AMXAttributeNameMapperImpl.javaAPI DocGlassfish v2 API10551Fri May 04 22:23:40 BST 2007com.sun.enterprise.management.support

AMXAttributeNameMapperImpl

public class AMXAttributeNameMapperImpl extends Object implements AMXAttributeNameMapper
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
mDerivedNames
final Map
mUnmappedOriginals
private com.sun.appserv.management.util.misc.Output
mDebug
private static final char
DASH_CHAR
private static final String
DASH
Constructors Summary
public AMXAttributeNameMapperImpl()
Create a new instance which will map nothing by default.

		mDebug  = null;
		mDerivedToOriginal	= new HashMap<String,String>();
		mOriginalToDerived	= new HashMap<String,String>();
		mDerivedNames		= new HashSet<String>();
		
		mUnmappedOriginals  = new HashMap<String,String>();
	
Methods Summary
public voidaddMapping(java.lang.String originalName, java.lang.String derivedName)

		mDerivedToOriginal.put( derivedName, originalName );
		mOriginalToDerived.put( originalName, derivedName );
		
		if ( mUnmappedOriginals.keySet().contains( originalName ) )
		{
		    mUnmappedOriginals.remove( originalName );
		}
		
		mDerivedNames.add( derivedName );
	
protected final voiddebug(java.lang.Object o)

	    if ( mDebug != null )
	    {
            mDebug.println( o );
	    }
	
public java.lang.StringderivedToOriginal(java.lang.String derivedName)

		final String	original = mDerivedToOriginal.get( derivedName );
		return( original );
	
public voiddontMap(java.lang.String originalName)

		addMapping( originalName, originalName );
	
public java.util.SetgetDerivedNames()

		return( mDerivedNames );
	
public java.util.SetgetUnmappedOriginals()

	    Set<String>   unmapped  = Collections.emptySet();
	    
	    if ( mUnmappedOriginals.keySet().size() != 0 )
	    {
    	    unmapped    = new HashSet<String>( mUnmappedOriginals.values());
	    }
	    
	    return unmapped;
	
public java.lang.StringmatchName(java.lang.String derivedName, java.lang.String[] candidates)
Attempt to match the derived name to one of the candidates. This facility is used when different runtime conditions present different original names which must be mapped to the same derived name.

If a name is matched it is added as a mapping and the original name is returned, otherwise an exception is thrown.

	    String  result  = null;
	    
	    final String    existingOriginal    = derivedToOriginal( derivedName );
	    if ( existingOriginal != null )
	    {
	        throw new IllegalArgumentException( "Already mapped: " +
	            derivedName + " => " + existingOriginal );
	    }
	    if ( mUnmappedOriginals.keySet().size() == 0 )
	    {
	        throw new IllegalArgumentException(
	            "There are no unmapped originals, existing mappings: " +
	            MapUtil.toString( mOriginalToDerived, ", " ) );
	    }
	    
	    for( final String candidate : candidates )
	    {
	        final String candidateNormalized    = normalize( candidate );
	        
	        if ( mUnmappedOriginals.keySet().contains( candidateNormalized ) )
	        {
	            result  = mUnmappedOriginals.get( candidateNormalized );
	            mUnmappedOriginals.remove( candidateNormalized );
	            addMapping( result, derivedName );
	            break;
	        }
	    }
	    
	    if ( result == null )
	    {
	        final String msg = derivedName + " can't be matched to " + 
	            StringUtil.toString( ", " , (Object[])candidates ) +
	            ", unmapped originals: " +
	                CollectionUtil.toString( mUnmappedOriginals.values(), ", ") +
                    ",  mapped originals: {" + MapUtil.toString( mDerivedToOriginal, ", ") + "}";
	        throw new IllegalArgumentException( msg );
	    }
	    
	    return result;
	
public java.lang.StringmatchName(java.lang.String derivedName, java.lang.String candidate1, java.lang.String candidate2)

	    return matchName( derivedName, new String[] { candidate1, candidate2 } );
	
public java.lang.StringmatchName(java.lang.String derivedName, java.lang.String candidate1)

	    return matchName( derivedName, new String[] { candidate1 } );
	
public voidmatchNames(java.lang.String[] derivedNames, java.lang.String[] originalNames)
Attempt to match an original name with a derived one by performing a case-insensitive comparison. It is OK if there are more derived names than originals, or if there are more original names than derived names.

param
derivedNames
param
originalNames

	    final Set<String>  originals  = GSetUtil.newSet( originalNames );
	    final Set<String>  deriveds   = GSetUtil.newSet( derivedNames );
	    
	    // first, eliminate identical names
	    for( final String original : originalNames )
	    {
	        if ( deriveds.contains( original ) )
	        {
    	        //System.out.println( "IDENTITY: " + original );
	            addMapping( original, original );
	            originals.remove( original );
	            deriveds.remove( original );
	        }
	    }
	    
	    // now we have remaining only those names that differ.
	    if ( originals.size() != 0 )
	    {
    	    final Map<String,String>   originalsMap = normalize( originals );
    	    final Map<String,String>   derivedsMap   = normalize( deriveds );
    	    
    	    for( final String originalLowerCase : originalsMap.keySet() )
    	    {
    	        final String    original    = originalsMap.get( originalLowerCase );
    	        final String    derived     = derivedsMap.get( originalLowerCase );
    	        if ( derived != null )
    	        {
    	            //System.out.println( "MATCHED: " + original + " => " + derived );
    	            addMapping( original, derived );
    	        }
    	        else
    	        {
    	           // System.out.println( "NO MATCH: " + original  );
    	            mUnmappedOriginals.put( normalize( original ), original );
    	        }
    	    }
	    }
	
private java.lang.Stringnormalize(java.lang.String s)

	    return stripDashes( s ).toLowerCase();
	
private java.util.Mapnormalize(java.util.Set s)

    	    
	    final Map<String,String>   result  = new HashMap<String,String>();
	    
	    for( final String name : s )
	    {
	        result.put( normalize( name ), name );
	    }

	    return result;
	
public java.lang.StringoriginalToDerived(java.lang.String originalName)

		final String	derivedName	= mOriginalToDerived.get( originalName );
		return( derivedName );
	
public voidsetDebugOutput(com.sun.appserv.management.util.misc.Output debugOutput)

	    mDebug  = debugOutput;
	
private java.lang.StringstripDashes(java.lang.String name)

	
	     
	    
	
	    String  result  = name;
	    
	    if ( name.indexOf( DASH ) >= 0 )
	    {
    	    final StringBuilder builder = new StringBuilder();
    	    final int length    = name.length();
    	    
    	    boolean upperNext   = false;
    	    for( int i = 0; i < length; ++i )
    	    {
    	        final char c    = name.charAt( i );
    	        
    	        if ( c == DASH_CHAR )
    	        {
    	            upperNext   = true;
    	        }
    	        else
    	        {
    	            if ( upperNext )
    	            {
    	                builder.append( ("" + c ).toUpperCase() );
    	                upperNext   = false;
    	            }
    	            else
    	            {
    	                builder.append( c );
    	            }
    	        }
    	    }
    	    
    	    result  = builder.toString();
	    }
	    
	    return result;
	
public java.lang.StringtoString()

		final StringBuilder	builder	= new StringBuilder();
		
		final String LINE_SEPARATOR = System.getProperty( "line.separator" );
		for( final String name : getDerivedNames() )
		{
			builder.append( name );
			builder.append( " => " );
			builder.append( derivedToOriginal( name )  );
			builder.append( LINE_SEPARATOR );
		}
		
		return( builder.toString() );