/** * Add these Sets to the list of sets in this composite * * @param sets the Sets to be appended to the composite */ public void addComposited(final Set<E>... sets) { for (final Set<E> set : sets) { addComposited(set); } }
/** * 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 */ @Override public boolean containsAll(final Collection<?> coll) { for (final Object item : coll) { if (contains(item) == false) { return false; } } return true; }
/** * 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 {@code Iterator.remove}, {@code Set.remove}, * {@code removeAll} {@code retainAll}, and {@code clear} operations. * It does not support the add or {@code addAll} 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. */ @Override public Set<K> keySet() { final CompositeSet<K> keys = new CompositeSet<>(); for (int i = this.composite.length - 1; i >= 0; --i) { keys.addComposited(this.composite[i].keySet()); } return keys; }
/** * Returns an array containing all of the elements in this composite. * * @return an object array of all the elements in the collection */ @Override public Object[] toArray() { final Object[] result = new Object[size()]; int i = 0; for (final Iterator<E> it = iterator(); it.hasNext(); i++) { result[i] = it.next(); } return result; }
/** * Add a Set to this composite. * * @param set the set to add * @throws IllegalArgumentException if a SetMutator is set, but fails to resolve a collision * @throws UnsupportedOperationException if there is no SetMutator set * @throws NullPointerException if {@code set} is null * @see SetMutator */ public synchronized void addComposited(final Set<E> set) { for (final Set<E> existingSet : getSets()) { final Collection<E> intersects = CollectionUtils.intersection(existingSet, set); if (intersects.size() > 0) { if (this.mutator == null) { throw new UnsupportedOperationException( "Collision adding composited set with no SetMutator set"); } getMutator().resolveCollision(this, existingSet, set, intersects); if (CollectionUtils.intersection(existingSet, set).size() > 0) { throw new IllegalArgumentException( "Attempt to add illegal entry unresolved by SetMutator.resolveCollision()"); } } } all.add(set); }
/** * {@inheritDoc} * @see java.util.Set#equals */ @Override public boolean equals(final Object obj) { if (obj instanceof Set) { final Set<?> set = (Set<?>) obj; return set.size() == this.size() && set.containsAll(this); } return false; }
/** * 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 */ @Override public boolean remove(final Object obj) { for (final Set<E> set : getSets()) { if (set.contains(obj)) { return set.remove(obj); } } return false; }
/** * 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 * {@code Iterator.remove}, {@code Set.remove}, {@code removeAll}, * {@code retainAll} and {@code clear} operations. It does not support * the {@code add} or {@code addAll} 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. */ @Override public Set<Map.Entry<K, V>> entrySet() { final CompositeSet<Map.Entry<K, V>> entries = new CompositeSet<>(); for (int i = composite.length - 1; i >= 0; --i) { entries.addComposited(composite[i].entrySet()); } return entries; }
/** * Returns an object array, populating the supplied array if possible. * See <code>Collection</code> interface for full details. * * @param <T> the type of the elements in the collection * @param array the array to use, populating if possible * @return an array of all the elements in the collection */ @Override @SuppressWarnings("unchecked") public <T> T[] toArray(final T[] array) { final int size = size(); Object[] result = null; if (array.length >= size) { result = array; } else { result = (Object[]) Array.newInstance(array.getClass().getComponentType(), size); } int offset = 0; for (final Collection<E> item : all) { for (final E e : item) { result[offset++] = e; } } if (result.length > size) { result[size] = null; } return (T[]) result; }
/** * Add these Sets to the list of sets in this composite. * * @param set1 the first Set to be appended to the composite * @param set2 the second Set to be appended to the composite */ public void addComposited(final Set<E> set1, final Set<E> set2) { addComposited(set1); addComposited(set2); }
/** * Create a composite set with sets as the initial set of composited Sets. * * @param sets the initial sets in the composite */ public CompositeSet(final Set<E>... sets) { super(); addComposited(sets); }
/** * Create a CompositeSet with just <code>set</code> composited. * * @param set the initial set in the composite */ public CompositeSet(final Set<E> set) { super(); addComposited(set); }