Java IRC bot
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.0KB

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