org.opencms.util.CmsManyToOneMap Maven / Gradle / Ivy
Show all versions of opencms-test Show documentation
/*
* This library is part of OpenCms -
* the Open Source Content Management System
*
* Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This 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
* Lesser General Public License for more details.
*
* For further information about Alkacon Software, please see the
* company website: http://www.alkacon.com
*
* For further information about OpenCms, please see the
* project website: http://www.opencms.org
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.opencms.util;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps;
/**
* Special collection class which allows lookup from keys to values and from values to sets of keys.
*
* It also implements efficient removal of values, not just keys.
*
* @param the key type
* @param the value type
*/
public class CmsManyToOneMap {
/** Map from keys to values . */
private Map m_forwardMap = Maps.newHashMap();
/** Map from values to sets of keys. */
private HashMultimap m_reverseMap = HashMultimap.create();
/**
* Creates a new instance.
*/
public CmsManyToOneMap() {
// do nothing
}
/**
* Creates a new instance by copying the data from another one.
*
* @param other the other map to copy the data from
*/
public CmsManyToOneMap(CmsManyToOneMap other) {
for (Map.Entry entry : other.getForwardMap().entrySet()) {
put(entry.getKey(), entry.getValue());
}
}
/**
* Gets the value for a key.
*
* @param key the key
* @return the value for the key, or null
*/
public V get(K key) {
return m_forwardMap.get(key);
}
/**
* Associates a value with a key.
*
* @param key the key
* @param value the value
*/
public void put(K key, V value) {
m_forwardMap.put(key, value);
m_reverseMap.put(value, key);
}
/**
* Removes the entry with the given key.
*
* @param key the key
*/
public void remove(K key) {
V removedValue = m_forwardMap.remove(key);
if (removedValue != null) {
m_reverseMap.remove(removedValue, key);
}
}
/**
* Removes all entries with the given value.
*
* @param value the value
*/
public void removeValue(V value) {
Set keys = m_reverseMap.removeAll(value);
for (K key : keys) {
m_forwardMap.remove(key);
}
}
/**
* Gets the (immutable) map from keys to values.
*
* @return the map from keys to values
*/
Map getForwardMap() {
return Collections.unmodifiableMap(m_forwardMap);
}
/**
* Gets the multimap from values to keys.
*
* @return the multimap from values to keys
*/
Multimap getReverseMap() {
return Multimaps.unmodifiableSetMultimap(m_reverseMap);
}
}