/** * Returns a set view of the keys contained in this map. The set is * backed by the map, so changes to the map are reflected in the set, and * vice-versa. If the map is modified while an iteration over the set is * in progress, the results of the iteration are undefined. The set * supports element removal, which removes the corresponding mapping from * the map, via the <tt>Iterator.remove</tt>, <tt>Set.remove</tt>, * <tt>removeAll</tt> <tt>retainAll</tt>, and <tt>clear</tt> operations. * It does not support the add or <tt>addAll</tt> operations. * <p> * This implementation returns a <code>CompositeSet</code> which * composites the key sets from all of the composited maps. * * @return a set view of the keys contained in this map. */ public Set keySet() { CompositeSet keys = new CompositeSet(); for (int i = this.composite.length - 1; i >= 0; --i) { keys.addComposited(this.composite[i].keySet()); } return keys; }
/** * If a <code>CollectionMutator</code> is defined for this CompositeSet then this * method will be called anyway. * * @param obj Object to be removed * @return true if the object is removed, false otherwise */ public boolean remove(Object obj) { for (Iterator i = this.getCollections().iterator(); i.hasNext();) { Set set = (Set) i.next(); if (set.contains(obj)) return set.remove(obj); } return false; }
/** * @see Set#hashCode */ public int hashCode() { int code = 0; for (Iterator i = this.iterator(); i.hasNext();) { Object next = i.next(); code += (next != null ? next.hashCode() : 0); } return code; }
public Set makeEmptySet() { final HashSet contained = new HashSet(); CompositeSet set = new CompositeSet(contained); set.setMutator(new CompositeSet.SetMutator() { public void resolveCollision(CompositeSet comp, Set existing, Set added, Collection intersects) { throw new IllegalArgumentException(); } public boolean add(CompositeCollection composite, Collection[] collections, Object obj) { return contained.add(obj); } public boolean addAll(CompositeCollection composite, Collection[] collections, Collection coll) { return contained.addAll(coll); } public boolean remove(CompositeCollection composite, Collection[] collections, Object obj) { return contained.remove(obj); } }); return set; }
public void testRemoveUnderlying() { Set one = buildOne(); Set two = buildTwo(); CompositeSet set = new CompositeSet(new Set[]{one, two}); one.remove("1"); assertFalse(set.contains("1")); two.remove("3"); assertFalse(set.contains("3")); }
/** * Add an array of sets to this composite * @param comps * @throws IllegalArgumentException if any of the collections in comps do not implement Set */ public synchronized void addComposited(Collection[] comps) { for (int i = comps.length - 1; i >= 0; --i) { this.addComposited(comps[i]); } }
public void testAddComposited() { Set one = buildOne(); Set two = buildTwo(); CompositeSet set = new CompositeSet(); set.addComposited(one, two); CompositeSet set2 = new CompositeSet(buildOne()); set2.addComposited(buildTwo()); assertTrue(set.equals(set2)); HashSet set3 = new HashSet(); set3.add("1"); set3.add("2"); set3.add("3"); HashSet set4 = new HashSet(); set4.add("4"); CompositeSet set5 = new CompositeSet(set3); set5.addComposited(set4); assertTrue(set.equals(set5)); try { set.addComposited(set3); fail("Expecting UnsupportedOperationException."); } catch (UnsupportedOperationException ex) { // expected } } }
public void testFailedCollisionResolution() { Set one = buildOne(); Set two = buildTwo(); CompositeSet set = new CompositeSet(new Set[]{one, two}); set.setMutator(new CompositeSet.SetMutator() { public void resolveCollision(CompositeSet comp, Set existing, Set added, Collection intersects) { three.add("1"); try { set.addComposited(three); fail("IllegalArgumentException should have been thrown");
/** * @see Set#equals */ public boolean equals(Object obj) { if (obj instanceof Set) { Set set = (Set) obj; if (set.containsAll(this) && set.size() == this.size()) { return true; } } return false; }
public void testRemoveComposited() { Set one = buildOne(); Set two = buildTwo(); CompositeSet set = new CompositeSet(new Set[]{one, two}); set.remove("1"); assertFalse(one.contains("1")); set.remove("3"); assertFalse(one.contains("3")); }
/** * Add an array of sets to this composite * @param comps * @throws IllegalArgumentException if any of the collections in comps do not implement Set */ public synchronized void addComposited(Collection[] comps) { for (int i = comps.length - 1; i >= 0; --i) { this.addComposited(comps[i]); } }
/** * @see Set#equals */ public boolean equals(Object obj) { if (obj instanceof Set) { Set set = (Set) obj; if (set.containsAll(this) && set.size() == this.size()) { return true; } } return false; }
public void testContains() { CompositeSet set = new CompositeSet(new Set[]{buildOne(), buildTwo()}); assertTrue(set.contains("1")); }
/** * Returns a set view of the mappings contained in this map. Each element * in the returned set is a <code>Map.Entry</code>. The set is backed by the * map, so changes to the map are reflected in the set, and vice-versa. * If the map is modified while an iteration over the set is in progress, * the results of the iteration are undefined. The set supports element * removal, which removes the corresponding mapping from the map, via the * <tt>Iterator.remove</tt>, <tt>Set.remove</tt>, <tt>removeAll</tt>, * <tt>retainAll</tt> and <tt>clear</tt> operations. It does not support * the <tt>add</tt> or <tt>addAll</tt> operations. * <p> * This implementation returns a <code>CompositeSet</code> which * composites the entry sets from all of the composited maps. * * @see CompositeSet * @return a set view of the mappings contained in this map. */ public Set entrySet() { CompositeSet entries = new CompositeSet(); for (int i = this.composite.length - 1; i >= 0; --i) { entries.addComposited(this.composite[i].entrySet()); } return entries; }
/** * Add two sets to this composite * * @throws IllegalArgumentException if c or d does not implement java.util.Set */ public synchronized void addComposited(Collection c, Collection d) { if (!(c instanceof Set)) throw new IllegalArgumentException("Argument must implement java.util.Set"); if (!(d instanceof Set)) throw new IllegalArgumentException("Argument must implement java.util.Set"); this.addComposited(new Set[]{(Set) c, (Set) d}); }
/** * If a <code>CollectionMutator</code> is defined for this CompositeSet then this * method will be called anyway. * * @param obj Object to be removed * @return true if the object is removed, false otherwise */ public boolean remove(Object obj) { for (Iterator i = this.getCollections().iterator(); i.hasNext();) { Set set = (Set) i.next(); if (set.contains(obj)) return set.remove(obj); } return false; }
/** * @see Set#equals */ public boolean equals(Object obj) { if (obj instanceof Set) { Set set = (Set) obj; if (set.containsAll(this) && set.size() == this.size()) { return true; } } return false; }
/** * @see Set#hashCode */ public int hashCode() { int code = 0; for (Iterator i = this.iterator(); i.hasNext();) { Object next = i.next(); code += (next != null ? next.hashCode() : 0); } return code; }
/** * Returns a set view of the keys contained in this map. The set is * backed by the map, so changes to the map are reflected in the set, and * vice-versa. If the map is modified while an iteration over the set is * in progress, the results of the iteration are undefined. The set * supports element removal, which removes the corresponding mapping from * the map, via the <tt>Iterator.remove</tt>, <tt>Set.remove</tt>, * <tt>removeAll</tt> <tt>retainAll</tt>, and <tt>clear</tt> operations. * It does not support the add or <tt>addAll</tt> operations. * <p> * This implementation returns a <code>CompositeSet</code> which * composites the key sets from all of the composited maps. * * @return a set view of the keys contained in this map. */ public Set keySet() { CompositeSet keys = new CompositeSet(); for (int i = this.composite.length - 1; i >= 0; --i) { keys.addComposited(this.composite[i].keySet()); } return keys; }
/** * Add two sets to this composite * * @throws IllegalArgumentException if c or d does not implement java.util.Set */ public synchronized void addComposited(Collection c, Collection d) { if (!(c instanceof Set)) throw new IllegalArgumentException("Argument must implement java.util.Set"); if (!(d instanceof Set)) throw new IllegalArgumentException("Argument must implement java.util.Set"); this.addComposited(new Set[]{(Set) c, (Set) d}); }