const classafConcurrent::AtomicMap

sys::Obj
  afConcurrent::AtomicMap

A Map that provides fast reads and lightweight writes between threads. Use when reads far out number the writes.

The map is stored in an AtomicRef through which all reads are made. Writing makes a rw copy of the map and is thus a more expensive operation.

CAUTION: Write operations ( getOrAdd, set, remove & clear ) are not synchronised. This makes them lightweight but also susceptible to data-loss during race conditions. This may be acceptable for caching situations where values is easily re-calculated.

Note that all objects held in the map must be immutable.

See From One Thread to Another... for more details.

caseInsensitive

Source

const Bool caseInsensitive := false

Configures case sensitivity for maps with Str keys.

AtomicMap() { it.keyType = Str#; it.caseInsensitive = true }
clear

Source

This clear()

Remove all key/value pairs from the map. Return this.

containsKey

Source

Bool containsKey(Obj key)

Returns true if the map contains the given key

def

Source

const Obj? def := null

The default value to use for get when a key isn't mapped.

get

Source

@Operator
Obj? get(Obj key, Obj? def := this.def)

Returns the value associated with the given key. If key is not mapped, then return the value of the def parameter. If def is omitted it defaults to null.

getOrAdd

Source

Obj? getOrAdd(Obj key, |Obj->Obj? valFunc)

Returns the value associated with the given key. If it doesn't exist then it is added from the value function.

This method is NOT thread safe. If two Actors call this method at the same time, the value function will be called twice for the same key.

isEmpty

Source

Bool isEmpty()

Return true if size() == 0

keyType

Source

const Type keyType := sys::Obj#

Used to parameterize the backing map. Must be non-nullable.

AtomicMap() { it.keyType = Int# }
keys

Source

Obj[] keys()

Returns a list of all the mapped keys.

map

Source

Obj:Obj? map

Gets or sets a read-only copy of the backing map.

ordered

Source

const Bool ordered := false

If true the map will maintain the order in which key/value pairs are added.

AtomicMap() { it.ordered = true }
remove

Source

Obj? remove(Obj key)

Remove the key/value pair identified by the specified key from the map and return the value. If the key was not mapped then return null.

rw

Source

Obj:Obj? rw()

Get a read-write, mutable Map instance with the same contents.

set

Source

@Operator
Void set(Obj key, Obj? val)

Sets the key / value pair.

size

Source

Int size()

Get the number of key/value pairs in the map.

valType

Source

const Type valType := sys::Obj?#

Used to parameterize the backing map.

AtomicMap() { it.valType = Int# }
vals

Source

Obj?[] vals()

Returns a list of all the mapped values.