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 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.data.util;
  17. import java.util.ArrayList;
  18. import java.util.Collection;
  19. import java.util.Collections;
  20. import java.util.HashMap;
  21. import java.util.HashSet;
  22. import java.util.Iterator;
  23. /**
  24. * ListSet is an internal Vaadin class which implements a combination of a List
  25. * and a Set. The main purpose of this class is to provide a list with a fast
  26. * {@link #contains(Object)} method. Each inserted object must by unique (as
  27. * specified by {@link #equals(Object)}). The {@link #set(int, Object)} method
  28. * allows duplicates because of the way {@link Collections#sort(java.util.List)}
  29. * works.
  30. *
  31. * This class is subject to change and should not be used outside Vaadin core.
  32. */
  33. public class ListSet<E> extends ArrayList<E> {
  34. private HashSet<E> itemSet = null;
  35. /**
  36. * Contains a map from an element to the number of duplicates it has. Used
  37. * to temporarily allow duplicates in the list.
  38. */
  39. private HashMap<E, Integer> duplicates = new HashMap<E, Integer>();
  40. public ListSet() {
  41. super();
  42. itemSet = new HashSet<E>();
  43. }
  44. public ListSet(Collection<? extends E> c) {
  45. super(c);
  46. itemSet = new HashSet<E>(c.size());
  47. itemSet.addAll(c);
  48. }
  49. public ListSet(int initialCapacity) {
  50. super(initialCapacity);
  51. itemSet = new HashSet<E>(initialCapacity);
  52. }
  53. // Delegate contains operations to the set
  54. @Override
  55. public boolean contains(Object o) {
  56. return itemSet.contains(o);
  57. }
  58. @Override
  59. public boolean containsAll(Collection<?> c) {
  60. return itemSet.containsAll(c);
  61. }
  62. // Methods for updating the set when the list is updated.
  63. @Override
  64. public boolean add(E e) {
  65. if (contains(e)) {
  66. // Duplicates are not allowed
  67. return false;
  68. }
  69. if (super.add(e)) {
  70. itemSet.add(e);
  71. return true;
  72. } else {
  73. return false;
  74. }
  75. }
  76. /**
  77. * Works as java.util.ArrayList#add(int, java.lang.Object) but returns
  78. * immediately if the element is already in the ListSet.
  79. */
  80. @Override
  81. public void add(int index, E element) {
  82. if (contains(element)) {
  83. // Duplicates are not allowed
  84. return;
  85. }
  86. super.add(index, element);
  87. itemSet.add(element);
  88. }
  89. @Override
  90. public boolean addAll(Collection<? extends E> c) {
  91. boolean modified = false;
  92. Iterator<? extends E> i = c.iterator();
  93. while (i.hasNext()) {
  94. E e = i.next();
  95. if (contains(e)) {
  96. continue;
  97. }
  98. if (add(e)) {
  99. itemSet.add(e);
  100. modified = true;
  101. }
  102. }
  103. return modified;
  104. }
  105. @Override
  106. public boolean addAll(int index, Collection<? extends E> c) {
  107. ensureCapacity(size() + c.size());
  108. boolean modified = false;
  109. Iterator<? extends E> i = c.iterator();
  110. while (i.hasNext()) {
  111. E e = i.next();
  112. if (contains(e)) {
  113. continue;
  114. }
  115. add(index++, e);
  116. itemSet.add(e);
  117. modified = true;
  118. }
  119. return modified;
  120. }
  121. @Override
  122. public void clear() {
  123. super.clear();
  124. itemSet.clear();
  125. }
  126. @Override
  127. public int indexOf(Object o) {
  128. if (!contains(o)) {
  129. return -1;
  130. }
  131. return super.indexOf(o);
  132. }
  133. @Override
  134. public int lastIndexOf(Object o) {
  135. if (!contains(o)) {
  136. return -1;
  137. }
  138. return super.lastIndexOf(o);
  139. }
  140. @Override
  141. public E remove(int index) {
  142. E e = super.remove(index);
  143. if (e != null) {
  144. itemSet.remove(e);
  145. }
  146. return e;
  147. }
  148. @Override
  149. public boolean remove(Object o) {
  150. if (super.remove(o)) {
  151. itemSet.remove(o);
  152. return true;
  153. } else {
  154. return false;
  155. }
  156. }
  157. @Override
  158. protected void removeRange(int fromIndex, int toIndex) {
  159. HashSet<E> toRemove = new HashSet<E>();
  160. for (int idx = fromIndex; idx < toIndex; idx++) {
  161. toRemove.add(get(idx));
  162. }
  163. super.removeRange(fromIndex, toIndex);
  164. itemSet.removeAll(toRemove);
  165. }
  166. @Override
  167. public E set(int index, E element) {
  168. if (contains(element)) {
  169. // Element already exist in the list
  170. if (get(index) == element) {
  171. // At the same position, nothing to be done
  172. return element;
  173. } else {
  174. // Adding at another position. We assume this is a sort
  175. // operation and temporarily allow it.
  176. // We could just remove (null) the old element and keep the list
  177. // unique. This would require finding the index of the old
  178. // element (indexOf(element)) which is not a fast operation in a
  179. // list. So we instead allow duplicates temporarily.
  180. addDuplicate(element);
  181. }
  182. }
  183. E old = super.set(index, element);
  184. removeFromSet(old);
  185. itemSet.add(element);
  186. return old;
  187. }
  188. /**
  189. * Removes "e" from the set if it no longer exists in the list.
  190. *
  191. * @param e
  192. */
  193. private void removeFromSet(E e) {
  194. Integer dupl = duplicates.get(e);
  195. if (dupl != null) {
  196. // A duplicate was present so we only decrement the duplicate count
  197. // and continue
  198. if (dupl == 1) {
  199. // This is what always should happen. A sort sets the items one
  200. // by one, temporarily breaking the uniqueness requirement.
  201. duplicates.remove(e);
  202. } else {
  203. duplicates.put(e, dupl - 1);
  204. }
  205. } else {
  206. // The "old" value is no longer in the list.
  207. itemSet.remove(e);
  208. }
  209. }
  210. /**
  211. * Marks the "element" can be found more than once from the list. Allowed in
  212. * {@link #set(int, Object)} to make sorting work.
  213. *
  214. * @param element
  215. */
  216. private void addDuplicate(E element) {
  217. Integer nr = duplicates.get(element);
  218. if (nr == null) {
  219. nr = 1;
  220. } else {
  221. nr++;
  222. }
  223. /*
  224. * Store the number of duplicates of this element so we know later on if
  225. * we should remove an element from the set or if it was a duplicate (in
  226. * removeFromSet)
  227. */
  228. duplicates.put(element, nr);
  229. }
  230. @SuppressWarnings("unchecked")
  231. @Override
  232. public Object clone() {
  233. ListSet<E> v = (ListSet<E>) super.clone();
  234. v.itemSet = new HashSet<E>(itemSet);
  235. return v;
  236. }
  237. }