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

public Class LazySortedMap<K, V>

extends LazyMap<K, V>
implements SortedMap<K, V>
Class Inheritance
All Implemented Interfaces
java.util.SortedMap, java.util.SequencedMap, java.util.Map
Type Parameters
<K>
the type of the keys in this map
<V>
the type of the values in this map
Imports
java.util.Comparator, .SortedMap, org.apache.commons.collections4.Factory, .Transformer

Decorates another SortedMap to create objects in the map on demand.

When the get(Object) method is called with a key that does not exist in the map, the factory is used to create the object. The created object will be added to the map using the requested key.

For instance:

Factory<Date> factory = new Factory<Date>() {
    public Date create() {
        return new Date();
    }
}
SortedMap<String, Date> lazy =
    LazySortedMap.lazySortedMap(new HashMap<String, Date>(), factory);
Date date = lazy.get("NOW");

After the above code is executed, date will refer to a new Date instance. Furthermore, that Date instance is mapped to the "NOW" key in the map.

Note that LazySortedMap 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#synchronizedSortedMap. 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
private static final long
Inherited from org.apache.commons.collections4.map.LazyMap:
factory

Constructor Summary

AccessConstructor and Description
protected
LazySortedMap(final SortedMap<K, V>
the map to decorate, must not be null
map
,
final Factory<? extends V>
the factory to use, must not be null
factory
)

Constructor that wraps (not copies).

protected
LazySortedMap(final SortedMap<K, V>
the map to decorate, must not be null
map
,
final Transformer<? super K, ? extends V>
the factory to use, must not be null
factory
)

Constructor that wraps (not copies).

Method Summary

Modifier and TypeMethod and Description
public Comparator<? super K>
comparator()

Implements java.util.SortedMap.comparator.

Returns the comparator used to order the keys in this map, or null if this map uses the natural ordering of its keys.
public K
firstKey()

Implements java.util.SortedMap.firstKey.

Returns the first (lowest) key currently in this map.
protected SortedMap<K, V>

Returns:

the decorated map
getSortedMap
()

Gets the map being decorated.

public SortedMap<K, V>
headMap(final K
high endpoint (exclusive) of the keys in the returned map
toKey
)

Implements java.util.SortedMap.headMap.

Returns a view of the portion of this map whose keys are strictly less than toKey.
public K
lastKey()

Implements java.util.SortedMap.lastKey.

Returns the last (highest) key currently in this map.
public static <
the key type
K
,
the value type
V
>
LazySortedMap<K, V>

Returns:

a new lazy sorted map
lazySortedMap
(final SortedMap<K, V>
the map to decorate, must not be null
map
,
final Factory<? extends V>
the factory to use, must not be null
factory
)

Factory method to create a lazily instantiated sorted map.

public static <
the key type
K
,
the value type
V
>
LazySortedMap<K, V>

Returns:

a new lazy sorted map
lazySortedMap
(final SortedMap<K, V>
the map to decorate, must not be null
map
,
final Transformer<? super K, ? extends V>
the factory to use, must not be null
factory
)

Factory method to create a lazily instantiated sorted map.

public SortedMap<K, V>
subMap(final K
low endpoint (inclusive) of the keys in the returned map
fromKey
,
final K
high endpoint (exclusive) of the keys in the returned map
toKey
)

Implements java.util.SortedMap.subMap.

Returns a view of the portion of this map whose keys range from fromKey, inclusive, to toKey, exclusive.
public SortedMap<K, V>
tailMap(final K
low endpoint (inclusive) of the keys in the returned map
fromKey
)

Implements java.util.SortedMap.tailMap.

Returns a view of the portion of this map whose keys are greater than or equal to fromKey.
Inherited from org.apache.commons.collections4.map.LazyMap:
getlazyMaplazyMap

Field Detail

serialVersionUIDback to summary
private static final long serialVersionUID

Hides org.apache.commons.collections4.map.LazyMap.serialVersionUID.

Serialization version

Constructor Detail

LazySortedMapback to summary
protected LazySortedMap(final SortedMap<K, V> map, final Factory<? extends V> factory)

Constructor that wraps (not copies).

Parameters
map:SortedMap<K, V>

the map to decorate, must not be null

factory:Factory<? extends V>

the factory to use, must not be null

Exceptions
NullPointerException:
if map or factory is null
LazySortedMapback to summary
protected LazySortedMap(final SortedMap<K, V> map, final Transformer<? super K, ? extends V> factory)

Constructor that wraps (not copies).

Parameters
map:SortedMap<K, V>

the map to decorate, must not be null

factory:Transformer<? super K, ? extends V>

the factory to use, must not be null

Exceptions
NullPointerException:
if map or factory is null

Method Detail

comparatorback to summary
public Comparator<? super K> comparator()

Implements java.util.SortedMap.comparator.

Doc from java.util.SortedMap.comparator.

Returns the comparator used to order the keys in this map, or null if this map uses the natural ordering of its keys.

Returns:Comparator<? super K>

the comparator used to order the keys in this map, or null if this map uses the natural ordering of its keys

Annotations
@Override
firstKeyback to summary
public K firstKey()

Implements java.util.SortedMap.firstKey.

Doc from java.util.SortedMap.firstKey.

Returns the first (lowest) key currently in this map.

Returns:K

the first (lowest) key currently in this map

Annotations
@Override
getSortedMapback to summary
protected SortedMap<K, V> getSortedMap()

Gets the map being decorated.

Returns:SortedMap<K, V>

the decorated map

headMapback to summary
public SortedMap<K, V> headMap(final K toKey)

Implements java.util.SortedMap.headMap.

Doc from java.util.SortedMap.headMap.

Returns a view of the portion of this map whose keys are strictly less than toKey. The returned map is backed by this map, so changes in the returned map are reflected in this map, and vice-versa. The returned map supports all optional map operations that this map supports.

The returned map will throw an IllegalArgumentException on an attempt to insert a key outside its range.

Parameters
toKey:K

high endpoint (exclusive) of the keys in the returned map

Returns:SortedMap<K, V>

a view of the portion of this map whose keys are strictly less than toKey

Annotations
@Override
lastKeyback to summary
public K lastKey()

Implements java.util.SortedMap.lastKey.

Doc from java.util.SortedMap.lastKey.

Returns the last (highest) key currently in this map.

Returns:K

the last (highest) key currently in this map

Annotations
@Override
lazySortedMapback to summary
public static <K, V> LazySortedMap<K, V> lazySortedMap(final SortedMap<K, V> map, final Factory<? extends V> factory)

Factory method to create a lazily instantiated sorted map.

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

the map to decorate, must not be null

factory:Factory<? extends V>

the factory to use, must not be null

Returns:LazySortedMap<K, V>

a new lazy sorted map

Exceptions
NullPointerException:
if map or factory is null
Since
4.0
lazySortedMapback to summary
public static <K, V> LazySortedMap<K, V> lazySortedMap(final SortedMap<K, V> map, final Transformer<? super K, ? extends V> factory)

Factory method to create a lazily instantiated sorted map.

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

the map to decorate, must not be null

factory:Transformer<? super K, ? extends V>

the factory to use, must not be null

Returns:LazySortedMap<K, V>

a new lazy sorted map

Exceptions
NullPointerException:
if map or factory is null
Since
4.0
subMapback to summary
public SortedMap<K, V> subMap(final K fromKey, final K toKey)

Implements java.util.SortedMap.subMap.

Doc from java.util.SortedMap.subMap.

Returns a view of the portion of this map whose keys range from fromKey, inclusive, to toKey, exclusive. (If fromKey and toKey are equal, the returned map is empty.) The returned map is backed by this map, so changes in the returned map are reflected in this map, and vice-versa. The returned map supports all optional map operations that this map supports.

The returned map will throw an IllegalArgumentException on an attempt to insert a key outside its range.

Parameters
fromKey:K

low endpoint (inclusive) of the keys in the returned map

toKey:K

high endpoint (exclusive) of the keys in the returned map

Returns:SortedMap<K, V>

a view of the portion of this map whose keys range from fromKey, inclusive, to toKey, exclusive

Annotations
@Override
tailMapback to summary
public SortedMap<K, V> tailMap(final K fromKey)

Implements java.util.SortedMap.tailMap.

Doc from java.util.SortedMap.tailMap.

Returns a view of the portion of this map whose keys are greater than or equal to fromKey. The returned map is backed by this map, so changes in the returned map are reflected in this map, and vice-versa. The returned map supports all optional map operations that this map supports.

The returned map will throw an IllegalArgumentException on an attempt to insert a key outside its range.

Parameters
fromKey:K

low endpoint (inclusive) of the keys in the returned map

Returns:SortedMap<K, V>

a view of the portion of this map whose keys are greater than or equal to fromKey

Annotations
@Override