You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MapList.java 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * Copyright (c) 2006-2011 Chris Smith, Shane Mc Cormack, Gregory Holmes
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.util;
  23. import java.util.ArrayList;
  24. import java.util.Collection;
  25. import java.util.Collections;
  26. import java.util.HashMap;
  27. import java.util.List;
  28. import java.util.Map;
  29. import java.util.Set;
  30. /**
  31. * Wraps a Map<S, List<T>> with various convenience methods for
  32. * accessing the data. Implements a Map-like interface for easier transition.
  33. * <p>
  34. * <strong>Note that this implementation is not synchronized.</strong> If
  35. * multiple threads access a <code>MapList</code> instance concurrently, and
  36. * at least one of the threads modifies the map, it <em>must</em> be
  37. * synchronized externally.
  38. * <p>
  39. * The <code>List</code>s used to back this map are synchronized using the
  40. * {@link Collections#synchronizedList(java.util.List)} method, which requires
  41. * manual synchronization in any code iterating over the values.
  42. *
  43. * @param <S> the type of keys maintained by this map
  44. * @param <T> the type of mapped values
  45. * @author chris
  46. */
  47. public class MapList<S, T> {
  48. /** Our internal map. */
  49. protected final Map<S, List<T>> map;
  50. /**
  51. * Creates a new, empty MapList.
  52. */
  53. public MapList() {
  54. map = new HashMap<S, List<T>>();
  55. }
  56. /**
  57. * Creates a new MapList with the values from the specified list.
  58. *
  59. * @param list The MapList whose values should be used
  60. */
  61. public MapList(final MapList<S, T> list) {
  62. map = list.getMap();
  63. }
  64. /**
  65. * Determines if this MapList is empty. An empty MapList is one that either
  66. * contains no keys, or contains only keys which have no associated values.
  67. *
  68. * @return True if this MapList is empty, false otherwise
  69. */
  70. public boolean isEmpty() {
  71. for (List<T> list : map.values()) {
  72. if (!list.isEmpty()) {
  73. return false;
  74. }
  75. }
  76. return true;
  77. }
  78. /**
  79. * Determines if this MapList contains the specified key.
  80. *
  81. * @param key The key to look for
  82. * @return True if this MapList contains the specified key, false otherwise
  83. */
  84. public boolean containsKey(final S key) {
  85. return map.containsKey(key);
  86. }
  87. /**
  88. * Determines if this MapList contains the specified value as a child of
  89. * the specified key.
  90. *
  91. * @param key The key to search under
  92. * @param value The value to look for
  93. * @return True if this MapList contains the specified key/value pair,
  94. * false otherwise
  95. */
  96. public boolean containsValue(final S key, final T value) {
  97. return map.containsKey(key) && map.get(key).contains(value);
  98. }
  99. /**
  100. * Retrieves the list of values associated with the specified key.
  101. *
  102. * @param key The key whose values are being retrieved
  103. * @return The values belonging to the specified key
  104. */
  105. public List<T> get(final S key) {
  106. return map.get(key);
  107. }
  108. /**
  109. * Retrieves the value at the specified offset of the specified key.
  110. *
  111. * @param key The key whose values are being retrieved
  112. * @param index The index of the value to retrieve
  113. * @return The specified value of the key
  114. */
  115. public T get(final S key, final int index) {
  116. return map.get(key).get(index);
  117. }
  118. /**
  119. * Retrieves the list of values associated with the specified key, creating
  120. * the key if necessary.
  121. *
  122. * @param key The key to retrieve
  123. * @return A list of the specified key's values
  124. */
  125. public List<T> safeGet(final S key) {
  126. if (!map.containsKey(key)) {
  127. map.put(key, Collections.synchronizedList(new ArrayList<T>()));
  128. }
  129. return map.get(key);
  130. }
  131. /**
  132. * Adds the specified key to the MapList.
  133. *
  134. * @param key The key to be added
  135. */
  136. public void add(final S key) {
  137. safeGet(key);
  138. }
  139. /**
  140. * Adds the specified value as a child of the specified key. If the key
  141. * didn't previous exist, it is created.
  142. *
  143. * @param key The key to which the value is being added
  144. * @param value The value to be added
  145. */
  146. public void add(final S key, final T value) {
  147. safeGet(key).add(value);
  148. }
  149. /**
  150. * Adds the specified set of values to the specified key. If the key
  151. * didn't previous exist, it is created.
  152. *
  153. * @param key The key to which the value is being added
  154. * @param values The values to be added
  155. */
  156. public void add(final S key, final Collection<T> values) {
  157. safeGet(key).addAll(values);
  158. }
  159. /**
  160. * Removes the specified key and all of its values.
  161. *
  162. * @param key The key to remove
  163. */
  164. public void remove(final S key) {
  165. map.remove(key);
  166. }
  167. /**
  168. * Removes the specified value from all keys.
  169. *
  170. * @param value The value to remove
  171. */
  172. public void removeFromAll(final T value) {
  173. for (List<T> list : map.values()) {
  174. list.remove(value);
  175. }
  176. }
  177. /**
  178. * Removes the specified value from the specified key.
  179. *
  180. * @param key The key whose value is being removed
  181. * @param value The value to be removed
  182. */
  183. public void remove(final S key, final T value) {
  184. if (map.containsKey(key)) {
  185. map.get(key).remove(value);
  186. }
  187. }
  188. /**
  189. * Entirely clears this MapList.
  190. */
  191. public void clear() {
  192. map.clear();
  193. }
  194. /**
  195. * Clears all values of the specified key.
  196. *
  197. * @param key The key to be cleared
  198. */
  199. public void clear(final S key) {
  200. safeGet(key).clear();
  201. }
  202. /**
  203. * Returns the set of all keys belonging to this MapList.
  204. *
  205. * @return This MapList's keyset
  206. */
  207. public Set<S> keySet() {
  208. return map.keySet();
  209. }
  210. /**
  211. * Returns a collection of all values belonging to the specified key.
  212. *
  213. * @param key The key whose values are being sought
  214. * @return A collection of values belonging to the key
  215. */
  216. public Collection<T> values(final S key) {
  217. return map.get(key);
  218. }
  219. /**
  220. * Retrieves the entry set for this MapList.
  221. *
  222. * @return This MapList's entry set
  223. */
  224. public Set<Map.Entry<S, List<T>>> entrySet() {
  225. return map.entrySet();
  226. }
  227. /**
  228. * Retrieves the map behind this maplist.
  229. *
  230. * @return This MapList's map.
  231. */
  232. public Map<S, List<T>> getMap() {
  233. return new HashMap<S, List<T>>(map);
  234. }
  235. }