PrimitiveTypeMap

Inherits from:AbstractMap < BasicClass
Implements:
  • Author: Simon Wacker
  • Classpath: org.as2lib.data.holder.map.PrimitiveTypeMap
  • File last modified: Tuesday, 10 May 2005, 16:57:30
PrimitiveTypeMap can be used to map any primitive type key to any type of value.

Primitive data types are strings, numbers and booleans. When you use this map you normally use it in conjunction with string keys, because you can use arrays for numbers and for booleans you normally do not need a data holder at all.

Note that if you only want to map values to keys you can also do this using a dynamic or untyped object. But if you also want to be able to iterate over your keys and values in the correct order or if you want this iteration to be fast - the for..in loop is quite slow - you should use this map.

This class offers ordered mapping functionality. That means that the methods getKeys and getValues return the keys and values in the order they were put to the map and that the iterators returned by the methods valueIterator and keyIterator also iterate over the keys and values in the correct order.

This map offers two methods that help you find out whether it contains a specific key or value. These two methods are containsKey and containsValue.

To get the data stored in this map you can use the getKeys, getValues and get methods. If you want to iterate over the values of this map you can use the iterators returned by the methods iterator or valueIterator. If you want to iterate over the keys you can use the iterator returned by the keyIterator method.

To add key-value pairs to this map you can use the methods put and putAll. The putAll method lets you add all key-value pairs contained in the passed-in map to this map.

To remove key-value pairs you can use the methods remove and clear. The remove method deletes only the key-value pair corresponding to the passed-in key, while the clear method removes all key-value pairs.

There are two more methods you may need. The isEmpty and the size method. These methods give you information about whether this map contains any mappings and how many mappings it contains.

To change the string representation returned by the toString method you can set your own stringifier using the static AbstractMap.setStringifier method.

Example:

// constructs the map
var map:Map = new PrimitiveTypeMap();
map.put("key1", "value1");
map.put("key2", "value2");
map.put("key3", "value3");
// uses the map
trace(map.get("key1"));
trace(map.get("key2"));
trace(map.get("key3"));

Output:

value1
value2
value3

Summary

Constructor
Class methods
Class methods inherited from AbstractMap

Constructor

PrimitiveTypeMap

function PrimitiveTypeMap (
source)
Constructs a new PrimitiveTypeMap instance.

This map iterates over the passed-in source with the for..in loop and uses the variables' names as key and their values as value. Variables that are hidden from for..in loops will not be added to this map.

Parameters:
source:
(optional) an object that contains key-value pairs to populate this map with

Instance methods

clear

function clear (
Void) : Void
Clears all mappings.

containsKey

function containsKey (
key) : Boolean
Checks if the passed-in key exists.

That means whether a value has been mapped to it.

Parameters:
key:
the key to be checked for availability
Returns:
true if the key exists else false

containsValue

function containsValue (
value) : Boolean
Checks if the passed-in value is mapped to a key.
Parameters:
value:
the value to be checked for availability
Returns:
true if the value is mapped to a key else false

get

function get (
key)
Returns the value that is mapped to the passed-in key.
Parameters:
key:
the key to return the corresponding value for
Returns:
the value corresponding to the passed-in key

getKeys

function getKeys (
Void) : Array
Returns an array that contains all keys that have a value mapped to it.
Returns:
an array that contains all keys

getValues

function getValues (
Void) : Array
Returns an array that contains all values that are mapped to a key.
Returns:
an array that contains all mapped values

isEmpty

function isEmpty (
Void) : Boolean
Returns whether this map contains any mappings.
Returns:
true if this map contains no mappings else false

iterator

function iterator (
Void) : Iterator
Returns an iterator to iterate over the values of this map.
Returns:
an iterator to iterate over the values of this map

keyIterator

function keyIterator (
Void) : Iterator
Returns an iterator to iterate over the keys of this map.
Returns:
an iterator to iterate over the keys of this map
See also:

put

function put (
key, value)
Maps the given key to the value.

null and undefined values are allowed.

Parameters:
key :
the key used as identifier for the value
value:
the value to map to the key
Returns:
the value that was originally mapped to the key or undefined

putAll

function putAll (
map:Map) : Void
Copies all mappings from the passed-in map to this map.
Parameters:
map:
the mappings to add to this map

remove

function remove (
key)
Removes the mapping from the given key to the value.
Parameters:
key:
the key identifying the mapping to remove
Returns:
the value that was originally mapped to the key

size

function size (
Void) : Number
Returns the amount of mappings.
Returns:
the amount of mappings

toString

function toString (
) : String
Returns the string representation of this map.

The string representation is obtained using the stringifier returned by the static AbstractMap.getStringifier method.

Returns:
the string representation of this map

valueIterator

function valueIterator (
Void) : Iterator
Returns an iterator to iterate over the values of this map.
Returns:
an iterator to iterate over the values of this map