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.

ListSet.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.data.util;
  5. import java.util.ArrayList;
  6. import java.util.Collection;
  7. import java.util.HashSet;
  8. import java.util.Iterator;
  9. /**
  10. * ListSet is an internal Vaadin class which implements a combination of a List
  11. * and a Set. The main purpose of this class is to provide a fast
  12. * {@link #contains(Object)} method. Each inserted object must by unique (as
  13. * specified by {@link #equals(Object)}).
  14. *
  15. * This class is subject to change and should not be used outside Vaadin core.
  16. */
  17. public class ListSet<E> extends ArrayList<E> {
  18. private HashSet<E> itemSet = null;
  19. public ListSet() {
  20. super();
  21. itemSet = new HashSet<E>();
  22. }
  23. public ListSet(Collection<? extends E> c) {
  24. super(c);
  25. itemSet = new HashSet<E>(c.size());
  26. itemSet.addAll(c);
  27. }
  28. public ListSet(int initialCapacity) {
  29. super(initialCapacity);
  30. itemSet = new HashSet<E>(initialCapacity);
  31. }
  32. // Delegate contains operations to the set
  33. @Override
  34. public boolean contains(Object o) {
  35. return itemSet.contains(o);
  36. }
  37. @Override
  38. public boolean containsAll(Collection<?> c) {
  39. return itemSet.containsAll(c);
  40. }
  41. // Methods for updating the set when the list is updated.
  42. @Override
  43. public boolean add(E e) {
  44. if (contains(e)) {
  45. // Duplicates are not allowed
  46. return false;
  47. }
  48. if (super.add(e)) {
  49. itemSet.add(e);
  50. return true;
  51. } else {
  52. return false;
  53. }
  54. };
  55. /**
  56. * Works as java.util.ArrayList#add(int, java.lang.Object) but returns
  57. * immediately if the element is already in the ListSet.
  58. */
  59. @Override
  60. public void add(int index, E element) {
  61. if (contains(element)) {
  62. // Duplicates are not allowed
  63. return;
  64. }
  65. super.add(index, element);
  66. itemSet.add(element);
  67. }
  68. @Override
  69. public boolean addAll(Collection<? extends E> c) {
  70. boolean modified = false;
  71. Iterator<? extends E> i = c.iterator();
  72. while (i.hasNext()) {
  73. E e = i.next();
  74. if (contains(e)) {
  75. continue;
  76. }
  77. if (add(e)) {
  78. itemSet.add(e);
  79. modified = true;
  80. }
  81. }
  82. return modified;
  83. }
  84. @Override
  85. public boolean addAll(int index, Collection<? extends E> c) {
  86. ensureCapacity(size() + c.size());
  87. boolean modified = false;
  88. Iterator<? extends E> i = c.iterator();
  89. while (i.hasNext()) {
  90. E e = i.next();
  91. if (contains(e)) {
  92. continue;
  93. }
  94. add(index++, e);
  95. itemSet.add(e);
  96. modified = true;
  97. }
  98. return modified;
  99. }
  100. @Override
  101. public void clear() {
  102. super.clear();
  103. itemSet.clear();
  104. }
  105. @Override
  106. public int indexOf(Object o) {
  107. if (!contains(o)) {
  108. return -1;
  109. }
  110. return super.indexOf(o);
  111. }
  112. @Override
  113. public int lastIndexOf(Object o) {
  114. if (!contains(o)) {
  115. return -1;
  116. }
  117. return super.lastIndexOf(o);
  118. }
  119. @Override
  120. public E remove(int index) {
  121. E e = super.remove(index);
  122. if (e != null) {
  123. itemSet.remove(e);
  124. }
  125. return e;
  126. }
  127. @Override
  128. public boolean remove(Object o) {
  129. if (super.remove(o)) {
  130. itemSet.remove(o);
  131. return true;
  132. } else {
  133. return false;
  134. }
  135. }
  136. @Override
  137. protected void removeRange(int fromIndex, int toIndex) {
  138. HashSet<E> toRemove = new HashSet<E>();
  139. for (int idx = fromIndex; idx < toIndex; idx++) {
  140. toRemove.add(get(idx));
  141. }
  142. super.removeRange(fromIndex, toIndex);
  143. itemSet.removeAll(toRemove);
  144. }
  145. @Override
  146. public E set(int index, E element) {
  147. if (contains(element)) {
  148. // Element already exist in the list
  149. if (get(index) == element) {
  150. // At the same position, nothing to be done
  151. return element;
  152. } else {
  153. // At another position, cannot set
  154. return null;
  155. }
  156. }
  157. E old = super.set(index, element);
  158. itemSet.remove(old);
  159. itemSet.add(element);
  160. return old;
  161. }
  162. @SuppressWarnings("unchecked")
  163. @Override
  164. public Object clone() {
  165. ListSet<E> v = (ListSet<E>) super.clone();
  166. v.itemSet = new HashSet<E>(itemSet);
  167. return v;
  168. }
  169. }