class MapUtils {

    public static int[] getWidthHeight( Integer maxWidth, Integer maxHeight, int subtractWidth, int subtractHeight, double widthFactor )
    {
        if ( maxWidth == null && maxHeight == null )
        {
            throw new IllegalArgumentException( "At least one of width and height must be specified" );
        }
        
        if ( maxWidth == null )
        {
            maxHeight -= subtractHeight;
            maxWidth = (int) Math.ceil( maxHeight * widthFactor );
        }   
        else if ( maxHeight == null )
        {
            maxWidth -= subtractWidth;
            maxHeight = (int) Math.ceil( maxWidth / widthFactor );
        }
        else // Both set
        {
            maxWidth -= subtractWidth;
            maxHeight -= subtractHeight;
            
            double maxWidthFactor = (double) maxWidth / maxHeight;
            
            if ( maxWidthFactor > widthFactor ) // Canvas wider than area
            {
                maxWidth = (int) Math.ceil( maxHeight * widthFactor );
            }
            else // Area wider than canvas
            {
                maxHeight = (int) Math.ceil( maxWidth / widthFactor );
            }
        }
        
        int[] result = { maxWidth, maxHeight };
        
        return result;
    }

}