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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * Copyright (c) 2006-2014 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. /**
  28. * Decorates a {@link List} to add observable functionality.
  29. *
  30. * @param <T> The type of object the list contains
  31. */
  32. public class ObservableListDecorator<T> implements ObservableList<T> {
  33. /** The list being decorated. */
  34. private final List<T> list;
  35. /** The listeners for this list. */
  36. private final ListenerList listeners = new ListenerList();
  37. /**
  38. * Creates a new {@link ObservableListDecorator} which will decorate the
  39. * given list.
  40. *
  41. * @param list The list to be decorated
  42. */
  43. public ObservableListDecorator(final List<T> list) {
  44. this.list = list;
  45. }
  46. @Override
  47. public void addListListener(final ListObserver listener) {
  48. listeners.add(ListObserver.class, listener);
  49. }
  50. @Override
  51. public void removeListListener(final ListObserver listener) {
  52. listeners.remove(ListObserver.class, listener);
  53. }
  54. @Override
  55. public int size() {
  56. return list.size();
  57. }
  58. @Override
  59. public boolean isEmpty() {
  60. return list.isEmpty();
  61. }
  62. @Override
  63. public boolean contains(final Object o) {
  64. return list.contains(o);
  65. }
  66. @Override
  67. public Iterator<T> iterator() {
  68. return list.iterator();
  69. }
  70. @Override
  71. public Object[] toArray() {
  72. return list.toArray();
  73. }
  74. @Override
  75. public <S> S[] toArray(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(final Collection<?> c) {
  97. return list.containsAll(c);
  98. }
  99. @Override
  100. public boolean addAll(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, 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(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(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. @Override
  176. public ListIterator<T> listIterator() {
  177. return list.listIterator();
  178. }
  179. @Override
  180. public ListIterator<T> listIterator(final int index) {
  181. return list.listIterator(index);
  182. }
  183. @Override
  184. public List<T> subList(final int fromIndex, final int toIndex) {
  185. return list.subList(fromIndex, toIndex);
  186. }
  187. }