com.alee.utils.MapUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of weblaf-core Show documentation
Show all versions of weblaf-core Show documentation
Core components for WebLaf
/*
* 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.utils.collection.DoubleMap;
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 maps.
*
* @author Mikle Garin
*/
public final class MapUtils
{
/**
* Returns whether specified map is empty or not.
*
* @param map map to process
* @return true if specified map is empty, false otherwise
*/
public static boolean isEmpty ( final Map, ?> map )
{
return map == null || map.isEmpty ();
}
/**
* 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 copied DoubleMap.
*
* @param map DoubleMap to copy
* @param DoubleMap key type
* @param DoubleMap value type
* @return copied DoubleMap
*/
public static DoubleMap copyDoubleMap ( final DoubleMap map )
{
return new DoubleMap ( map );
}
/**
* 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 key-value pairs
* @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 ( CompareUtils.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