FileDocCategorySizeDatePackage
MapUtil.javaAPI DocGlassfish v2 API7830Fri May 04 22:31:06 BST 2007com.sun.appserv.management.util.misc

MapUtil

public final class MapUtil extends Object

Fields Summary
Constructors Summary
private MapUtil()

		// disallow instantiation
	
Methods Summary
public static java.lang.Object[]getKeyObjects(java.util.Map m)

		return( SetUtil.toArray( m.keySet() ) );
	
public static java.lang.String[]getKeyStrings(java.util.Map m)

		return( GSetUtil.toSortedStringArray( m.keySet() ) );
	
public static java.util.SetgetNullValueKeys(java.util.Map m)

		final Set<K>	s	= new HashSet<K>();
		
		for( final K key : m.keySet() )
		{
			if ( m.get( key ) == null )
			{
				s.add( key );
			}
		}
		return( s );
	
public static VgetWithDefault(java.util.Map m, K key, V defaultValue)

	    return m.containsKey( key ) ? m.get( key ) : defaultValue;
	
public static booleanisAllStrings(java.util.Map m)

return
true if non-null Map and all keys and values are of type java.lang.String

	    return m != null && CollectionUtil.isAllStrings( m.keySet() ) &&
	            CollectionUtil.isAllStrings( m.values() );
	
public static booleanmapsEqual(java.util.Map m1, java.util.Map m2)

		if ( m1 == m2 )
		{
			return( true );
		}
		
		boolean	equal	= false;
		
		if ( m1.size() == m2.size() &&
			m1.keySet().equals( m2.keySet() ) )
		{
			equal	= true;
			
			for( final Object key : m1.keySet() )
			{
				final Object	value1	= m1.get( key );
				final Object	value2	= m2.get( key );
				
				if ( ! CompareUtil.objectsEqual( value1, value2 ) )
				{
					equal	= false;
					break;
				}
			}
		}
		
		return( equal );
	
public static java.util.MapnewMap(java.lang.String key, V value)
Create a new Map consisting of a single key/value pair.

		final Map<String,V>	m	= new HashMap<String,V>();
		
		m.put( key, value );
		
		return( m );
	
public static java.util.MapnewMap(java.util.Map m1, java.util.Map m2)
Create a new Map consisting of a single key/value pair.

		final Map<K,V>	m	= new HashMap<K,V>();
		
		if ( m1 != null )
		{
			m.putAll( m1 );
		}
		if ( m2 != null )
		{
			m.putAll( m2 );
		}
		
		return( m );
	
public static java.util.MapnewMap(T[] mappings)
Create a new Map and insert the specified mappings as found in 'mappings'. The even-numbered entries are the keys, and the odd-numbered entries are the values.

		if ( (mappings.length % 2) != 0 )
		{
			throw new IllegalArgumentException( "mappings must have even length" );
		}
		
		final Map<T,T> m	= new HashMap<T,T>();
		
		for( int i = 0; i < mappings.length; i += 2 )
		{
			m.put( mappings[ i ], mappings[ i + 1 ] );
		}
		
		return( m );
	
public static java.util.MapnewMap(java.lang.String[] mappings)
Create a new Map and insert the specified mappings as found in 'mappings'. The even-numbered entries are the keys, and the odd-numbered entries are the values.

		if ( (mappings.length % 2) != 0 )
		{
			throw new IllegalArgumentException( "mappings must have even length" );
		}
		
		final Map<String,String>	m	= new HashMap<String,String>();
		
		for( int i = 0; i < mappings.length; i += 2 )
		{
			m.put( mappings[ i ], mappings[ i + 1 ] );
		}
		
		return( m );
	
public static java.util.MapnewMapNoNullValues(java.util.Map m)

		final Map<K,V>	result	= new HashMap<K,V>();
		
		for ( final K key : m.keySet() )
		{
			final V	value	= m.get( key );
			
			if ( value != null )
			{
				result.put( key, value );
			}
		}
		
		return( result );
	
public static voidremoveAll(java.util.Map m, java.util.Set s)

	    for( final K key : s )
	    {
	        m.remove( key );
	    }
	
public static voidremoveAll(java.util.Map m, T[] keys)
Remove all entries keyed by 'keys'

		for( int i = 0; i < keys.length; ++i )
		{
			m.remove( keys[ i ] );
		}
	
public static java.util.MaptoMap(java.util.Properties props, java.lang.Class kClass, java.lang.Class vClass)

	    return TypeCast.checkMap( props, kClass, vClass );
	
public static java.lang.StringtoString(java.util.Map m)

		return( toString( m, "," ) );
	
public static java.lang.StringtoString(java.util.Map m, java.lang.String separator)

		if ( m == null )
		{
			return( "null" );
		}
		
		final StringBuffer	buf	= new StringBuffer();
		
		final String[]  keyStrings  = getKeyStrings( m );
		for( final String key : keyStrings )
		{
			final Object	value	= m.get( key );
			
			buf.append( key );
			buf.append( "=" );
			buf.append( SmartStringifier.toString( value ) );
			buf.append( separator );
		}
		if ( buf.length() != 0 )
		{
			// strip trailing separator
			buf.setLength( buf.length() - separator.length() );
		}
		
		return( buf.toString() );
	
public static java.util.MaptoStringStringMap(java.util.Map m)
Convert an arbitrary Map to one whose keys and values are both of type String.

	    if ( m == null )
	    {
	        return null;
	    }
	    
	    Map<String,String>  result  = null;
	    
	    if ( isAllStrings( m ) )
	    {
	        result  = TypeCast.asMap( m );
	    }
	    else
	    {
	        result = new HashMap<String,String>();
	        
    	    for( final Object key : m.keySet() )
    	    {
    	        final Object value = m.get( key );
    	        
    	        if ( (key instanceof String) && (value instanceof String) )
    	        {
    	            result.put( (String)key, (String)value );
    	        }
    	        else
    	        {
    	            result.put( "" + key, "" + value );
    	        }
    	    }
	    }
	    
	    return result;