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 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * Copyright (c) 2006-2007 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.HashMap;
  26. import java.util.List;
  27. import java.util.Map;
  28. import java.util.Set;
  29. /**
  30. * Wraps a Map<S, List<T>> with various convenience methods for
  31. * accessing the data. Implements a Map-like interface for easier transition.
  32. *
  33. * @param <S> the type of keys maintained by this map
  34. * @param <T> the type of mapped values
  35. * @author chris
  36. */
  37. public class MapList<S,T> {
  38. /** Our internal map. */
  39. protected final Map<S, List<T>> map = new HashMap<S, List<T>>();
  40. /**
  41. * Determines if this MapList is empty. An empty MapList is one that either
  42. * contains no keys, or contains only keys which have no associated values.
  43. *
  44. * @return True if this MapList is empty, false otherwise
  45. */
  46. public boolean isEmpty() {
  47. for (List<T> list : map.values()) {
  48. if (!list.isEmpty()) {
  49. return false;
  50. }
  51. }
  52. return true;
  53. }
  54. /**
  55. * Determines if this MapList contains the specified key.
  56. *
  57. * @param key The key to look for
  58. * @return True if this MapList contains the specified key, false otherwise
  59. */
  60. public boolean containsKey(final S key) {
  61. return map.containsKey(key);
  62. }
  63. /**
  64. * Determines if this MapList contains the specified value as a child of
  65. * the specified key.
  66. *
  67. * @param key The key to search under
  68. * @param value The value to look for
  69. * @return True if this MapList contains the specified key/value pair,
  70. * false otherwise
  71. */
  72. public boolean containsValue(final S key, final T value) {
  73. return map.containsKey(key) && map.get(key).contains(value);
  74. }
  75. /**
  76. * Retrieves the list of values associated with the specified key.
  77. *
  78. * @param key The key whose values are being retrieved
  79. * @return The values belonging to the specified key
  80. */
  81. public List<T> get(final S key) {
  82. return map.get(key);
  83. }
  84. /**
  85. * Retrieves the value at the specified offset of the specified key.
  86. *
  87. * @param key The key whose values are being retrieved
  88. * @param index The index of the value to retrieve
  89. * @return The specified value of the key
  90. */
  91. public T get(final S key, final int index) {
  92. return map.get(key).get(index);
  93. }
  94. /**
  95. * Retrieves the list of values associated with the specified key, creating
  96. * the key if neccessary.
  97. *
  98. * @param key The key to retrieve
  99. * @return A list of the specified key's values
  100. */
  101. protected List<T> safeGet(final S key) {
  102. if (!map.containsKey(key)) {
  103. map.put(key, new ArrayList<T>());
  104. }
  105. return map.get(key);
  106. }
  107. /**
  108. * Adds the specified key to the MapList.
  109. *
  110. * @param key The key to be added
  111. */
  112. public void add(final S key) {
  113. safeGet(key);
  114. }
  115. /**
  116. * Adds the specified value as a child of the specified key. If the key
  117. * didn't previous exist, it is created.
  118. *
  119. * @param key The key to which the value is being added
  120. * @param value The value to be added
  121. */
  122. public void add(final S key, final T value) {
  123. safeGet(key).add(value);
  124. }
  125. /**
  126. * Adds the specified set of values to the specified key. If the key
  127. * didn't previous exist, it is created.
  128. *
  129. * @param key The key to which the value is being added
  130. * @param values The values to be added
  131. */
  132. public void add(final S key, final Collection<T> values) {
  133. safeGet(key).addAll(values);
  134. }
  135. /**
  136. * Removes the specified key and all of its values.
  137. *
  138. * @param key The key to remove
  139. */
  140. public void remove(final S key) {
  141. map.remove(key);
  142. }
  143. /**
  144. * Removes the specified value from all keys.
  145. *
  146. * @param value The value to remove
  147. */
  148. public void removeFromAll(final T value) {
  149. for (List<T> list : map.values()) {
  150. list.remove(value);
  151. }
  152. }
  153. /**
  154. * Removes the specified value from the specified key.
  155. *
  156. * @param key The key whose value is being removed
  157. * @param value The value to be removed
  158. */
  159. public void remove(final S key, final T value) {
  160. map.get(key).remove(value);
  161. }
  162. /**
  163. * Entirely clears this MapList.
  164. */
  165. public void clear() {
  166. map.clear();
  167. }
  168. /**
  169. * Clears all values of the specified key.
  170. *
  171. * @param key The key to be cleared
  172. */
  173. public void clear(final S key) {
  174. safeGet(key).clear();
  175. }
  176. /**
  177. * Returns the set of all keys belonging to this MapList.
  178. *
  179. * @return This MapList's keyset
  180. */
  181. public Set<S> keySet() {
  182. return map.keySet();
  183. }
  184. /**
  185. * Returns a collection of all values belonging to the specified key.
  186. *
  187. * @param key The key whose values are being sought
  188. * @return A collection of values belonging to the key
  189. */
  190. public Collection<T> values(final S key) {
  191. return map.get(key);
  192. }
  193. /**
  194. * Retrieves the entry set for this MapList.
  195. *
  196. * @return This MapList's entry set
  197. */
  198. public Set<Map.Entry<S, List<T>>> entrySet() {
  199. return map.entrySet();
  200. }
  201. }