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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  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.collections;
  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(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. */
  46. public class MapList<S, T> {
  47. /** Our internal map. */
  48. protected final Map<S, List<T>> map;
  49. /**
  50. * Creates a new, empty MapList.
  51. */
  52. public MapList() {
  53. map = new HashMap<>();
  54. }
  55. /**
  56. * Creates a new MapList with the values from the specified list.
  57. *
  58. * @param list The MapList whose values should be used
  59. */
  60. public MapList(final MapList<S, T> list) {
  61. map = list.getMap();
  62. }
  63. /**
  64. * Determines if this MapList is empty. An empty MapList is one that either
  65. * contains no keys, or contains only keys which have no associated values.
  66. *
  67. * @return True if this MapList is empty, false otherwise
  68. */
  69. public boolean isEmpty() {
  70. for (List<T> list : map.values()) {
  71. if (!list.isEmpty()) {
  72. return false;
  73. }
  74. }
  75. return true;
  76. }
  77. /**
  78. * Determines if this MapList contains the specified key.
  79. *
  80. * @param key The key to look for
  81. * @return True if this MapList contains the specified key, false otherwise
  82. */
  83. public boolean containsKey(final S key) {
  84. return map.containsKey(key);
  85. }
  86. /**
  87. * Determines if this MapList contains the specified value as a child of
  88. * the specified key.
  89. *
  90. * @param key The key to search under
  91. * @param value The value to look for
  92. * @return True if this MapList contains the specified key/value pair,
  93. * false otherwise
  94. */
  95. public boolean containsValue(final S key, final T value) {
  96. return map.containsKey(key) && map.get(key).contains(value);
  97. }
  98. /**
  99. * Retrieves the list of values associated with the specified key.
  100. *
  101. * @param key The key whose values are being retrieved
  102. * @return The values belonging to the specified key
  103. */
  104. public List<T> get(final S key) {
  105. return map.get(key);
  106. }
  107. /**
  108. * Retrieves the value at the specified offset of the specified key.
  109. *
  110. * @param key The key whose values are being retrieved
  111. * @param index The index of the value to retrieve
  112. * @return The specified value of the key
  113. */
  114. public T get(final S key, final int index) {
  115. return map.get(key).get(index);
  116. }
  117. /**
  118. * Retrieves the list of values associated with the specified key, creating
  119. * the key if necessary.
  120. *
  121. * @param key The key to retrieve
  122. * @return A list of the specified key's values
  123. */
  124. public List<T> safeGet(final S key) {
  125. if (!map.containsKey(key)) {
  126. map.put(key, Collections.synchronizedList(new ArrayList<>()));
  127. }
  128. return map.get(key);
  129. }
  130. /**
  131. * Adds the specified key to the MapList.
  132. *
  133. * @param key The key to be added
  134. */
  135. public void add(final S key) {
  136. safeGet(key);
  137. }
  138. /**
  139. * Adds the specified value as a child of the specified key. If the key
  140. * didn't previous exist, it is created.
  141. *
  142. * @param key The key to which the value is being added
  143. * @param value The value to be added
  144. */
  145. public void add(final S key, final T value) {
  146. safeGet(key).add(value);
  147. }
  148. /**
  149. * Adds the specified set of values to the specified key. If the key
  150. * didn't previous exist, it is created.
  151. *
  152. * @param key The key to which the value is being added
  153. * @param values The values to be added
  154. */
  155. public void add(final S key, final Collection<T> values) {
  156. safeGet(key).addAll(values);
  157. }
  158. /**
  159. * Removes the specified key and all of its values.
  160. *
  161. * @param key The key to remove
  162. */
  163. public void remove(final S key) {
  164. map.remove(key);
  165. }
  166. /**
  167. * Removes the specified value from all keys.
  168. *
  169. * @param value The value to remove
  170. */
  171. public void removeFromAll(final T value) {
  172. for (List<T> list : map.values()) {
  173. list.remove(value);
  174. }
  175. }
  176. /**
  177. * Removes the specified value from the specified key.
  178. *
  179. * @param key The key whose value is being removed
  180. * @param value The value to be removed
  181. */
  182. public void remove(final S key, final T value) {
  183. if (map.containsKey(key)) {
  184. map.get(key).remove(value);
  185. }
  186. }
  187. /**
  188. * Entirely clears this MapList.
  189. */
  190. public void clear() {
  191. map.clear();
  192. }
  193. /**
  194. * Clears all values of the specified key.
  195. *
  196. * @param key The key to be cleared
  197. */
  198. public void clear(final S key) {
  199. safeGet(key).clear();
  200. }
  201. /**
  202. * Returns the set of all keys belonging to this MapList.
  203. *
  204. * @return This MapList's keyset
  205. */
  206. public Set<S> keySet() {
  207. return map.keySet();
  208. }
  209. /**
  210. * Returns a collection of all values belonging to the specified key.
  211. *
  212. * @param key The key whose values are being sought
  213. * @return A collection of values belonging to the key
  214. */
  215. public Collection<T> values(final S key) {
  216. return map.get(key);
  217. }
  218. /**
  219. * Retrieves the entry set for this MapList.
  220. *
  221. * @return This MapList's entry set
  222. */
  223. public Set<Map.Entry<S, List<T>>> entrySet() {
  224. return map.entrySet();
  225. }
  226. /**
  227. * Retrieves the map behind this maplist.
  228. *
  229. * @return This MapList's map.
  230. */
  231. public Map<S, List<T>> getMap() {
  232. return new HashMap<>(map);
  233. }
  234. }