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

public Class PredicatedMap<K, V>

extends AbstractInputCheckedMapDecorator<K, V>
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Known Direct Subclasses
org.apache.commons.collections4.map.PredicatedSortedMap
Type Parameters
<K>
the type of the keys in this map
<V>
the type of the values in this map
Imports
java.io.IOException, .ObjectInputStream, .ObjectOutputStream, .Serializable, java.util.Iterator, .Map, org.apache.commons.collections4.Predicate

Decorates another Map to validate that additions match a specified predicate.

This map exists to provide validation for the decorated map. It is normally created to decorate an empty map. If an object cannot be added to the map, an IllegalArgumentException is thrown.

One usage would be to ensure that no null keys are added to the map.

Map map = PredicatedSet.decorate(new HashMap(), NotNullPredicate.INSTANCE, null);

Note that PredicatedMap is not synchronized and is not thread-safe. If you wish to use this map from multiple threads concurrently, you must use appropriate synchronization. The simplest approach is to wrap this map using java.util.Collections#synchronizedMap(Map). This class may throw exceptions when accessed by concurrent threads without synchronization.

This class is Serializable from Commons Collections 3.1.

Since
3.0

Field Summary

Modifier and TypeField and Description
protected final Predicate<? super K>
keyPredicate

The key predicate to use

private static final long
serialVersionUID

Serialization version

protected final Predicate<? super V>
valuePredicate

The value predicate to use

Constructor Summary

AccessConstructor and Description
protected
PredicatedMap(final Map<K, V>
the map to decorate, must not be null
map
,
final Predicate<? super K>
the predicate to validate the keys, null means no check
keyPredicate
,
final Predicate<? super V>
the predicate to validate to values, null means no check
valuePredicate
)

Constructor that wraps (not copies).

Method Summary

Modifier and TypeMethod and Description
protected V

Returns:

the value itself
checkSetValue
(final V
the value to validate
value
)

Implements abstract org.apache.commons.collections4.map.AbstractInputCheckedMapDecorator.checkSetValue.

Override to validate an object set into the map via setValue.
protected boolean

Returns:

true if a value predicate is in use
isSetValueChecking
()

Overrides org.apache.commons.collections4.map.AbstractInputCheckedMapDecorator.isSetValueChecking.

Override to only return true when there is a value transformer.
public static <
the key type
K
,
the value type
V
>
PredicatedMap<K, V>

Returns:

a new predicated map
predicatedMap
(final Map<K, V>
the map to decorate, must not be null
map
,
final Predicate<? super K>
the predicate to validate the keys, null means no check
keyPredicate
,
final Predicate<? super V>
the predicate to validate to values, null means no check
valuePredicate
)

Factory method to create a predicated (validating) map.

public V
put(final K
key with which the specified value is to be associated
key
,
final V
value to be associated with the specified key
value
)

Overrides org.apache.commons.collections4.map.AbstractMapDecorator.put.

Implements java.util.Map.put, org.apache.commons.collections4.Put.put.

Associates the specified value with the specified key in this map (optional operation).
public void
putAll(final Map<? extends K, ? extends V>
mappings to be stored in this map
mapToCopy
)

Overrides org.apache.commons.collections4.map.AbstractMapDecorator.putAll.

Implements java.util.Map.putAll, org.apache.commons.collections4.Put.putAll.

Copies all of the mappings from the specified map to this map (optional operation).
private void
readObject(final ObjectInputStream
the input stream
in
)

Read the map in using a custom routine.

protected void
validate(final K
the key to validate
key
,
final V
the value to validate
value
)

Validates a key value pair.

private void
writeObject(final ObjectOutputStream
the output stream
out
)

Write the map out using a custom routine.

Inherited from org.apache.commons.collections4.map.AbstractInputCheckedMapDecorator:
entrySet

Field Detail

keyPredicateback to summary
protected final Predicate<? super K> keyPredicate

The key predicate to use

serialVersionUIDback to summary
private static final long serialVersionUID

Serialization version

valuePredicateback to summary
protected final Predicate<? super V> valuePredicate

The value predicate to use

Constructor Detail

PredicatedMapback to summary
protected PredicatedMap(final Map<K, V> map, final Predicate<? super K> keyPredicate, final Predicate<? super V> valuePredicate)

Constructor that wraps (not copies).

Parameters
map:Map<K, V>

the map to decorate, must not be null

keyPredicate:Predicate<? super K>

the predicate to validate the keys, null means no check

valuePredicate:Predicate<? super V>

the predicate to validate to values, null means no check

Exceptions
NullPointerException:
if the map is null

Method Detail

checkSetValueback to summary
protected V checkSetValue(final V value)

Implements abstract org.apache.commons.collections4.map.AbstractInputCheckedMapDecorator.checkSetValue.

Override to validate an object set into the map via setValue.

Parameters
value:V

the value to validate

Returns:V

the value itself

Annotations
@Override
Exceptions
IllegalArgumentException:
if invalid
Since
3.1
isSetValueCheckingback to summary
protected boolean isSetValueChecking()

Overrides org.apache.commons.collections4.map.AbstractInputCheckedMapDecorator.isSetValueChecking.

Override to only return true when there is a value transformer.

Returns:boolean

true if a value predicate is in use

Annotations
@Override
Since
3.1
predicatedMapback to summary
public static <K, V> PredicatedMap<K, V> predicatedMap(final Map<K, V> map, final Predicate<? super K> keyPredicate, final Predicate<? super V> valuePredicate)

Factory method to create a predicated (validating) map.

If there are any elements already in the list being decorated, they are validated.

Parameters
<K>
the key type
<V>
the value type
map:Map<K, V>

the map to decorate, must not be null

keyPredicate:Predicate<? super K>

the predicate to validate the keys, null means no check

valuePredicate:Predicate<? super V>

the predicate to validate to values, null means no check

Returns:PredicatedMap<K, V>

a new predicated map

Exceptions
NullPointerException:
if the map is null
Since
4.0
putback to summary
public V put(final K key, final V value)

Overrides org.apache.commons.collections4.map.AbstractMapDecorator.put.

Implements java.util.Map.put, org.apache.commons.collections4.Put.put.

Doc from java.util.Map.put.

Associates the specified value with the specified key in this map (optional operation). If the map previously contained a mapping for the key, the old value is replaced by the specified value. (A map m is said to contain a mapping for a key k if and only if m.containsKey(k) would return true.)

Parameters
key:K

key with which the specified value is to be associated

value:V

value to be associated with the specified key

Returns:V

the previous value associated with key, or null if there was no mapping for key. (A null return can also indicate that the map previously associated null with key, if the implementation supports null values.)

Annotations
@Override
putAllback to summary
public void putAll(final Map<? extends K, ? extends V> mapToCopy)

Overrides org.apache.commons.collections4.map.AbstractMapDecorator.putAll.

Implements java.util.Map.putAll, org.apache.commons.collections4.Put.putAll.

Doc from java.util.Map.putAll.

Copies all of the mappings from the specified map to this map (optional operation). The effect of this call is equivalent to that of calling put(k, v) on this map once for each mapping from key k to value v in the specified map. The behavior of this operation is undefined if the specified map is modified while the operation is in progress. If the specified map has a defined encounter order, processing of its mappings generally occurs in that order.

Parameters
mapToCopy:Map<? extends K, ? extends V>

mappings to be stored in this map

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

Read the map 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
Since
3.1
validateback to summary
protected void validate(final K key, final V value)

Validates a key value pair.

Parameters
key:K

the key to validate

value:V

the value to validate

Exceptions
IllegalArgumentException:
if invalid
writeObjectback to summary
private void writeObject(final ObjectOutputStream out) throws IOException

Write the map out using a custom routine.

Parameters
out:ObjectOutputStream

the output stream

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