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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * Copyright (c) 2006-2010 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.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. * @author chris
  35. */
  36. public class WeakList<T> implements List<T> {
  37. /** The items in this list. */
  38. private final List<WeakReference<T>> list = 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(final Collection<?> c) {
  74. final Collection<WeakReference<T>> res = new ArrayList<WeakReference<T>>();
  75. for (Object item : c) {
  76. res.add(new EquatableWeakReference(item));
  77. }
  78. return res;
  79. }
  80. /** {@inheritDoc} */
  81. @Override
  82. public int size() {
  83. cleanUp();
  84. return list.size();
  85. }
  86. /** {@inheritDoc} */
  87. @Override
  88. public boolean isEmpty() {
  89. cleanUp();
  90. return list.isEmpty();
  91. }
  92. /** {@inheritDoc} */
  93. @Override @SuppressWarnings("unchecked")
  94. public boolean contains(final Object o) {
  95. return list.contains(new EquatableWeakReference(o));
  96. }
  97. /** {@inheritDoc} */
  98. @Override
  99. public Iterator<T> iterator() {
  100. return dereferenceList(list).iterator();
  101. }
  102. /** {@inheritDoc} */
  103. @Override
  104. public Object[] toArray() {
  105. return dereferenceList(list).toArray();
  106. }
  107. /** {@inheritDoc} */
  108. @Override
  109. public <S> S[] toArray(final S[] a) {
  110. return dereferenceList(list).toArray(a);
  111. }
  112. /** {@inheritDoc} */
  113. @Override
  114. public boolean add(final T e) {
  115. return list.add(new EquatableWeakReference<T>(e));
  116. }
  117. /** {@inheritDoc} */
  118. @Override @SuppressWarnings(value = "unchecked")
  119. public boolean remove(final Object o) {
  120. return list.remove(new EquatableWeakReference(o));
  121. }
  122. /** {@inheritDoc} */
  123. @Override
  124. public boolean containsAll(final Collection<?> c) {
  125. return dereferenceList(list).containsAll(c);
  126. }
  127. /** {@inheritDoc} */
  128. @Override
  129. public boolean addAll(final Collection<? extends T> c) {
  130. return list.addAll(referenceCollection(c));
  131. }
  132. /** {@inheritDoc} */
  133. @Override
  134. public boolean addAll(final int index, final Collection<? extends T> c) {
  135. return list.addAll(index, referenceCollection(c));
  136. }
  137. /** {@inheritDoc} */
  138. @Override
  139. public boolean removeAll(final Collection<?> c) {
  140. return list.removeAll(referenceCollection(c));
  141. }
  142. /** {@inheritDoc} */
  143. @Override
  144. public boolean retainAll(final Collection<?> c) {
  145. return list.retainAll(referenceCollection(c));
  146. }
  147. /** {@inheritDoc} */
  148. @Override
  149. public void clear() {
  150. list.clear();
  151. }
  152. /** {@inheritDoc} */
  153. @Override
  154. public T get(final int index) {
  155. cleanUp();
  156. return list.get(index).get();
  157. }
  158. /** {@inheritDoc} */
  159. @Override
  160. public T set(final int index, final T element) {
  161. list.set(index, new EquatableWeakReference<T>(element));
  162. return element;
  163. }
  164. /** {@inheritDoc} */
  165. @Override
  166. public void add(final int index, final T element) {
  167. list.add(index, new EquatableWeakReference<T>(element));
  168. }
  169. /** {@inheritDoc} */
  170. @Override
  171. public T remove(final int index) {
  172. return list.remove(index).get();
  173. }
  174. /** {@inheritDoc} */
  175. @Override
  176. public int indexOf(final Object o) {
  177. cleanUp();
  178. return list.indexOf(o);
  179. }
  180. /** {@inheritDoc} */
  181. @Override
  182. public int lastIndexOf(final Object o) {
  183. cleanUp();
  184. return list.lastIndexOf(o);
  185. }
  186. /** {@inheritDoc} */
  187. @Override
  188. public ListIterator<T> listIterator() {
  189. cleanUp();
  190. return dereferenceList(list).listIterator();
  191. }
  192. /** {@inheritDoc} */
  193. @Override
  194. public ListIterator<T> listIterator(final int index) {
  195. cleanUp();
  196. return dereferenceList(list).listIterator(index);
  197. }
  198. /** {@inheritDoc} */
  199. @Override
  200. public List<T> subList(final int fromIndex, final int toIndex) {
  201. return dereferenceList(list.subList(fromIndex, toIndex));
  202. }
  203. }