📄 compositecollection.java
字号:
* 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;
}
/**
* Adds a collection of elements to this collection, throwing
* UnsupportedOperationException unless a CollectionMutator strategy is
* specified.
*
* @param coll
* the collection to add
* @return true if the collection was modified
* @throws UnsupportedOperationException
* if CollectionMutator hasn't been set
* @throws UnsupportedOperationException
* if add is unsupported
* @throws ClassCastException
* if the object cannot be added due to its type
* @throws NullPointerException
* if the object cannot be added because its null
* @throws IllegalArgumentException
* if the object cannot be added
*/
public boolean addAll(Collection coll)
{
if ( this.mutator == null )
{
throw new UnsupportedOperationException( "addAll() is not supported on CompositeCollection without a CollectionMutator strategy" );
}
return this.mutator.addAll( this,
this.all,
coll );
}
/**
* Removes the elements in the specified collection from this composite
* collection.
* <p>
* This implementation calls <code>removeAll</code> on each collection.
*
* @param coll
* the collection to remove
* @return true if the collection was modified
* @throws UnsupportedOperationException
* if removeAll is unsupported
*/
public boolean removeAll(Collection coll)
{
if ( coll.isEmpty( ) )
{
return false;
}
boolean changed = false;
for ( int i = this.all.length - 1; i >= 0; i-- )
{
changed |= this.all[i].removeAll( coll );
}
return changed;
}
/**
* Retains all the elements in the specified collection in this composite
* collection, removing all others.
* <p>
* This implementation calls <code>retainAll()</code> on each collection.
*
* @param coll
* the collection to remove
* @return true if the collection was modified
* @throws UnsupportedOperationException
* if retainAll is unsupported
*/
public boolean retainAll(final Collection coll)
{
boolean changed = false;
for ( int i = this.all.length - 1; i >= 0; i-- )
{
changed = (this.all[i].retainAll( coll ) || changed);
}
return changed;
}
/**
* Removes all of the elements from this collection .
* <p>
* This implementation calls <code>clear()</code> on each collection.
*
* @throws UnsupportedOperationException
* if clear is unsupported
*/
public void clear()
{
for ( int i = 0; i < this.all.length; ++i )
{
this.all[i].clear( );
}
}
// -----------------------------------------------------------------------
/**
* Specify a CollectionMutator strategy instance to handle changes.
*
* @param mutator
* the mutator to use
*/
public void setMutator(CollectionMutator mutator)
{
this.mutator = mutator;
}
/**
* Add these Collections to the list of collections in this composite
*
* @param comps
* Collections to be appended to the composite
*/
public void addComposited(Collection[] comps)
{
ArrayList list = new ArrayList( Arrays.asList( this.all ) );
list.addAll( Arrays.asList( comps ) );
all = (Collection[]) list.toArray( new Collection[list.size( )] );
}
/**
* Add an additional collection to this composite.
*
* @param c
* the collection to add
*/
public void addComposited(Collection c)
{
this.addComposited( new Collection[]{c} );
}
/**
* Add two additional collections to this composite.
*
* @param c
* the first collection to add
* @param d
* the second collection to add
*/
public void addComposited(Collection c,
Collection d)
{
this.addComposited( new Collection[]{c, d} );
}
/**
* Removes a collection from the those being decorated in this composite.
*
* @param coll
* collection to be removed
*/
public void removeComposited(Collection coll)
{
ArrayList list = new ArrayList( this.all.length );
list.addAll( Arrays.asList( this.all ) );
list.remove( coll );
this.all = (Collection[]) list.toArray( new Collection[list.size( )] );
}
/**
* Returns a new collection containing all of the elements
*
* @return A new ArrayList containing all of the elements in this composite.
* The new collection is <i>not </i> backed by this composite.
*/
public Collection toCollection()
{
return new ArrayList( this );
}
/**
* Gets the collections being decorated.
*
* @return Unmodifiable collection of all collections in this composite.
*/
public Collection getCollections()
{
return Collections.unmodifiableList( Arrays.asList( this.all ) );
}
// -----------------------------------------------------------------------
/**
* Pluggable strategy to handle changes to the composite.
*/
public interface CollectionMutator
{
/**
* Called when an object is to be added to the composite.
*
* @param composite
* the CompositeCollection being changed
* @param collections
* all of the Collection instances in this
* CompositeCollection
* @param obj
* the object being added
* @return true if the collection is changed
* @throws UnsupportedOperationException
* if add is unsupported
* @throws ClassCastException
* if the object cannot be added due to its type
* @throws NullPointerException
* if the object cannot be added because its null
* @throws IllegalArgumentException
* if the object cannot be added
*/
public boolean add(CompositeCollection composite,
Collection[] collections,
Object obj);
/**
* Called when a collection is to be added to the composite.
*
* @param composite
* the CompositeCollection being changed
* @param collections
* all of the Collection instances in this
* CompositeCollection
* @param coll
* the collection being added
* @return true if the collection is changed
* @throws UnsupportedOperationException
* if add is unsupported
* @throws ClassCastException
* if the object cannot be added due to its type
* @throws NullPointerException
* if the object cannot be added because its null
* @throws IllegalArgumentException
* if the object cannot be added
*/
public boolean addAll(CompositeCollection composite,
Collection[] collections,
Collection coll);
/**
* Called when an object is to be removed to the composite.
*
* @param composite
* the CompositeCollection being changed
* @param collections
* all of the Collection instances in this
* CompositeCollection
* @param obj
* the object being removed
* @return true if the collection is changed
* @throws UnsupportedOperationException
* if removed is unsupported
* @throws ClassCastException
* if the object cannot be removed due to its type
* @throws NullPointerException
* if the object cannot be removed because its null
* @throws IllegalArgumentException
* if the object cannot be removed
*/
public boolean remove(CompositeCollection composite,
Collection[] collections,
Object obj);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -