public final class ProtectedList extends AbstractList implements Cloneable
List
implementaton that can be protected from
unauthorized changes.
A secret key must be passed when constructing a
ProtectedList
instance. All modification methods on
this object then require this same secret key to be passed, otherwise they
fail with an IncorrectSecretKeyException
.
Note that the secret key equality is always checked before the other
preconditions. This means that if the secret key is incorrect, then the
other preconditions will not even be checked. For example, if
is called, then an
remove
(null, -1)IncorrectSecretKeyException
is thrown for the mismatching secret
key, and not an IndexOutOfBoundsException
, for the negative index.
modCount
Constructor and Description |
---|
ProtectedList(Object secretKey)
Constructs an empty
ProtectedList . |
ProtectedList(Object secretKey,
Collection c)
Constructs a new
ProtectedList containing the elements of
the specified collection, in the order they are returned by the
collection's iterator. |
ProtectedList(Object secretKey,
int initialCapacity)
Constructs an empty
ProtectedList with the specified
initial capacity. |
Modifier and Type | Method and Description |
---|---|
void |
add(Object secretKey,
Object element)
Adds the specified element to the list.
|
Object |
clone()
Clones this object.
|
Object |
get(int index)
Returns the element at the specified position in this list.
|
void |
remove(Object secretKey,
int index)
Removes the specified element.
|
void |
remove(Object secretKey,
Object element)
Removes the specified element.
|
int |
size()
Returns the number of elements in this list.
|
add, add, addAll, clear, equals, hashCode, indexOf, iterator, lastIndexOf, listIterator, listIterator, remove, removeRange, set, subList
addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray, toString
public ProtectedList(Object secretKey, int initialCapacity) throws IllegalArgumentException
ProtectedList
with the specified
initial capacity.secretKey
- the secret key that must be passed to the modification methods in
order to be authorized to modify this collection.initialCapacity
- the initial capacity, cannot be a negative number.IllegalArgumentException
- if secretKey == null || initialCapacity < 0
.public ProtectedList(Object secretKey) throws IllegalArgumentException
ProtectedList
.secretKey
- the secret key that must be passed to the modification methods in
order to be authorized to modify this collection, cannot be
null
.IllegalArgumentException
- if secretKey == null
.public ProtectedList(Object secretKey, Collection c) throws IllegalArgumentException
ProtectedList
containing the elements of
the specified collection, in the order they are returned by the
collection's iterator.secretKey
- the secret key that must be passed to the modification methods in
order to be authorized to modify this collection, cannot be
null
.c
- the collection whose elements are to be placed into this list, cannot
be null
.IllegalArgumentException
- if secretKey == null || c == null
.public Object get(int index) throws IndexOutOfBoundsException
get
in interface List
get
in class AbstractList
index
- the index of the element to return, must be >= 0 and <
size()
.null
.IndexOutOfBoundsException
- if index < 0
|| index >= size()
.public int size()
Integer.MAX_VALUE
elements, then Integer.MAX_VALUE
is returned.size
in interface Collection
size
in interface List
size
in class AbstractCollection
Integer.MAX_VALUE
.public void add(Object secretKey, Object element) throws IncorrectSecretKeyException
The correct secret key must be passed. If it is incorrect, then an
IncorrectSecretKeyException
is thrown. Note that an identity
check is done, not an equality check. So
the Object.equals(Object)
method is not used, but the
==
operator is.
secretKey
- the secret key, must be identity-equal to the secret key passed to
the constructor, cannot be null
.element
- the element to add to the list, can be null
.IncorrectSecretKeyException
- if secretKey
does not match the secret key passed to the
constructor.public void remove(Object secretKey, int index) throws IncorrectSecretKeyException, IndexOutOfBoundsException
The correct secret key must be passed. If it is incorrect, then an
IncorrectSecretKeyException
is thrown. Note that an identity
check is done, not an equality check. So
the Object.equals(Object)
method is not used, but the
==
operator is.
secretKey
- the secret key, must be identity-equal to the secret key passed to
the constructor, cannot be null
.index
- the position of the element to remove, must be >= 0 and <
size()
.IncorrectSecretKeyException
- if secretKey
does not match the secret key passed to the
constructor.IndexOutOfBoundsException
- if index < 0
|| index >= size()
.public void remove(Object secretKey, Object element) throws IncorrectSecretKeyException
The correct secret key must be passed. If it is incorrect, then an
IncorrectSecretKeyException
is thrown. Note that an identity
check is done, not an equality check. So
the Object.equals(Object)
method is not used, but the
==
operator is.
secretKey
- the secret key, must be identity-equal to the secret key passed to
the constructor, cannot be null
.element
- the element to remove.IncorrectSecretKeyException
- if secretKey
does not match the secret key passed to the
constructor.See http://www.xins.org/.