PageRenderTime 29ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/Java.NET/JavApi Commons collections (Apache Port)/org.apache.commons.collections.collection.CompositeCollection.cs

https://github.com/gadfly/nofs
C# | 486 lines | 213 code | 36 blank | 237 comment | 30 complexity | 1ebcc5f3b344a0cd7ba4dd1c6e89916e MD5 | raw file
  1. /*
  2. * Licensed under the Apache License, Version 2.0 (the "License");
  3. * you may not use this file except in compliance with the License.
  4. * You may obtain a copy of the License at
  5. *
  6. * http://www.apache.org/licenses/LICENSE-2.0
  7. *
  8. * Unless required by applicable law or agreed to in writing, software
  9. * distributed under the License is distributed on an "AS IS" BASIS,
  10. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. * See the License for the specific language governing permissions and
  12. * limitations under the License.
  13. *
  14. */
  15. using System;
  16. using java = biz.ritter.javapi;
  17. using org.apache.commons.collections.iterators;
  18. using org.apache.commons.collections.list;
  19. namespace org.apache.commons.collections.collection
  20. {
  21. /**
  22. * Decorates a collection of other collections to provide a single unified view.
  23. * <p>
  24. * Changes made to this collection will actually be made on the decorated collection.
  25. * Add and remove operations require the use of a pluggable strategy. If no
  26. * strategy is provided then add and remove are unsupported.
  27. *
  28. * @since Commons Collections 3.0
  29. * @version $Revision$ $Date$
  30. *
  31. * @author Brian McCallister
  32. * @author Stephen Colebourne
  33. * @author Phil Steitz
  34. */
  35. public class CompositeCollection : java.util.Collection<Object>
  36. {
  37. /** CollectionMutator to handle changes to the collection */
  38. protected CollectionMutator mutator;
  39. /** Collections in the composite */
  40. protected java.util.Collection<Object>[] all;
  41. /**
  42. * Create an empty CompositeCollection.
  43. */
  44. public CompositeCollection()
  45. : base()
  46. {
  47. this.all = new java.util.Collection<Object>[0];
  48. }
  49. /**
  50. * Create a Composite Collection with only coll composited.
  51. *
  52. * @param coll a collection to decorate
  53. */
  54. public CompositeCollection(java.util.Collection<Object> coll)
  55. : this()
  56. {
  57. this.addComposited(coll);
  58. }
  59. /**
  60. * Create a CompositeCollection with colls as the initial list of
  61. * composited collections.
  62. *
  63. * @param colls an array of collections to decorate
  64. */
  65. public CompositeCollection(java.util.Collection<Object>[] colls)
  66. : this()
  67. {
  68. this.addComposited(colls);
  69. }
  70. //-----------------------------------------------------------------------
  71. /**
  72. * Gets the size of this composite collection.
  73. * <p>
  74. * This implementation calls <code>size()</code> on each collection.
  75. *
  76. * @return total number of elements in all contained containers
  77. */
  78. public virtual int size()
  79. {
  80. int size = 0;
  81. for (int i = this.all.Length - 1; i >= 0; i--)
  82. {
  83. size += this.all[i].size();
  84. }
  85. return size;
  86. }
  87. /**
  88. * Checks whether this composite collection is empty.
  89. * <p>
  90. * This implementation calls <code>isEmpty()</code> on each collection.
  91. *
  92. * @return true if all of the contained collections are empty
  93. */
  94. public virtual bool isEmpty()
  95. {
  96. for (int i = this.all.Length - 1; i >= 0; i--)
  97. {
  98. if (this.all[i].isEmpty() == false)
  99. {
  100. return false;
  101. }
  102. }
  103. return true;
  104. }
  105. /**
  106. * Checks whether this composite collection contains the object.
  107. * <p>
  108. * This implementation calls <code>contains()</code> on each collection.
  109. *
  110. * @param obj the object to search for
  111. * @return true if obj is contained in any of the contained collections
  112. */
  113. public virtual bool contains(Object obj)
  114. {
  115. for (int i = this.all.Length - 1; i >= 0; i--)
  116. {
  117. if (this.all[i].contains(obj))
  118. {
  119. return true;
  120. }
  121. }
  122. return false;
  123. }
  124. /**
  125. * Gets an iterator over all the collections in this composite.
  126. * <p>
  127. * This implementation uses an <code>IteratorChain</code>.
  128. *
  129. * @return an <code>IteratorChain</code> instance which supports
  130. * <code>remove()</code>. Iteration occurs over contained collections in
  131. * the order they were added, but this behavior should not be relied upon.
  132. * @see IteratorChain
  133. */
  134. public virtual java.util.Iterator<Object> iterator()
  135. {
  136. if (this.all.Length == 0)
  137. {
  138. return EmptyIterator.INSTANCE;
  139. }
  140. IteratorChain chain = new IteratorChain();
  141. for (int i = 0; i < this.all.Length; ++i)
  142. {
  143. chain.addIterator(this.all[i].iterator());
  144. }
  145. return chain;
  146. }
  147. /**
  148. * Returns an array containing all of the elements in this composite.
  149. *
  150. * @return an object array of all the elements in the collection
  151. */
  152. public virtual Object[] toArray()
  153. {
  154. Object[] result = new Object[this.size()];
  155. int i = 0;
  156. for (java.util.Iterator<Object> it = this.iterator(); it.hasNext(); i++)
  157. {
  158. result[i] = it.next();
  159. }
  160. return result;
  161. }
  162. /**
  163. * Returns an object array, populating the supplied array if possible.
  164. * See <code>Collection</code> interface for full details.
  165. *
  166. * @param array the array to use, populating if possible
  167. * @return an array of all the elements in the collection
  168. */
  169. public virtual Object[] toArray<Object>(Object[] array)
  170. {
  171. int size = this.size();
  172. Object[] result = null;
  173. if (array.Length >= size)
  174. {
  175. result = array;
  176. }
  177. else
  178. {
  179. result = new Object[size];// (Object[]) Array.newInstance(array.getClass().getComponentType(), size);
  180. }
  181. int offset = 0;
  182. for (int i = 0; i < this.all.Length; ++i)
  183. {
  184. for (java.util.Iterator<Object> it = ((java.util.Iterator<Object>)this.all[i].iterator()); it.hasNext(); )
  185. {
  186. result[offset++] = it.next();
  187. }
  188. }
  189. if (result.Length > size)
  190. {
  191. result[size] = default(Object);
  192. }
  193. return result;
  194. }
  195. /**
  196. * Adds an object to the collection, throwing UnsupportedOperationException
  197. * unless a CollectionMutator strategy is specified.
  198. *
  199. * @param obj the object to add
  200. * @return true if the collection was modified
  201. * @throws UnsupportedOperationException if CollectionMutator hasn't been set
  202. * @throws UnsupportedOperationException if add is unsupported
  203. * @throws ClassCastException if the object cannot be added due to its type
  204. * @throws NullPointerException if the object cannot be added because its null
  205. * @throws IllegalArgumentException if the object cannot be added
  206. */
  207. public virtual bool add(Object obj)
  208. {
  209. if (this.mutator == null)
  210. {
  211. throw new java.lang.UnsupportedOperationException(
  212. "add() is not supported on CompositeCollection without a CollectionMutator strategy");
  213. }
  214. return this.mutator.add(this, this.all, obj);
  215. }
  216. /**
  217. * Removes an object from the collection, throwing UnsupportedOperationException
  218. * unless a CollectionMutator strategy is specified.
  219. *
  220. * @param obj the object being removed
  221. * @return true if the collection is changed
  222. * @throws UnsupportedOperationException if removed is unsupported
  223. * @throws ClassCastException if the object cannot be removed due to its type
  224. * @throws NullPointerException if the object cannot be removed because its null
  225. * @throws IllegalArgumentException if the object cannot be removed
  226. */
  227. public virtual bool remove(Object obj)
  228. {
  229. if (this.mutator == null)
  230. {
  231. throw new java.lang.UnsupportedOperationException(
  232. "remove() is not supported on CompositeCollection without a CollectionMutator strategy");
  233. }
  234. return this.mutator.remove(this, this.all, obj);
  235. }
  236. /**
  237. * Checks whether this composite contains all the elements in the specified collection.
  238. * <p>
  239. * This implementation calls <code>contains()</code> for each element in the
  240. * specified collection.
  241. *
  242. * @param coll the collection to check for
  243. * @return true if all elements contained
  244. */
  245. public virtual bool containsAll(java.util.Collection<Object> coll)
  246. {
  247. for (java.util.Iterator<Object> it = coll.iterator(); it.hasNext(); )
  248. {
  249. if (this.contains(it.next()) == false)
  250. {
  251. return false;
  252. }
  253. }
  254. return true;
  255. }
  256. /**
  257. * Adds a collection of elements to this collection, throwing
  258. * UnsupportedOperationException unless a CollectionMutator strategy is specified.
  259. *
  260. * @param coll the collection to add
  261. * @return true if the collection was modified
  262. * @throws UnsupportedOperationException if CollectionMutator hasn't been set
  263. * @throws UnsupportedOperationException if add is unsupported
  264. * @throws ClassCastException if the object cannot be added due to its type
  265. * @throws NullPointerException if the object cannot be added because its null
  266. * @throws IllegalArgumentException if the object cannot be added
  267. */
  268. public virtual bool addAll(java.util.Collection<Object> coll)
  269. {
  270. if (this.mutator == null)
  271. {
  272. throw new java.lang.UnsupportedOperationException(
  273. "addAll() is not supported on CompositeCollection without a CollectionMutator strategy");
  274. }
  275. return this.mutator.addAll(this, this.all, coll);
  276. }
  277. /**
  278. * Removes the elements in the specified collection from this composite collection.
  279. * <p>
  280. * This implementation calls <code>removeAll</code> on each collection.
  281. *
  282. * @param coll the collection to remove
  283. * @return true if the collection was modified
  284. * @throws UnsupportedOperationException if removeAll is unsupported
  285. */
  286. public virtual bool removeAll(java.util.Collection<Object> coll)
  287. {
  288. if (coll.size() == 0)
  289. {
  290. return false;
  291. }
  292. bool changed = false;
  293. for (int i = this.all.Length - 1; i >= 0; i--)
  294. {
  295. changed = (this.all[i].removeAll(coll) || changed);
  296. }
  297. return changed;
  298. }
  299. /**
  300. * Retains all the elements in the specified collection in this composite collection,
  301. * removing all others.
  302. * <p>
  303. * This implementation calls <code>retainAll()</code> on each collection.
  304. *
  305. * @param coll the collection to remove
  306. * @return true if the collection was modified
  307. * @throws UnsupportedOperationException if retainAll is unsupported
  308. */
  309. public virtual bool retainAll(java.util.Collection<Object> coll)
  310. {
  311. bool changed = false;
  312. for (int i = this.all.Length - 1; i >= 0; i--)
  313. {
  314. changed = (this.all[i].retainAll(coll) || changed);
  315. }
  316. return changed;
  317. }
  318. /**
  319. * Removes all of the elements from this collection .
  320. * <p>
  321. * This implementation calls <code>clear()</code> on each collection.
  322. *
  323. * @throws UnsupportedOperationException if clear is unsupported
  324. */
  325. public virtual void clear()
  326. {
  327. for (int i = 0; i < this.all.Length; ++i)
  328. {
  329. this.all[i].clear();
  330. }
  331. }
  332. //-----------------------------------------------------------------------
  333. /**
  334. * Specify a CollectionMutator strategy instance to handle changes.
  335. *
  336. * @param mutator the mutator to use
  337. */
  338. public virtual void setMutator(CollectionMutator mutator)
  339. {
  340. this.mutator = mutator;
  341. }
  342. /**
  343. * Add these Collections to the list of collections in this composite
  344. *
  345. * @param comps Collections to be appended to the composite
  346. */
  347. public virtual void addComposited(java.util.Collection<Object>[] comps)
  348. {
  349. java.util.ArrayList<Object> list = new java.util.ArrayList<Object>(java.util.Arrays<Object>.asList<Object>(this.all));
  350. list.addAll(java.util.Arrays<Object>.asList<Object>(comps));
  351. all = (java.util.Collection<Object>[])list.toArray(new java.util.Collection<Object>[list.size()]);
  352. }
  353. /**
  354. * Add an additional collection to this composite.
  355. *
  356. * @param c the collection to add
  357. */
  358. public virtual void addComposited(java.util.Collection<Object> c)
  359. {
  360. this.addComposited(new java.util.Collection<Object>[] { c });
  361. }
  362. /**
  363. * Add two additional collections to this composite.
  364. *
  365. * @param c the first collection to add
  366. * @param d the second collection to add
  367. */
  368. public virtual void addComposited(java.util.Collection<Object> c, java.util.Collection<Object> d)
  369. {
  370. this.addComposited(new java.util.Collection<Object>[] { c, d });
  371. }
  372. /**
  373. * Removes a collection from the those being decorated in this composite.
  374. *
  375. * @param coll collection to be removed
  376. */
  377. public virtual void removeComposited(java.util.Collection<Object> coll)
  378. {
  379. java.util.ArrayList<Object> list = new java.util.ArrayList<Object>(this.all.Length);
  380. list.addAll(java.util.Arrays<Object>.asList<Object>(this.all));
  381. list.remove(coll);
  382. this.all = (java.util.Collection<Object>[])list.toArray(new java.util.Collection<Object>[list.size()]);
  383. }
  384. /**
  385. * Returns a new collection containing all of the elements
  386. *
  387. * @return A new ArrayList containing all of the elements in this composite.
  388. * The new collection is <i>not</i> backed by this composite.
  389. */
  390. public virtual java.util.Collection<Object> toCollection()
  391. {
  392. return new java.util.ArrayList<Object>(this);
  393. }
  394. /**
  395. * Gets the collections being decorated.
  396. *
  397. * @return Unmodifiable collection of all collections in this composite.
  398. */
  399. public virtual java.util.Collection<Object> getCollections()
  400. {
  401. return UnmodifiableList.decorate(java.util.Arrays<Object>.asList<Object>(this.all));
  402. }
  403. //-----------------------------------------------------------------------
  404. /**
  405. * Pluggable strategy to handle changes to the composite.
  406. */
  407. public interface CollectionMutator
  408. {
  409. /**
  410. * Called when an object is to be added to the composite.
  411. *
  412. * @param composite the CompositeCollection being changed
  413. * @param collections all of the Collection instances in this CompositeCollection
  414. * @param obj the object being added
  415. * @return true if the collection is changed
  416. * @throws UnsupportedOperationException if add is unsupported
  417. * @throws ClassCastException if the object cannot be added due to its type
  418. * @throws NullPointerException if the object cannot be added because its null
  419. * @throws IllegalArgumentException if the object cannot be added
  420. */
  421. bool add(CompositeCollection composite, java.util.Collection<Object>[] collections, Object obj);
  422. /**
  423. * Called when a collection is to be added to the composite.
  424. *
  425. * @param composite the CompositeCollection being changed
  426. * @param collections all of the Collection instances in this CompositeCollection
  427. * @param coll the collection being added
  428. * @return true if the collection is changed
  429. * @throws UnsupportedOperationException if add is unsupported
  430. * @throws ClassCastException if the object cannot be added due to its type
  431. * @throws NullPointerException if the object cannot be added because its null
  432. * @throws IllegalArgumentException if the object cannot be added
  433. */
  434. bool addAll(CompositeCollection composite, java.util.Collection<Object>[] collections, java.util.Collection<Object> coll);
  435. /**
  436. * Called when an object is to be removed to the composite.
  437. *
  438. * @param composite the CompositeCollection being changed
  439. * @param collections all of the Collection instances in this CompositeCollection
  440. * @param obj the object being removed
  441. * @return true if the collection is changed
  442. * @throws UnsupportedOperationException if removed is unsupported
  443. * @throws ClassCastException if the object cannot be removed due to its type
  444. * @throws NullPointerException if the object cannot be removed because its null
  445. * @throws IllegalArgumentException if the object cannot be removed
  446. */
  447. bool remove(CompositeCollection composite, java.util.Collection<Object>[] collections, Object obj);
  448. }
  449. }
  450. }