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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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(value = "unchecked")
  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 @SuppressWarnings("unchecked")
  96. public boolean contains(final Object o) {
  97. return list.contains(new EquatableWeakReference(o));
  98. }
  99. /** {@inheritDoc} */
  100. @Override
  101. public Iterator<T> iterator() {
  102. return dereferenceList(list).iterator();
  103. }
  104. /** {@inheritDoc} */
  105. @Override
  106. public Object[] toArray() {
  107. return dereferenceList(list).toArray();
  108. }
  109. /** {@inheritDoc} */
  110. @Override
  111. public <S> S[] toArray(final S[] a) {
  112. return dereferenceList(list).toArray(a);
  113. }
  114. /** {@inheritDoc} */
  115. @Override
  116. public boolean add(final T e) {
  117. return list.add(new EquatableWeakReference<T>(e));
  118. }
  119. /** {@inheritDoc} */
  120. @Override @SuppressWarnings(value = "unchecked")
  121. public boolean remove(final Object o) {
  122. return list.remove(new EquatableWeakReference(o));
  123. }
  124. /** {@inheritDoc} */
  125. @Override
  126. public boolean containsAll(final Collection<?> c) {
  127. return dereferenceList(list).containsAll(c);
  128. }
  129. /** {@inheritDoc} */
  130. @Override
  131. public boolean addAll(final Collection<? extends T> c) {
  132. return list.addAll(referenceCollection(c));
  133. }
  134. /** {@inheritDoc} */
  135. @Override
  136. public boolean addAll(final int index, final Collection<? extends T> c) {
  137. return list.addAll(index, referenceCollection(c));
  138. }
  139. /** {@inheritDoc} */
  140. @Override
  141. public boolean removeAll(final Collection<?> c) {
  142. return list.removeAll(referenceCollection(c));
  143. }
  144. /** {@inheritDoc} */
  145. @Override
  146. public boolean retainAll(final Collection<?> c) {
  147. return list.retainAll(referenceCollection(c));
  148. }
  149. /** {@inheritDoc} */
  150. @Override
  151. public void clear() {
  152. list.clear();
  153. }
  154. /** {@inheritDoc} */
  155. @Override
  156. public T get(final int index) {
  157. cleanUp();
  158. return list.get(index).get();
  159. }
  160. /** {@inheritDoc} */
  161. @Override
  162. public T set(final int index, final T element) {
  163. list.set(index, new EquatableWeakReference<T>(element));
  164. return element;
  165. }
  166. /** {@inheritDoc} */
  167. @Override
  168. public void add(final int index, final T element) {
  169. list.add(index, new EquatableWeakReference<T>(element));
  170. }
  171. /** {@inheritDoc} */
  172. @Override
  173. public T remove(final int index) {
  174. return list.remove(index).get();
  175. }
  176. /** {@inheritDoc} */
  177. @Override
  178. public int indexOf(final Object o) {
  179. cleanUp();
  180. return dereferenceList(list).indexOf(o);
  181. }
  182. /** {@inheritDoc} */
  183. @Override
  184. public int lastIndexOf(final Object o) {
  185. cleanUp();
  186. return dereferenceList(list).lastIndexOf(o);
  187. }
  188. /** {@inheritDoc} */
  189. @Override
  190. public ListIterator<T> listIterator() {
  191. cleanUp();
  192. return dereferenceList(list).listIterator();
  193. }
  194. /** {@inheritDoc} */
  195. @Override
  196. public ListIterator<T> listIterator(final int index) {
  197. cleanUp();
  198. return dereferenceList(list).listIterator(index);
  199. }
  200. /** {@inheritDoc} */
  201. @Override
  202. public List<T> subList(final int fromIndex, final int toIndex) {
  203. return dereferenceList(list.subList(fromIndex, toIndex));
  204. }
  205. }