1. /*
  2. * Copyright 2003-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of 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,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.collections.set;
  17. import java.util.ArrayList;
  18. import java.util.Collection;
  19. import java.util.HashSet;
  20. import java.util.Iterator;
  21. import java.util.List;
  22. import java.util.Set;
  23. import org.apache.commons.collections.iterators.AbstractIteratorDecorator;
  24. import org.apache.commons.collections.list.UnmodifiableList;
  25. /**
  26. * Decorates another <code>Set</code> to ensure that the order of addition
  27. * is retained and used by the iterator.
  28. * <p>
  29. * If an object is added to the set for a second time, it will remain in the
  30. * original position in the iteration.
  31. * The order can be observed from the set via the iterator or toArray methods.
  32. * <p>
  33. * The ListOrderedSet also has various useful direct methods. These include many
  34. * from <code>List</code>, such as <code>get(int)</code>, <code>remove(int)</code>
  35. * and <code>indexOf(int)</code>. An unmodifiable <code>List</code> view of
  36. * the set can be obtained via <code>asList()</code>.
  37. * <p>
  38. * This class cannot implement the <code>List</code> interface directly as
  39. * various interface methods (notably equals/hashCode) are incompatable with a set.
  40. * <p>
  41. * This class is Serializable from Commons Collections 3.1.
  42. *
  43. * @since Commons Collections 3.0
  44. * @version $Revision: 1.9 $ $Date: 2004/06/07 21:42:12 $
  45. *
  46. * @author Stephen Colebourne
  47. * @author Henning P. Schmiedehausen
  48. */
  49. public class ListOrderedSet extends AbstractSerializableSetDecorator implements Set {
  50. /** Serialization version */
  51. private static final long serialVersionUID = -228664372470420141L;
  52. /** Internal list to hold the sequence of objects */
  53. protected final List setOrder;
  54. /**
  55. * Factory method to create an ordered set specifying the list and set to use.
  56. *
  57. * @param set the set to decorate, must be empty and not null
  58. * @param list the list to decorate, must be empty and not null
  59. * @throws IllegalArgumentException if set or list is null
  60. * @throws IllegalArgumentException if either the set or list is not empty
  61. * @since Commons Collections 3.1
  62. */
  63. public static ListOrderedSet decorate(Set set, List list) {
  64. if (set == null) {
  65. throw new IllegalArgumentException("Set must not be null");
  66. }
  67. if (list == null) {
  68. throw new IllegalArgumentException("List must not be null");
  69. }
  70. if (set.size() > 0 || list.size() > 0) {
  71. throw new IllegalArgumentException("Set and List must be empty");
  72. }
  73. return new ListOrderedSet(set, list);
  74. }
  75. /**
  76. * Factory method to create an ordered set.
  77. * <p>
  78. * An <code>ArrayList</code> is used to retain order.
  79. *
  80. * @param set the set to decorate, must not be null
  81. * @throws IllegalArgumentException if set is null
  82. */
  83. public static ListOrderedSet decorate(Set set) {
  84. return new ListOrderedSet(set);
  85. }
  86. /**
  87. * Factory method to create an ordered set using the supplied list to retain order.
  88. * <p>
  89. * A <code>HashSet</code> is used for the set behaviour.
  90. *
  91. * @param list the list to decorate, must not be null
  92. * @throws IllegalArgumentException if list is null
  93. */
  94. public static ListOrderedSet decorate(List list) {
  95. if (list == null) {
  96. throw new IllegalArgumentException("List must not be null");
  97. }
  98. Set set = new HashSet(list);
  99. list.retainAll(set);
  100. return new ListOrderedSet(set, list);
  101. }
  102. //-----------------------------------------------------------------------
  103. /**
  104. * Constructs a new empty <code>ListOrderedSet</code> using
  105. * a <code>HashSet</code> and an <code>ArrayList</code> internally.
  106. *
  107. * @since Commons Collections 3.1
  108. */
  109. public ListOrderedSet() {
  110. super(new HashSet());
  111. setOrder = new ArrayList();
  112. }
  113. /**
  114. * Constructor that wraps (not copies).
  115. *
  116. * @param set the set to decorate, must not be null
  117. * @throws IllegalArgumentException if set is null
  118. */
  119. protected ListOrderedSet(Set set) {
  120. super(set);
  121. setOrder = new ArrayList(set);
  122. }
  123. /**
  124. * Constructor that wraps (not copies) the Set and specifies the list to use.
  125. * <p>
  126. * The set and list must both be correctly initialised to the same elements.
  127. *
  128. * @param set the set to decorate, must not be null
  129. * @param list the list to decorate, must not be null
  130. * @throws IllegalArgumentException if set or list is null
  131. */
  132. protected ListOrderedSet(Set set, List list) {
  133. super(set);
  134. if (list == null) {
  135. throw new IllegalArgumentException("List must not be null");
  136. }
  137. setOrder = list;
  138. }
  139. //-----------------------------------------------------------------------
  140. /**
  141. * Gets an unmodifiable view of the order of the Set.
  142. *
  143. * @return an unmodifiable list view
  144. */
  145. public List asList() {
  146. return UnmodifiableList.decorate(setOrder);
  147. }
  148. //-----------------------------------------------------------------------
  149. public void clear() {
  150. collection.clear();
  151. setOrder.clear();
  152. }
  153. public Iterator iterator() {
  154. return new OrderedSetIterator(setOrder.iterator(), collection);
  155. }
  156. public boolean add(Object object) {
  157. if (collection.contains(object)) {
  158. // re-adding doesn't change order
  159. return collection.add(object);
  160. } else {
  161. // first add, so add to both set and list
  162. boolean result = collection.add(object);
  163. setOrder.add(object);
  164. return result;
  165. }
  166. }
  167. public boolean addAll(Collection coll) {
  168. boolean result = false;
  169. for (Iterator it = coll.iterator(); it.hasNext();) {
  170. Object object = it.next();
  171. result = result | add(object);
  172. }
  173. return result;
  174. }
  175. public boolean remove(Object object) {
  176. boolean result = collection.remove(object);
  177. setOrder.remove(object);
  178. return result;
  179. }
  180. public boolean removeAll(Collection coll) {
  181. boolean result = false;
  182. for (Iterator it = coll.iterator(); it.hasNext();) {
  183. Object object = it.next();
  184. result = result | remove(object);
  185. }
  186. return result;
  187. }
  188. public boolean retainAll(Collection coll) {
  189. boolean result = collection.retainAll(coll);
  190. if (result == false) {
  191. return false;
  192. } else if (collection.size() == 0) {
  193. setOrder.clear();
  194. } else {
  195. for (Iterator it = setOrder.iterator(); it.hasNext();) {
  196. Object object = it.next();
  197. if (collection.contains(object) == false) {
  198. it.remove();
  199. }
  200. }
  201. }
  202. return result;
  203. }
  204. public Object[] toArray() {
  205. return setOrder.toArray();
  206. }
  207. public Object[] toArray(Object a[]) {
  208. return setOrder.toArray(a);
  209. }
  210. //-----------------------------------------------------------------------
  211. public Object get(int index) {
  212. return setOrder.get(index);
  213. }
  214. public int indexOf(Object object) {
  215. return setOrder.indexOf(object);
  216. }
  217. public void add(int index, Object object) {
  218. if (contains(object) == false) {
  219. collection.add(object);
  220. setOrder.add(index, object);
  221. }
  222. }
  223. public boolean addAll(int index, Collection coll) {
  224. boolean changed = false;
  225. for (Iterator it = coll.iterator(); it.hasNext();) {
  226. Object object = it.next();
  227. if (contains(object) == false) {
  228. collection.add(object);
  229. setOrder.add(index, object);
  230. index++;
  231. changed = true;
  232. }
  233. }
  234. return changed;
  235. }
  236. public Object remove(int index) {
  237. Object obj = setOrder.remove(index);
  238. remove(obj);
  239. return obj;
  240. }
  241. /**
  242. * Uses the underlying List's toString so that order is achieved.
  243. * This means that the decorated Set's toString is not used, so
  244. * any custom toStrings will be ignored.
  245. */
  246. // Fortunately List.toString and Set.toString look the same
  247. public String toString() {
  248. return setOrder.toString();
  249. }
  250. //-----------------------------------------------------------------------
  251. /**
  252. * Internal iterator handle remove.
  253. */
  254. static class OrderedSetIterator extends AbstractIteratorDecorator {
  255. /** Object we iterate on */
  256. protected final Collection set;
  257. /** Last object retrieved */
  258. protected Object last;
  259. private OrderedSetIterator(Iterator iterator, Collection set) {
  260. super(iterator);
  261. this.set = set;
  262. }
  263. public Object next() {
  264. last = iterator.next();
  265. return last;
  266. }
  267. public void remove() {
  268. set.remove(last);
  269. iterator.remove();
  270. last = null;
  271. }
  272. }
  273. }