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

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