com.alee.utils.collection.DoubleMap 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.collection;
import com.alee.utils.MapUtils;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* @author Mikle Garin
*/
public class DoubleMap implements Map
{
/**
* Values by keys map.
*/
protected Map valuesByKeys;
/**
* Keys by values map.
*/
protected Map keysByValues;
public DoubleMap ()
{
this ( 10 );
}
public DoubleMap ( final int initialCapacity )
{
super ();
valuesByKeys = new HashMap ( initialCapacity );
keysByValues = new HashMap ( initialCapacity );
}
public DoubleMap ( final Map map )
{
this ( map.size () );
putAll ( map );
}
public V getValue ( final K key )
{
return valuesByKeys.get ( key );
}
public K getKey ( final V value )
{
return keysByValues.get ( value );
}
public V removeByKey ( final K key )
{
final V removed = valuesByKeys.remove ( key );
if ( removed != null )
{
keysByValues.remove ( removed );
}
return removed;
}
public K removeByValue ( final V value )
{
final K removed = keysByValues.remove ( value );
if ( removed != null )
{
valuesByKeys.remove ( removed );
}
return removed;
}
@Override
public V put ( final K key, final V value )
{
// Removing existing entries with same key or value
final V v;
if ( valuesByKeys.containsKey ( key ) )
{
v = valuesByKeys.get ( key );
valuesByKeys.remove ( key );
keysByValues.remove ( v );
}
else
{
v = null;
}
if ( keysByValues.containsKey ( value ) )
{
final K k = keysByValues.get ( value );
valuesByKeys.remove ( k );
keysByValues.remove ( value );
}
// Adding new entry
valuesByKeys.put ( key, value );
keysByValues.put ( value, key );
return v;
}
@Override
public int size ()
{
return valuesByKeys.size ();
}
@Override
public boolean isEmpty ()
{
return valuesByKeys.isEmpty ();
}
@Override
public boolean containsKey ( final Object key )
{
return valuesByKeys.containsKey ( key );
}
@Override
public boolean containsValue ( final Object value )
{
return keysByValues.containsKey ( value );
}
@Override
public V get ( final Object key )
{
return valuesByKeys.get ( key );
}
@Override
public V remove ( final Object key )
{
return removeByKey ( ( K ) key );
}
@Override
public void putAll ( final Map extends K, ? extends V> m )
{
for ( final Map.Entry extends K, ? extends V> entry : m.entrySet () )
{
put ( entry.getKey (), entry.getValue () );
}
}
@Override
public void clear ()
{
valuesByKeys.clear ();
keysByValues.clear ();
}
@Override
public Set keySet ()
{
return valuesByKeys.keySet ();
}
@Override
public Collection values ()
{
return valuesByKeys.values ();
}
@Override
public Set> entrySet ()
{
return valuesByKeys.entrySet ();
}
public Map getValuesByKeys ()
{
return MapUtils.copyMap ( valuesByKeys );
}
public Map getKeysByValues ()
{
return MapUtils.copyMap ( keysByValues );
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy