PCJ API
Version 1.2

bak.pcj.adapter
Class MapToLongKeyShortMapAdapter

java.lang.Object
  extended bybak.pcj.map.AbstractLongKeyShortMap
      extended bybak.pcj.adapter.MapToLongKeyShortMapAdapter
All Implemented Interfaces:
LongKeyShortMap

public class MapToLongKeyShortMapAdapter
extends AbstractLongKeyShortMap
implements LongKeyShortMap

This class represents adaptions of Java Collections Framework maps to primitive maps from long values to short values. The adapter is implemented as a wrapper around the map. Thus, changes to the underlying map are reflected by this map and vice versa.

Adapters from JCF maps to primitive map will fail if the JCF collection contains null keys/values or keys/values of the wrong class. However, adapters are not fast failing in the case that the underlying map should contain illegal keys or values. To implement fast failure would require every operation to check every key and value of the underlying map before doing anything. Instead validation methods are provided. They can be called using the assertion facility in the client code:

      MapToLongKeyShortMapAdapter s;
      ...
      assert s.validate();
  
or by letting the adapter throw an exception on illegal values:
      MapToLongKeyShortMapAdapter s;
      ...
      s.evalidate();  // Throws an exception on illegal values
  
Either way, validation must be invoked directly by the client code.

Since:
1.0

Field Summary
protected  Short lastValue
          The value corresponding to the last key found by containsKey().
protected  Map map
          The underlying map.
 
Constructor Summary
MapToLongKeyShortMapAdapter(Map map)
          Creates a new adaption to a map from long values to short values.
MapToLongKeyShortMapAdapter(Map map, boolean validate)
          Creates a new adaption to a map from long values to short values.
 
Method Summary
 void clear()
          Clears this map.
 boolean containsKey(long key)
          Indicates whether this map contains a mapping from a specified key.
 boolean containsValue(short value)
          Indicates whether this map contains a mapping to a specified value.
 LongKeyShortMapIterator entries()
          Returns an iterator over the entries of this map.
 void evalidate()
          Validates the map underlying this adapter and throws an exception if it is invalid.
 short get(long key)
          Maps a specified key to a value.
 LongSet keySet()
          Returns a set view of the keys of this map.
 short lget()
          Returns the last value corresponding to a positive result from containsKey(long).
 short put(long key, short value)
          Adds a mapping from a specified key to a specified value to this map.
 short remove(long key)
          Removes the mapping from a specified key from this map.
 int size()
          Returns the size of this map.
 short tget(long key)
          Maps a specified key to a value.
 boolean validate()
          Indicates whether the underlying map is valid for this adapter.
 ShortCollection values()
          Returns a collection view of the values in this map.
 
Methods inherited from class bak.pcj.map.AbstractLongKeyShortMap
equals, hashCode, isEmpty, putAll, toString, trimToSize
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface bak.pcj.map.LongKeyShortMap
equals, hashCode, isEmpty, putAll, trimToSize
 

Field Detail

map

protected Map map
The underlying map.


lastValue

protected Short lastValue
The value corresponding to the last key found by containsKey().

Constructor Detail

MapToLongKeyShortMapAdapter

public MapToLongKeyShortMapAdapter(Map map)
Creates a new adaption to a map from long values to short values.

Parameters:
map - the underlying map. This map must consist of keys of class Long. values of class Short. Otherwise a ClassCastException will be thrown by some methods.
Throws:
NullPointerException - if map is null.

MapToLongKeyShortMapAdapter

public MapToLongKeyShortMapAdapter(Map map,
                                   boolean validate)
Creates a new adaption to a map from long values to short values. The map to adapt is optionally validated.

Parameters:
map - the underlying map. This map must consist of keys of class Long. values of class Short. Otherwise a ClassCastException will be thrown by some methods.
validate - indicates whether map should be checked for illegal values.
Throws:
NullPointerException - if map is null.
IllegalStateException - if validate is true and map contains a null key/value, a key that is not of class Long, or a value that is not of class Short.
Method Detail

clear

public void clear()
Description copied from interface: LongKeyShortMap
Clears this map.

Specified by:
clear in interface LongKeyShortMap
Overrides:
clear in class AbstractLongKeyShortMap

containsKey

public boolean containsKey(long key)
Description copied from interface: LongKeyShortMap
Indicates whether this map contains a mapping from a specified key. If the key is contained in this map, a succeeding call to lget() will return the corresponding value.

Specified by:
containsKey in interface LongKeyShortMap
Overrides:
containsKey in class AbstractLongKeyShortMap

containsValue

public boolean containsValue(short value)
Description copied from interface: LongKeyShortMap
Indicates whether this map contains a mapping to a specified value.

Specified by:
containsValue in interface LongKeyShortMap
Overrides:
containsValue in class AbstractLongKeyShortMap

entries

public LongKeyShortMapIterator entries()
Description copied from interface: LongKeyShortMap
Returns an iterator over the entries of this map. It is possible to remove entries from this map using the iterator provided that the concrete map supports removal of entries.

Specified by:
entries in interface LongKeyShortMap
Returns:
an iterator over the entries of this map.

get

public short get(long key)
Description copied from interface: LongKeyShortMap
Maps a specified key to a value. Returns a default value as specified by the MapDefaults class if no mapping exists for the specified key.

Specified by:
get in interface LongKeyShortMap
Overrides:
get in class AbstractLongKeyShortMap

keySet

public LongSet keySet()
Description copied from interface: LongKeyShortMap
Returns a set view of the keys of this map. Removals from the returned set removes the corresponding entries in this map. Changes to the map are reflected in the set.

Specified by:
keySet in interface LongKeyShortMap
Returns:
a set view of the keys of this map.

lget

public short lget()
Description copied from interface: LongKeyShortMap
Returns the last value corresponding to a positive result from containsKey(long). This is useful for checking checking the existence of a mapping while avoiding two lookups on the same key.

Specified by:
lget in interface LongKeyShortMap
Returns:
the value corresponding to the key from the last invokation of containsKey(long).
See Also:
LongKeyShortMap.get(long), LongKeyShortMap.tget(long), LongKeyShortMap.containsKey(long)

put

public short put(long key,
                 short value)
Description copied from interface: LongKeyShortMap
Adds a mapping from a specified key to a specified value to this map. If a mapping already exists for the specified key it is overwritten by the new mapping.

Specified by:
put in interface LongKeyShortMap
Parameters:
key - the key of the mapping to add to this map.
value - the value of the mapping to add to this map.
Returns:
the old value if a mapping from the specified key already existed in this map; otherwise returns a default value as specified by the MapDefaults class.
See Also:
MapDefaults

remove

public short remove(long key)
Description copied from interface: LongKeyShortMap
Removes the mapping from a specified key from this map.

Specified by:
remove in interface LongKeyShortMap
Overrides:
remove in class AbstractLongKeyShortMap

size

public int size()
Description copied from interface: LongKeyShortMap
Returns the size of this map. The size is defined as the number of mappings from keys to values.

Specified by:
size in interface LongKeyShortMap
Overrides:
size in class AbstractLongKeyShortMap

values

public ShortCollection values()
Description copied from interface: LongKeyShortMap
Returns a collection view of the values in this map. The collection is not modifiable, but changes to the map are reflected in the collection.

Specified by:
values in interface LongKeyShortMap
Returns:
a collection view of the values in this map.

tget

public short tget(long key)
Description copied from interface: LongKeyShortMap
Maps a specified key to a value. This method should be used when the key is known to be in the map.

Specified by:
tget in interface LongKeyShortMap
Overrides:
tget in class AbstractLongKeyShortMap

validate

public boolean validate()
Indicates whether the underlying map is valid for this adapter. For the underlying map to be valid, it can only contain Long keys, no null keys/values, and only Short values.

Returns:
true if the underlying map is valid; returns false otherwise.

evalidate

public void evalidate()
Validates the map underlying this adapter and throws an exception if it is invalid. For the underlying map to be valid, it can only contain Long keys, no null keys/values, and only Short values.

Throws:
IllegalStateException - if the underlying map is invalid.

PCJ API
Version 1.2

Report a bug or request a feature.
Further information on the development and latest release of PCJ can be found at the project homepage.

Primitive Collections for Java is released under the GNU Lesser General Public License.
Copyright © 2002, 2003 Søren Bak. All Rights Reserved.

Hosted by SourceForge.net
SourceForge.net Logo