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

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