All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alee.utils.MapUtils Maven / Gradle / Ivy

The newest version!
/*
 * This file is part of WebLookAndFeel library.
 *
 * WebLookAndFeel library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * WebLookAndFeel library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with WebLookAndFeel library.  If not, see .
 */

package com.alee.utils;

import com.alee.api.jdk.Objects;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * This class provides a set of utilities to work with various {@link Map} implementations.
 *
 * @author Mikle Garin
 */
public final class MapUtils
{
    /**
     * Private constructor to avoid instantiation.
     */
    private MapUtils ()
    {
        throw new UtilityException ( "Utility classes are not meant to be instantiated" );
    }

    /**
     * Returns whether specified {@link Map} is empty or not.
     *
     * @param map {@link Map} to process
     * @return {@code true} if specified {@link Map} is empty, {@code false} otherwise
     */
    public static boolean isEmpty ( final Map map )
    {
        return map == null || map.isEmpty ();
    }

    /**
     * Returns whether specified {@link Map} is empty or not.
     *
     * @param map {@link Map} to process
     * @return {@code true} if specified {@link Map} is not empty, {@code false} otherwise
     */
    public static boolean notEmpty ( final Map map )
    {
        return map != null && !map.isEmpty ();
    }

    /**
     * Returns non-{@code null} {@link Map} that is either specified {@code map} or new empty {@link HashMap}.
     *
     * @param map {@link Map}
     * @param  map key type
     * @param  map value type
     * @return non-{@code null} {@link Map} that is either specified {@code map} or new empty {@link HashMap}
     */
    public static  Map nonNull ( final Map map )
    {
        return map != null ? map : new HashMap ( 0 );
    }

    /**
     * Returns copied Map.
     *
     * @param map map to copy
     * @param  map key type
     * @param  map value type
     * @return copied Map
     */
    public static  HashMap copyMap ( final Map map )
    {
        return new HashMap ( map );
    }

    /**
     * Returns copied HashMap.
     *
     * @param map HashMap to copy
     * @param  HashMap key type
     * @param  HashMap value type
     * @return copied HashMap
     */
    public static  HashMap copyHashMap ( final HashMap map )
    {
        return new HashMap ( map );
    }

    /**
     * Returns copied LinkedHashMap.
     *
     * @param map LinkedHashMap to copy
     * @param  LinkedHashMap key type
     * @param  LinkedHashMap value type
     * @return copied LinkedHashMap
     */
    public static  LinkedHashMap copyLinkedHashMap ( final LinkedHashMap map )
    {
        return new LinkedHashMap ( map );
    }

    /**
     * Returns newly created HashMap with the specified map data.
     *
     * @param data data map
     * @param   key type
     * @param   value type
     * @return newly created HashMap
     */
    public static  HashMap newHashMap ( final Map data )
    {
        return new HashMap ( data );
    }

    /**
     * Returns newly created HashMap with the specified key and value pair added.
     *
     * @param key   key to add
     * @param value value to add
     * @param    key type
     * @param    value type
     * @return newly created HashMap
     */
    public static  HashMap newHashMap ( final K key, final V value )
    {
        final HashMap map = new HashMap ( 1 );
        map.put ( key, value );
        return map;
    }

    /**
     * Returns newly created HashMap with the specified key and value pairs added.
     *
     * @param objects mixed keys and values
     * @param      key type
     * @param      value type
     * @return newly created HashMap
     */
    public static  HashMap newHashMap ( final Object... objects )
    {
        if ( objects != null && objects.length > 0 )
        {
            if ( objects.length % 2 == 0 )
            {
                final HashMap map = new HashMap ( 1 );
                for ( int i = 0; i < objects.length; i += 2 )
                {
                    map.put ( ( K ) objects[ i ], ( V ) objects[ i + 1 ] );
                }
                return map;
            }
            else
            {
                throw new RuntimeException ( "Amount of key-value objects must be even" );
            }
        }
        else
        {
            return new HashMap ( 0 );
        }
    }

    /**
     * Returns newly created LinkedHashMap with the specified key and value pair added.
     *
     * @param key   key to add
     * @param value value to add
     * @param    key type
     * @param    value type
     * @return newly created LinkedHashMap
     */
    public static  LinkedHashMap newLinkedHashMap ( final K key, final V value )
    {
        final LinkedHashMap map = new LinkedHashMap ( 1 );
        map.put ( key, value );
        return map;
    }

    /**
     * Returns newly created LinkedHashMap with the specified key and value pairs added.
     *
     * @param objects key-value pairs
     * @param      key type
     * @param      value type
     * @return newly created LinkedHashMap
     */
    public static  LinkedHashMap newLinkedHashMap ( final Object... objects )
    {
        if ( objects != null && objects.length > 0 )
        {
            if ( objects.length % 2 == 0 )
            {
                final LinkedHashMap map = new LinkedHashMap ( 1 );
                for ( int i = 0; i < objects.length; i += 2 )
                {
                    map.put ( ( K ) objects[ i ], ( V ) objects[ i + 1 ] );
                }
                return map;
            }
            else
            {
                throw new RuntimeException ( "Amount of key-value objects must be even" );
            }
        }
        else
        {
            return new LinkedHashMap ( 0 );
        }
    }

    /**
     * Removes all map entries where value is the same as the specified one.
     *
     * @param map   map to remove entries from
     * @param value value for which entries should be removed
     * @param    key type
     * @param    value type
     */
    public static  void removeAllValues ( final Map map, final V value )
    {
        final Iterator> iterator = map.entrySet ().iterator ();
        while ( iterator.hasNext () )
        {
            final Map.Entry entry = iterator.next ();
            if ( Objects.equals ( entry.getValue (), value ) )
            {
                iterator.remove ();
            }
        }
    }

    /**
     * Merges specified maps into one new map and returns it.
     *
     * @param maps maps to merge into new one
     * @param   key type
     * @param   value type
     * @return new map containing all provided maps merged into it
     */
    public static  HashMap merge ( final Map... maps )
    {
        // Preparing new map size
        int size = 0;
        for ( final Map map : maps )
        {
            if ( map != null )
            {
                size += map.size ();
            }
        }

        // Creating and filling new map
        final HashMap merged = new HashMap ( size );
        for ( final Map map : maps )
        {
            if ( map != null )
            {
                merged.putAll ( map );
            }
        }
        return merged;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy