/** * Returns a collection view of the values contained in this map. The * collection is backed by the map, so changes to the map are reflected in * the collection, and vice-versa. If the map is modified while an * iteration over the collection is in progress, the results of the * iteration are undefined. The collection supports element removal, * which removes the corresponding mapping from the map, via the * <tt>Iterator.remove</tt>, <tt>Collection.remove</tt>, * <tt>removeAll</tt>, <tt>retainAll</tt> and <tt>clear</tt> operations. * It does not support the add or <tt>addAll</tt> operations. * * @return a collection view of the values contained in this map. */ public Collection values() { CompositeCollection keys = new CompositeCollection(); for (int i = this.composite.length - 1; i >= 0; --i) { keys.addComposited(this.composite[i].values()); } return keys; }
public void testRemoveComposited() { setUpMutatorTest(); one.add("1"); two.add("2"); two.add("1"); c.addComposited(one, two); c.removeComposited(one); assertTrue(c.contains("1")); assertEquals(c.size(), 2); } }
/** * Returns an array containing all of the elements in this composite. * * @return an object array of all the elements in the collection */ public Object[] toArray() { final Object[] result = new Object[this.size()]; int i = 0; for (Iterator it = this.iterator(); it.hasNext(); i++) { result[i] = it.next(); } return result; }
/** * Create a Composite Collection with only coll composited. * * @param coll a collection to decorate */ public CompositeCollection(Collection coll) { this(); this.addComposited(coll); }
public void testAddMutator() { setUpTest(); c.setMutator(new CompositeCollection.CollectionMutator() { public boolean add(CompositeCollection composite, Collection[] collections, Object obj) { c.addComposited(one); c.add("foo"); assertTrue(c.contains("foo")); assertTrue(one.contains("foo"));
public void testAddAllMutator() { setUpTest(); c.setMutator(new CompositeCollection.CollectionMutator() { public boolean add(CompositeCollection composite, Collection[] collections, Object obj) { c.addComposited(one); two.add("foo"); c.addAll(two); assertTrue(c.contains("foo")); assertTrue(one.contains("foo"));
public void testIterator() { setUpTest(); one.add("1"); two.add("2"); c.addComposited(one); c.addComposited(two); Iterator i = c.iterator(); Object next = i.next(); assertTrue(c.contains(next)); assertTrue(one.contains(next)); next = i.next(); i.remove(); assertTrue(!c.contains(next)); assertTrue(!two.contains(next)); }
public void testMultipleCollectionsSize() { setUpTest(); HashSet set = new HashSet(); set.add("a"); set.add("b"); c.addComposited(set); HashSet other = new HashSet(); other.add("c"); c.addComposited(other); assertEquals(set.size() + other.size(), c.size()); }
/** * Empty collection is empty composite */ public Collection makeCollection() { return new CompositeCollection(); }
public void testToCollection() { setUpTest(); one.add("1"); two.add("2"); c.addComposited(one, two); Collection foo = c.toCollection(); assertTrue(foo.containsAll(c)); assertEquals(c.size(), foo.size()); one.add("3"); assertTrue(!foo.containsAll(c)); }
public void testRetainAll() { setUpTest(); one.add("1"); one.add("2"); two.add("1"); c.addComposited(one); c.retainAll(two); assertTrue(!c.contains("2")); assertTrue(!one.contains("2")); assertTrue(c.contains("1")); assertTrue(one.contains("1")); }
public void testRemove() { setUpMutatorTest(); one.add("1"); two.add("2"); two.add("1"); c.addComposited(one, two); c.remove("1"); assertTrue(!c.contains("1")); assertTrue(!one.contains("1")); assertTrue(!two.contains("1")); }
public void testRemoveAll() { setUpMutatorTest(); one.add("1"); two.add("2"); two.add("1"); c.addComposited(one, two); c.removeAll(one); assertTrue(!c.contains("1")); assertTrue(!one.contains("1")); assertTrue(!two.contains("1")); }
/** * This can receive either a <code>CompositeCollection.CollectionMutator</code> * or a <code>CompositeSet.SetMutator</code>. If a * <code>CompositeCollection.CollectionMutator</code> is used than conflicts when adding * composited sets will throw IllegalArgumentException * <p> */ public void setMutator(CollectionMutator mutator) { super.setMutator(mutator); }
/** * Checks whether this composite contains all the elements in the specified collection. * <p> * This implementation calls <code>contains()</code> for each element in the * specified collection. * * @param coll the collection to check for * @return true if all elements contained */ public boolean containsAll(Collection coll) { for (Iterator it = coll.iterator(); it.hasNext();) { if (this.contains(it.next()) == false) { return false; } } return true; }
/** * Returns an object array, populating the supplied array if possible. * See <code>Collection</code> interface for full details. * * @param array the array to use, populating if possible * @return an array of all the elements in the collection */ public Object[] toArray(Object[] array) { int size = this.size(); Object[] result = null; if (array.length >= size) { result = array; } else { result = (Object[]) Array.newInstance(array.getClass().getComponentType(), size); } int offset = 0; for (int i = 0; i < this.all.length; ++i) { for (Iterator it = this.all[i].iterator(); it.hasNext();) { result[offset++] = it.next(); } } if (result.length > size) { result[size] = null; } return result; }
public void testClear() { setUpTest(); one.add("1"); two.add("2"); c.addComposited(one, two); c.clear(); assertTrue(one.isEmpty()); assertTrue(two.isEmpty()); assertTrue(c.isEmpty()); }
public void testIsEmpty() { setUpTest(); assertTrue(c.isEmpty()); HashSet empty = new HashSet(); c.addComposited(empty); assertTrue(c.isEmpty()); empty.add("a"); assertTrue(!c.isEmpty()); }
public void testContainsAll() { setUpTest(); one.add("1"); two.add("1"); c.addComposited(one); assertTrue(c.containsAll(two)); }
/** * Create a CompositeCollection with colls as the initial list of * composited collections. * * @param colls an array of collections to decorate */ public CompositeCollection(Collection[] colls) { this(); this.addComposited(colls); }