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.

WeakList.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * Copyright (c) 2006-2013 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.lang.ref.WeakReference;
  24. import java.util.ArrayList;
  25. import java.util.Collection;
  26. import java.util.Iterator;
  27. import java.util.List;
  28. import java.util.ListIterator;
  29. /**
  30. * Implements a list of weak references. The weak references (and subsequent
  31. * garbage collection) are handled transparently.
  32. *
  33. * @param <T> The type of object that this list will contain.
  34. */
  35. public class WeakList<T> implements List<T> {
  36. /** The items in this list. */
  37. private final List<WeakReference<T>> list
  38. = new ArrayList<WeakReference<T>>();
  39. /**
  40. * Removes any entries from the list that have been GC'd.
  41. */
  42. private void cleanUp() {
  43. for (int i = 0; i < list.size(); i++) {
  44. if (list.get(i).get() == null) {
  45. list.remove(i--);
  46. }
  47. }
  48. }
  49. /**
  50. * Dereferences the specified list of WeakReferences to get a plain List.
  51. *
  52. * @param list The list to be dereferenced
  53. * @return A list containing the items referenced by the specified list
  54. */
  55. private List<T> dereferenceList(final List<WeakReference<T>> list) {
  56. final List<T> res = new ArrayList<T>();
  57. for (WeakReference<T> item : list) {
  58. if (item.get() != null) {
  59. res.add(item.get());
  60. }
  61. }
  62. return res;
  63. }
  64. /**
  65. * Creates a new collection of weak references to elements in the specified
  66. * collection.
  67. *
  68. * @param c The collection whose elements should be referenced
  69. * @return A copy of the specified collection, with each item wrapped in
  70. * a weak reference.
  71. */
  72. @SuppressWarnings({"unchecked", "rawtypes"})
  73. private Collection<WeakReference<T>> referenceCollection(
  74. final Collection<?> c) {
  75. final Collection<WeakReference<T>> res
  76. = new ArrayList<WeakReference<T>>();
  77. for (Object item : c) {
  78. res.add(new EquatableWeakReference(item));
  79. }
  80. return res;
  81. }
  82. /** {@inheritDoc} */
  83. @Override
  84. public int size() {
  85. cleanUp();
  86. return list.size();
  87. }
  88. /** {@inheritDoc} */
  89. @Override
  90. public boolean isEmpty() {
  91. cleanUp();
  92. return list.isEmpty();
  93. }
  94. /** {@inheritDoc} */
  95. @Override
  96. @SuppressWarnings({"unchecked", "rawtypes"})
  97. public boolean contains(final Object o) {
  98. return list.contains(new EquatableWeakReference(o));
  99. }
  100. /** {@inheritDoc} */
  101. @Override
  102. public Iterator<T> iterator() {
  103. return dereferenceList(list).iterator();
  104. }
  105. /** {@inheritDoc} */
  106. @Override
  107. public Object[] toArray() {
  108. return dereferenceList(list).toArray();
  109. }
  110. /** {@inheritDoc} */
  111. @Override
  112. public <S> S[] toArray(final S[] a) {
  113. return dereferenceList(list).toArray(a);
  114. }
  115. /** {@inheritDoc} */
  116. @Override
  117. public boolean add(final T e) {
  118. return list.add(new EquatableWeakReference<T>(e));
  119. }
  120. /** {@inheritDoc} */
  121. @Override
  122. @SuppressWarnings({"unchecked", "rawtypes"})
  123. public boolean remove(final Object o) {
  124. return list.remove(new EquatableWeakReference(o));
  125. }
  126. /** {@inheritDoc} */
  127. @Override
  128. public boolean containsAll(final Collection<?> c) {
  129. return dereferenceList(list).containsAll(c);
  130. }
  131. /** {@inheritDoc} */
  132. @Override
  133. public boolean addAll(final Collection<? extends T> c) {
  134. return list.addAll(referenceCollection(c));
  135. }
  136. /** {@inheritDoc} */
  137. @Override
  138. public boolean addAll(final int index, final Collection<? extends T> c) {
  139. return list.addAll(index, referenceCollection(c));
  140. }
  141. /** {@inheritDoc} */
  142. @Override
  143. public boolean removeAll(final Collection<?> c) {
  144. return list.removeAll(referenceCollection(c));
  145. }
  146. /** {@inheritDoc} */
  147. @Override
  148. public boolean retainAll(final Collection<?> c) {
  149. return list.retainAll(referenceCollection(c));
  150. }
  151. /** {@inheritDoc} */
  152. @Override
  153. public void clear() {
  154. list.clear();
  155. }
  156. /** {@inheritDoc} */
  157. @Override
  158. public T get(final int index) {
  159. cleanUp();
  160. return list.get(index).get();
  161. }
  162. /** {@inheritDoc} */
  163. @Override
  164. public T set(final int index, final T element) {
  165. list.set(index, new EquatableWeakReference<T>(element));
  166. return element;
  167. }
  168. /** {@inheritDoc} */
  169. @Override
  170. public void add(final int index, final T element) {
  171. list.add(index, new EquatableWeakReference<T>(element));
  172. }
  173. /** {@inheritDoc} */
  174. @Override
  175. public T remove(final int index) {
  176. return list.remove(index).get();
  177. }
  178. /** {@inheritDoc} */
  179. @Override
  180. public int indexOf(final Object o) {
  181. cleanUp();
  182. return dereferenceList(list).indexOf(o);
  183. }
  184. /** {@inheritDoc} */
  185. @Override
  186. public int lastIndexOf(final Object o) {
  187. cleanUp();
  188. return dereferenceList(list).lastIndexOf(o);
  189. }
  190. /** {@inheritDoc} */
  191. @Override
  192. public ListIterator<T> listIterator() {
  193. cleanUp();
  194. return dereferenceList(list).listIterator();
  195. }
  196. /** {@inheritDoc} */
  197. @Override
  198. public ListIterator<T> listIterator(final int index) {
  199. cleanUp();
  200. return dereferenceList(list).listIterator(index);
  201. }
  202. /** {@inheritDoc} */
  203. @Override
  204. public List<T> subList(final int fromIndex, final int toIndex) {
  205. return dereferenceList(list.subList(fromIndex, toIndex));
  206. }
  207. }