您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

WeakList.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Copyright (c) 2006-2011 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
  39. = new ArrayList<WeakReference<T>>();
  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. final List<T> res = new ArrayList<T>();
  58. for (WeakReference<T> item : list) {
  59. if (item.get() != null) {
  60. res.add(item.get());
  61. }
  62. }
  63. return res;
  64. }
  65. /**
  66. * Creates a new collection of weak references to elements in the specified
  67. * collection.
  68. *
  69. * @param c The collection whose elements should be referenced
  70. * @return A copy of the specified collection, with each item wrapped in
  71. * a weak reference.
  72. */
  73. @SuppressWarnings(value = "unchecked")
  74. private Collection<WeakReference<T>> referenceCollection(
  75. final Collection<?> c) {
  76. final Collection<WeakReference<T>> res
  77. = new ArrayList<WeakReference<T>>();
  78. for (Object item : c) {
  79. res.add(new EquatableWeakReference(item));
  80. }
  81. return res;
  82. }
  83. /** {@inheritDoc} */
  84. @Override
  85. public int size() {
  86. cleanUp();
  87. return list.size();
  88. }
  89. /** {@inheritDoc} */
  90. @Override
  91. public boolean isEmpty() {
  92. cleanUp();
  93. return list.isEmpty();
  94. }
  95. /** {@inheritDoc} */
  96. @Override @SuppressWarnings("unchecked")
  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 @SuppressWarnings(value = "unchecked")
  122. public boolean remove(final Object o) {
  123. return list.remove(new EquatableWeakReference(o));
  124. }
  125. /** {@inheritDoc} */
  126. @Override
  127. public boolean containsAll(final Collection<?> c) {
  128. return dereferenceList(list).containsAll(c);
  129. }
  130. /** {@inheritDoc} */
  131. @Override
  132. public boolean addAll(final Collection<? extends T> c) {
  133. return list.addAll(referenceCollection(c));
  134. }
  135. /** {@inheritDoc} */
  136. @Override
  137. public boolean addAll(final int index, final Collection<? extends T> c) {
  138. return list.addAll(index, referenceCollection(c));
  139. }
  140. /** {@inheritDoc} */
  141. @Override
  142. public boolean removeAll(final Collection<?> c) {
  143. return list.removeAll(referenceCollection(c));
  144. }
  145. /** {@inheritDoc} */
  146. @Override
  147. public boolean retainAll(final Collection<?> c) {
  148. return list.retainAll(referenceCollection(c));
  149. }
  150. /** {@inheritDoc} */
  151. @Override
  152. public void clear() {
  153. list.clear();
  154. }
  155. /** {@inheritDoc} */
  156. @Override
  157. public T get(final int index) {
  158. cleanUp();
  159. return list.get(index).get();
  160. }
  161. /** {@inheritDoc} */
  162. @Override
  163. public T set(final int index, final T element) {
  164. list.set(index, new EquatableWeakReference<T>(element));
  165. return element;
  166. }
  167. /** {@inheritDoc} */
  168. @Override
  169. public void add(final int index, final T element) {
  170. list.add(index, new EquatableWeakReference<T>(element));
  171. }
  172. /** {@inheritDoc} */
  173. @Override
  174. public T remove(final int index) {
  175. return list.remove(index).get();
  176. }
  177. /** {@inheritDoc} */
  178. @Override
  179. public int indexOf(final Object o) {
  180. cleanUp();
  181. return list.indexOf(o);
  182. }
  183. /** {@inheritDoc} */
  184. @Override
  185. public int lastIndexOf(final Object o) {
  186. cleanUp();
  187. return list.lastIndexOf(o);
  188. }
  189. /** {@inheritDoc} */
  190. @Override
  191. public ListIterator<T> listIterator() {
  192. cleanUp();
  193. return dereferenceList(list).listIterator();
  194. }
  195. /** {@inheritDoc} */
  196. @Override
  197. public ListIterator<T> listIterator(final int index) {
  198. cleanUp();
  199. return dereferenceList(list).listIterator(index);
  200. }
  201. /** {@inheritDoc} */
  202. @Override
  203. public List<T> subList(final int fromIndex, final int toIndex) {
  204. return dereferenceList(list.subList(fromIndex, toIndex));
  205. }
  206. }