123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- /*
- * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- package com.dmdirc.util;
-
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.Collections;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.Set;
-
- /**
- * Wraps a Map<S, List<T>> with various convenience methods for
- * accessing the data. Implements a Map-like interface for easier transition.
- * <p>
- * <strong>Note that this implementation is not synchronized.</strong> If
- * multiple threads access a <code>MapList</code> instance concurrently, and
- * at least one of the threads modifies the map, it <em>must</em> be
- * synchronized externally.
- * <p>
- * The <code>List</code>s used to back this map are synchronized using the
- * {@link Collections#synchronizedList(java.util.List)} method, which requires
- * manual synchronization in any code iterating over the values.
- *
- * @param <S> the type of keys maintained by this map
- * @param <T> the type of mapped values
- * @author chris
- */
- public class MapList<S, T> {
-
- /** Our internal map. */
- protected final Map<S, List<T>> map;
-
- /**
- * Creates a new, empty MapList.
- */
- public MapList() {
- map = new HashMap<S, List<T>>();
- }
-
- /**
- * Creates a new MapList with the values from the specified list.
- *
- * @param list The MapList whose values should be used
- */
- public MapList(final MapList<S, T> list) {
- map = list.getMap();
- }
-
- /**
- * Determines if this MapList is empty. An empty MapList is one that either
- * contains no keys, or contains only keys which have no associated values.
- *
- * @return True if this MapList is empty, false otherwise
- */
- public boolean isEmpty() {
- for (List<T> list : map.values()) {
- if (!list.isEmpty()) {
- return false;
- }
- }
-
- return true;
- }
-
- /**
- * Determines if this MapList contains the specified key.
- *
- * @param key The key to look for
- * @return True if this MapList contains the specified key, false otherwise
- */
- public boolean containsKey(final S key) {
- return map.containsKey(key);
- }
-
- /**
- * Determines if this MapList contains the specified value as a child of
- * the specified key.
- *
- * @param key The key to search under
- * @param value The value to look for
- * @return True if this MapList contains the specified key/value pair,
- * false otherwise
- */
- public boolean containsValue(final S key, final T value) {
- return map.containsKey(key) && map.get(key).contains(value);
- }
-
- /**
- * Retrieves the list of values associated with the specified key.
- *
- * @param key The key whose values are being retrieved
- * @return The values belonging to the specified key
- */
- public List<T> get(final S key) {
- return map.get(key);
- }
-
- /**
- * Retrieves the value at the specified offset of the specified key.
- *
- * @param key The key whose values are being retrieved
- * @param index The index of the value to retrieve
- * @return The specified value of the key
- */
- public T get(final S key, final int index) {
- return map.get(key).get(index);
- }
-
- /**
- * Retrieves the list of values associated with the specified key, creating
- * the key if necessary.
- *
- * @param key The key to retrieve
- * @return A list of the specified key's values
- */
- public List<T> safeGet(final S key) {
- if (!map.containsKey(key)) {
- map.put(key, Collections.synchronizedList(new ArrayList<T>()));
- }
-
- return map.get(key);
- }
-
- /**
- * Adds the specified key to the MapList.
- *
- * @param key The key to be added
- */
- public void add(final S key) {
- safeGet(key);
- }
-
- /**
- * Adds the specified value as a child of the specified key. If the key
- * didn't previous exist, it is created.
- *
- * @param key The key to which the value is being added
- * @param value The value to be added
- */
- public void add(final S key, final T value) {
- safeGet(key).add(value);
- }
-
- /**
- * Adds the specified set of values to the specified key. If the key
- * didn't previous exist, it is created.
- *
- * @param key The key to which the value is being added
- * @param values The values to be added
- */
- public void add(final S key, final Collection<T> values) {
- safeGet(key).addAll(values);
- }
-
- /**
- * Removes the specified key and all of its values.
- *
- * @param key The key to remove
- */
- public void remove(final S key) {
- map.remove(key);
- }
-
- /**
- * Removes the specified value from all keys.
- *
- * @param value The value to remove
- */
- public void removeFromAll(final T value) {
- for (List<T> list : map.values()) {
- list.remove(value);
- }
- }
-
- /**
- * Removes the specified value from the specified key.
- *
- * @param key The key whose value is being removed
- * @param value The value to be removed
- */
- public void remove(final S key, final T value) {
- if (map.containsKey(key)) {
- map.get(key).remove(value);
- }
- }
-
- /**
- * Entirely clears this MapList.
- */
- public void clear() {
- map.clear();
- }
-
- /**
- * Clears all values of the specified key.
- *
- * @param key The key to be cleared
- */
- public void clear(final S key) {
- safeGet(key).clear();
- }
-
- /**
- * Returns the set of all keys belonging to this MapList.
- *
- * @return This MapList's keyset
- */
- public Set<S> keySet() {
- return map.keySet();
- }
-
- /**
- * Returns a collection of all values belonging to the specified key.
- *
- * @param key The key whose values are being sought
- * @return A collection of values belonging to the key
- */
- public Collection<T> values(final S key) {
- return map.get(key);
- }
-
- /**
- * Retrieves the entry set for this MapList.
- *
- * @return This MapList's entry set
- */
- public Set<Map.Entry<S, List<T>>> entrySet() {
- return map.entrySet();
- }
-
- /**
- * Retrieves the map behind this maplist.
- *
- * @return This MapList's map.
- */
- public Map<S, List<T>> getMap() {
- return new HashMap<S, List<T>>(map);
- }
-
- }
|