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.

ObservableListDecorator.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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.util.Collection;
  24. import java.util.Iterator;
  25. import java.util.List;
  26. import java.util.ListIterator;
  27. import javax.annotation.Nonnull;
  28. /**
  29. * Decorates a {@link List} to add observable functionality.
  30. *
  31. * @param <T> The type of object the list contains
  32. */
  33. public class ObservableListDecorator<T> implements ObservableList<T> {
  34. /** The list being decorated. */
  35. private final List<T> list;
  36. /** The listeners for this list. */
  37. private final ListenerList listeners = new ListenerList();
  38. /**
  39. * Creates a new {@link ObservableListDecorator} which will decorate the
  40. * given list.
  41. *
  42. * @param list The list to be decorated
  43. */
  44. @SuppressWarnings("AssignmentToCollectionOrArrayFieldFromParameter")
  45. public ObservableListDecorator(final List<T> list) {
  46. this.list = list;
  47. }
  48. @Override
  49. public void addListListener(final ListObserver listener) {
  50. listeners.add(ListObserver.class, listener);
  51. }
  52. @Override
  53. public void removeListListener(final ListObserver listener) {
  54. listeners.remove(ListObserver.class, listener);
  55. }
  56. @Override
  57. public int size() {
  58. return list.size();
  59. }
  60. @Override
  61. public boolean isEmpty() {
  62. return list.isEmpty();
  63. }
  64. @Override
  65. public boolean contains(final Object o) {
  66. return list.contains(o);
  67. }
  68. @Nonnull
  69. @Override
  70. public Iterator<T> iterator() {
  71. return list.iterator();
  72. }
  73. @Nonnull
  74. @Override
  75. public Object[] toArray() {
  76. return list.toArray();
  77. }
  78. @Nonnull
  79. @Override
  80. public <S> S[] toArray(@Nonnull final S[] a) {
  81. return list.toArray(a);
  82. }
  83. @Override
  84. public boolean add(final T e) {
  85. list.add(e);
  86. listeners.getCallable(ListObserver.class).onItemsAdded(this,
  87. list.size() - 1, list.size() - 1);
  88. return true;
  89. }
  90. @Override
  91. public boolean remove(final Object o) {
  92. final int index = list.indexOf(o);
  93. if (list.remove(o)) {
  94. listeners.getCallable(ListObserver.class).onItemsRemoved(this,
  95. index, index);
  96. return true;
  97. }
  98. return false;
  99. }
  100. @Override
  101. public boolean containsAll(@Nonnull final Collection<?> c) {
  102. return list.containsAll(c);
  103. }
  104. @Override
  105. public boolean addAll(@Nonnull final Collection<? extends T> c) {
  106. if (list.addAll(c)) {
  107. listeners.getCallable(ListObserver.class).onItemsAdded(this,
  108. list.size() - c.size(), list.size() - 1);
  109. return true;
  110. }
  111. return false;
  112. }
  113. @Override
  114. public boolean addAll(final int index, @Nonnull final Collection<? extends T> c) {
  115. if (list.addAll(index, c)) {
  116. listeners.getCallable(ListObserver.class).onItemsAdded(this,
  117. index, index + c.size());
  118. return true;
  119. }
  120. return false;
  121. }
  122. @Override
  123. public boolean removeAll(@Nonnull final Collection<?> c) {
  124. final int length = list.size();
  125. if (list.removeAll(c)) {
  126. listeners.getCallable(ListObserver.class).onItemsChanged(this, 0,
  127. length - 1);
  128. return true;
  129. }
  130. return false;
  131. }
  132. @Override
  133. public boolean retainAll(@Nonnull final Collection<?> c) {
  134. final int length = list.size();
  135. if (list.retainAll(c)) {
  136. listeners.getCallable(ListObserver.class).onItemsChanged(this, 0,
  137. length - 1);
  138. return true;
  139. }
  140. return false;
  141. }
  142. @Override
  143. public void clear() {
  144. final int length = list.size();
  145. list.clear();
  146. if (length > 0) {
  147. listeners.getCallable(ListObserver.class).onItemsRemoved(this, 0,
  148. length - 1);
  149. }
  150. }
  151. @Override
  152. public T get(final int index) {
  153. return list.get(index);
  154. }
  155. @Override
  156. public T set(final int index, final T element) {
  157. final T res = list.set(index, element);
  158. listeners.getCallable(ListObserver.class).onItemsChanged(this, index, index);
  159. return res;
  160. }
  161. @Override
  162. public void add(final int index, final T element) {
  163. list.add(index, element);
  164. listeners.getCallable(ListObserver.class).onItemsAdded(this, index, index);
  165. }
  166. @Override
  167. public T remove(final int index) {
  168. final T res = list.remove(index);
  169. listeners.getCallable(ListObserver.class).onItemsRemoved(this, index, index);
  170. return res;
  171. }
  172. @Override
  173. public int indexOf(final Object o) {
  174. return list.indexOf(o);
  175. }
  176. @Override
  177. public int lastIndexOf(final Object o) {
  178. return list.lastIndexOf(o);
  179. }
  180. @Nonnull
  181. @Override
  182. public ListIterator<T> listIterator() {
  183. return list.listIterator();
  184. }
  185. @Nonnull
  186. @Override
  187. public ListIterator<T> listIterator(final int index) {
  188. return list.listIterator(index);
  189. }
  190. @Nonnull
  191. @Override
  192. public List<T> subList(final int fromIndex, final int toIndex) {
  193. return list.subList(fromIndex, toIndex);
  194. }
  195. }