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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.util.collections;
  18. import java.util.Collection;
  19. import java.util.Iterator;
  20. import java.util.List;
  21. import java.util.ListIterator;
  22. import javax.annotation.Nonnull;
  23. /**
  24. * Decorates a {@link List} to add observable functionality.
  25. *
  26. * @param <T> The type of object the list contains
  27. */
  28. public class ObservableListDecorator<T> implements ObservableList<T> {
  29. /** The list being decorated. */
  30. private final List<T> list;
  31. /** The listeners for this list. */
  32. private final ListenerList listeners = new ListenerList();
  33. /**
  34. * Creates a new {@link ObservableListDecorator} which will decorate the
  35. * given list.
  36. *
  37. * @param list The list to be decorated
  38. */
  39. @SuppressWarnings("AssignmentToCollectionOrArrayFieldFromParameter")
  40. public ObservableListDecorator(final List<T> list) {
  41. this.list = list;
  42. }
  43. @Override
  44. public void addListListener(final ListObserver listener) {
  45. listeners.add(ListObserver.class, listener);
  46. }
  47. @Override
  48. public void removeListListener(final ListObserver listener) {
  49. listeners.remove(ListObserver.class, listener);
  50. }
  51. @Override
  52. public int size() {
  53. return list.size();
  54. }
  55. @Override
  56. public boolean isEmpty() {
  57. return list.isEmpty();
  58. }
  59. @Override
  60. public boolean contains(final Object o) {
  61. return list.contains(o);
  62. }
  63. @Nonnull
  64. @Override
  65. public Iterator<T> iterator() {
  66. return list.iterator();
  67. }
  68. @Nonnull
  69. @Override
  70. public Object[] toArray() {
  71. return list.toArray();
  72. }
  73. @Nonnull
  74. @Override
  75. public <S> S[] toArray(@Nonnull final S[] a) {
  76. return list.toArray(a);
  77. }
  78. @Override
  79. public boolean add(final T e) {
  80. list.add(e);
  81. listeners.getCallable(ListObserver.class).onItemsAdded(this,
  82. list.size() - 1, list.size() - 1);
  83. return true;
  84. }
  85. @Override
  86. public boolean remove(final Object o) {
  87. final int index = list.indexOf(o);
  88. if (list.remove(o)) {
  89. listeners.getCallable(ListObserver.class).onItemsRemoved(this,
  90. index, index);
  91. return true;
  92. }
  93. return false;
  94. }
  95. @Override
  96. public boolean containsAll(@Nonnull final Collection<?> c) {
  97. return list.containsAll(c);
  98. }
  99. @Override
  100. public boolean addAll(@Nonnull final Collection<? extends T> c) {
  101. if (list.addAll(c)) {
  102. listeners.getCallable(ListObserver.class).onItemsAdded(this,
  103. list.size() - c.size(), list.size() - 1);
  104. return true;
  105. }
  106. return false;
  107. }
  108. @Override
  109. public boolean addAll(final int index, @Nonnull final Collection<? extends T> c) {
  110. if (list.addAll(index, c)) {
  111. listeners.getCallable(ListObserver.class).onItemsAdded(this,
  112. index, index + c.size());
  113. return true;
  114. }
  115. return false;
  116. }
  117. @Override
  118. public boolean removeAll(@Nonnull final Collection<?> c) {
  119. final int length = list.size();
  120. if (list.removeAll(c)) {
  121. listeners.getCallable(ListObserver.class).onItemsChanged(this, 0,
  122. length - 1);
  123. return true;
  124. }
  125. return false;
  126. }
  127. @Override
  128. public boolean retainAll(@Nonnull final Collection<?> c) {
  129. final int length = list.size();
  130. if (list.retainAll(c)) {
  131. listeners.getCallable(ListObserver.class).onItemsChanged(this, 0,
  132. length - 1);
  133. return true;
  134. }
  135. return false;
  136. }
  137. @Override
  138. public void clear() {
  139. final int length = list.size();
  140. list.clear();
  141. if (length > 0) {
  142. listeners.getCallable(ListObserver.class).onItemsRemoved(this, 0,
  143. length - 1);
  144. }
  145. }
  146. @Override
  147. public T get(final int index) {
  148. return list.get(index);
  149. }
  150. @Override
  151. public T set(final int index, final T element) {
  152. final T res = list.set(index, element);
  153. listeners.getCallable(ListObserver.class).onItemsChanged(this, index, index);
  154. return res;
  155. }
  156. @Override
  157. public void add(final int index, final T element) {
  158. list.add(index, element);
  159. listeners.getCallable(ListObserver.class).onItemsAdded(this, index, index);
  160. }
  161. @Override
  162. public T remove(final int index) {
  163. final T res = list.remove(index);
  164. listeners.getCallable(ListObserver.class).onItemsRemoved(this, index, index);
  165. return res;
  166. }
  167. @Override
  168. public int indexOf(final Object o) {
  169. return list.indexOf(o);
  170. }
  171. @Override
  172. public int lastIndexOf(final Object o) {
  173. return list.lastIndexOf(o);
  174. }
  175. @Nonnull
  176. @Override
  177. public ListIterator<T> listIterator() {
  178. return list.listIterator();
  179. }
  180. @Nonnull
  181. @Override
  182. public ListIterator<T> listIterator(final int index) {
  183. return list.listIterator(index);
  184. }
  185. @Nonnull
  186. @Override
  187. public List<T> subList(final int fromIndex, final int toIndex) {
  188. return list.subList(fromIndex, toIndex);
  189. }
  190. }