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

WeakList.java 6.7KB

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