Top Description Fields Constructors Methods
org.apache.commons.collections4.bag

public final Class UnmodifiableSortedBag<E>

extends AbstractSortedBagDecorator<E>
implements Unmodifiable
Class Inheritance
All Implemented Interfaces
org.apache.commons.collections4.Unmodifiable
Type Parameters
<E>
the type of elements in this bag
Imports
java.io.IOException, .ObjectInputStream, .ObjectOutputStream, java.util.Collection, .Iterator, .Set, java.util.function.Predicate, org.apache.commons.collections4.SortedBag, .Unmodifiable, org.apache.commons.collections4.iterators.UnmodifiableIterator, org.apache.commons.collections4.set.UnmodifiableSet

Decorates another SortedBag to ensure it can't be altered.

This class is Serializable from Commons Collections 3.1.

Attempts to modify it will result in an UnsupportedOperationException.

Since
3.0

Field Summary

Modifier and TypeField and Description
private static final long

Constructor Summary

AccessConstructor and Description
private
UnmodifiableSortedBag(final SortedBag<E>
the bag to decorate, must not be null
bag
)

Constructor that wraps (not copies).

Method Summary

Modifier and TypeMethod and Description
public boolean
add(final E
the object to add
object
)

Overrides org.apache.commons.collections4.collection.AbstractCollectionDecorator.add.

Implements org.apache.commons.collections4.Bag.add, java.util.Collection.add.

(Violation) Adds one copy of the specified object to the Bag.
public boolean
add(final E
the object to add
object
,
final int
the number of copies to add
count
)

Overrides org.apache.commons.collections4.bag.AbstractBagDecorator.add.

Implements org.apache.commons.collections4.Bag.add.

Adds nCopies copies of the specified object to the Bag.
public boolean
addAll(final Collection<? extends E>
collection containing elements to be added to this collection
coll
)

Overrides org.apache.commons.collections4.collection.AbstractCollectionDecorator.addAll.

Implements java.util.Collection.addAll.

Adds all of the elements in the specified collection to this collection (optional operation).
public void
clear()

Overrides org.apache.commons.collections4.collection.AbstractCollectionDecorator.clear.

Implements java.util.Collection.clear.

Removes all of the elements from this collection (optional operation).
public Iterator<E>
private void
readObject(final ObjectInputStream
the input stream
in
)

Read the collection in using a custom routine.

public boolean
remove(final Object
the object to remove
object
)

Overrides org.apache.commons.collections4.collection.AbstractCollectionDecorator.remove.

Implements org.apache.commons.collections4.Bag.remove, java.util.Collection.remove.

(Violation) Removes all occurrences of the given object from the bag.
public boolean
remove(final Object
the object to remove
object
,
final int
the number of copies to remove
count
)

Overrides org.apache.commons.collections4.bag.AbstractBagDecorator.remove.

Implements org.apache.commons.collections4.Bag.remove.

Removes nCopies copies of the specified object from the Bag.
public boolean
removeAll(final Collection<?>
the collection to remove
coll
)

Overrides org.apache.commons.collections4.collection.AbstractCollectionDecorator.removeAll.

Implements org.apache.commons.collections4.Bag.removeAll, java.util.Collection.removeAll.

(Violation) Remove all elements represented in the given collection, respecting cardinality.
public boolean
removeIf(Predicate<? super E>
a predicate which returns true for elements to be removed
filter
)

Overrides org.apache.commons.collections4.collection.AbstractCollectionDecorator.removeIf.

Overrides default java.util.Collection.removeIf.

Removes all of the elements of this collection that satisfy the given predicate.
public boolean
retainAll(final Collection<?>
the collection to retain
coll
)

Overrides org.apache.commons.collections4.collection.AbstractCollectionDecorator.retainAll.

Implements org.apache.commons.collections4.Bag.retainAll, java.util.Collection.retainAll.

(Violation) Remove any members of the bag that are not in the given collection, respecting cardinality.
public Set<E>
public static <
the type of the elements in the bag
E
>
SortedBag<E>

Returns:

an unmodifiable SortedBag
unmodifiableSortedBag
(final SortedBag<E>
the bag to decorate, must not be null
bag
)

Factory method to create an unmodifiable bag.

private void
writeObject(final ObjectOutputStream
the output stream
out
)

Write the collection out using a custom routine.

Inherited from org.apache.commons.collections4.bag.AbstractSortedBagDecorator:
comparatordecoratedfirstlast

Field Detail

serialVersionUIDback to summary
private static final long serialVersionUID

Hides org.apache.commons.collections4.bag.AbstractSortedBagDecorator.serialVersionUID.

Serialization version

Constructor Detail

UnmodifiableSortedBagback to summary
private UnmodifiableSortedBag(final SortedBag<E> bag)

Constructor that wraps (not copies).

Parameters
bag:SortedBag<E>

the bag to decorate, must not be null

Exceptions
NullPointerException:
if bag is null

Method Detail

addback to summary
public boolean add(final E object)

Overrides org.apache.commons.collections4.collection.AbstractCollectionDecorator.add.

Implements org.apache.commons.collections4.Bag.add, java.util.Collection.add.

Doc from org.apache.commons.collections4.Bag.add.

(Violation) Adds one copy of the specified object to the Bag.

If the object is already in the uniqueSet() then increment its count as reported by getCount(Object). Otherwise add it to the uniqueSet() and report its count as 1.

Since this method always increases the size of the bag, according to the Collection#add(Object) contract, it should always return true. Since it sometimes returns false, this method violates the contract.

Parameters
object:E

the object to add

Returns:boolean

true if the object was not already in the uniqueSet

Annotations
@Override
addback to summary
public boolean add(final E object, final int count)

Overrides org.apache.commons.collections4.bag.AbstractBagDecorator.add.

Implements org.apache.commons.collections4.Bag.add.

Doc from org.apache.commons.collections4.Bag.add.

Adds nCopies copies of the specified object to the Bag.

If the object is already in the uniqueSet() then increment its count as reported by getCount(Object). Otherwise add it to the uniqueSet() and report its count as nCopies.

Parameters
object:E

the object to add

count:int

the number of copies to add

Returns:boolean

true if the object was not already in the uniqueSet

Annotations
@Override
addAllback to summary
public boolean addAll(final Collection<? extends E> coll)

Overrides org.apache.commons.collections4.collection.AbstractCollectionDecorator.addAll.

Implements java.util.Collection.addAll.

Doc from java.util.Collection.addAll.

Adds all of the elements in the specified collection to this collection (optional operation). The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (This implies that the behavior of this call is undefined if the specified collection is this collection, and this collection is nonempty.) If the specified collection has a defined encounter order, processing of its elements generally occurs in that order.

Parameters
coll:Collection<? extends E>

collection containing elements to be added to this collection

Returns:boolean

true if this collection changed as a result of the call

Annotations
@Override
clearback to summary
public void clear()

Overrides org.apache.commons.collections4.collection.AbstractCollectionDecorator.clear.

Implements java.util.Collection.clear.

Doc from java.util.Collection.clear.

Removes all of the elements from this collection (optional operation). The collection will be empty after this method returns.

Annotations
@Override
iteratorback to summary
public Iterator<E> iterator()

Overrides org.apache.commons.collections4.collection.AbstractCollectionDecorator.iterator.

Implements org.apache.commons.collections4.Bag.iterator, java.util.Collection.iterator.

Doc from org.apache.commons.collections4.Bag.iterator.

Returns an Iterator over the entire set of members, including copies due to cardinality. This iterator is fail-fast and will not tolerate concurrent modifications.

Returns:Iterator<E>

iterator over all elements in the Bag

Annotations
@Override
readObjectback to summary
private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException

Read the collection in using a custom routine.

Parameters
in:ObjectInputStream

the input stream

Annotations
@SuppressWarnings:unchecked
Exceptions
IOException:
if an error occurs while reading from the stream
ClassNotFoundException:
if an object read from the stream can not be loaded
ClassCastException:
if deserialised object has wrong type
removeback to summary
public boolean remove(final Object object)

Overrides org.apache.commons.collections4.collection.AbstractCollectionDecorator.remove.

Implements org.apache.commons.collections4.Bag.remove, java.util.Collection.remove.

Doc from org.apache.commons.collections4.Bag.remove.

(Violation) Removes all occurrences of the given object from the bag.

This will also remove the object from the uniqueSet().

According to the Collection#remove(Object) method, this method should only remove the first occurrence of the given object, not all occurrences.

Parameters
object:Object

the object to remove

Returns:boolean

true if this call changed the collection

Annotations
@Override
removeback to summary
public boolean remove(final Object object, final int count)

Overrides org.apache.commons.collections4.bag.AbstractBagDecorator.remove.

Implements org.apache.commons.collections4.Bag.remove.

Doc from org.apache.commons.collections4.Bag.remove.

Removes nCopies copies of the specified object from the Bag.

If the number of copies to remove is greater than the actual number of copies in the Bag, no error is thrown.

Parameters
object:Object

the object to remove

count:int

the number of copies to remove

Returns:boolean

true if this call changed the collection

Annotations
@Override
removeAllback to summary
public boolean removeAll(final Collection<?> coll)

Overrides org.apache.commons.collections4.collection.AbstractCollectionDecorator.removeAll.

Implements org.apache.commons.collections4.Bag.removeAll, java.util.Collection.removeAll.

Doc from org.apache.commons.collections4.Bag.removeAll.

(Violation) Remove all elements represented in the given collection, respecting cardinality. That is, if the given collection coll contains n copies of a given object, the bag will have n fewer copies, assuming the bag had at least n copies to begin with.

The Collection#removeAll(Collection) method specifies that cardinality should not be respected; this method should remove all occurrences of every object contained in the given collection.

Parameters
coll:Collection<?>

the collection to remove

Returns:boolean

true if this call changed the collection

Annotations
@Override
removeIfback to summary
public boolean removeIf(Predicate<? super E> filter)

Overrides org.apache.commons.collections4.collection.AbstractCollectionDecorator.removeIf.

Overrides default java.util.Collection.removeIf.

Doc from java.util.Collection.removeIf.

Removes all of the elements of this collection that satisfy the given predicate. Errors or runtime exceptions thrown during iteration or by the predicate are relayed to the caller.

Parameters
filter:Predicate<? super E>

a predicate which returns true for elements to be removed

Returns:boolean

true if any elements were removed

Annotations
@Override
Since
4.4
retainAllback to summary
public boolean retainAll(final Collection<?> coll)

Overrides org.apache.commons.collections4.collection.AbstractCollectionDecorator.retainAll.

Implements org.apache.commons.collections4.Bag.retainAll, java.util.Collection.retainAll.

Doc from org.apache.commons.collections4.Bag.retainAll.

(Violation) Remove any members of the bag that are not in the given collection, respecting cardinality. That is, if the given collection coll contains n copies of a given object and the bag has m > n copies, then delete m - n copies from the bag. In addition, if e is an object in the bag but !coll.contains(e), then remove e and any of its copies.

The Collection#retainAll(Collection) method specifies that cardinality should not be respected; this method should keep all occurrences of every object contained in the given collection.

Parameters
coll:Collection<?>

the collection to retain

Returns:boolean

true if this call changed the collection

Annotations
@Override
uniqueSetback to summary
public Set<E> uniqueSet()

Overrides org.apache.commons.collections4.bag.AbstractBagDecorator.uniqueSet.

Implements org.apache.commons.collections4.Bag.uniqueSet.

Doc from org.apache.commons.collections4.Bag.uniqueSet.

Returns a Set of unique elements in the Bag.

Uniqueness constraints are the same as those in java.util.Set.

Returns:Set<E>

the Set of unique Bag elements

Annotations
@Override
unmodifiableSortedBagback to summary
public static <E> SortedBag<E> unmodifiableSortedBag(final SortedBag<E> bag)

Factory method to create an unmodifiable bag.

If the bag passed in is already unmodifiable, it is returned.

Parameters
<E>
the type of the elements in the bag
bag:SortedBag<E>

the bag to decorate, must not be null

Returns:SortedBag<E>

an unmodifiable SortedBag

Exceptions
NullPointerException:
if bag is null
Since
4.0
writeObjectback to summary
private void writeObject(final ObjectOutputStream out) throws IOException

Write the collection out using a custom routine.

Parameters
out:ObjectOutputStream

the output stream

Exceptions
IOException:
if an error occurs while writing to the stream