Wrapped maps with default values
Gosu provides a class called gw.util.AutoMap that implements the Java interface
java.util.Map and the Gosu interface gw.lang.reflect.gs.IGosuObject. This
class provides an alternative to using the standard java.util.HashMap class.
The AutoMap<K,V> class provides a default value for the map if the key is not present. When
you create an AutoMap object, you pass a Gosu block into the constructor. If your code calls
get on the map and its value is null, Gosu runs the mapping block that you
provide. Gosu takes the return value from the block, stores it in the map for that key, and returns the value to
the original caller. All other methods delegate directly to the wrapped map.
The simplest constructor takes only one
argument, which is a Gosu block that provides a default value. The block
takes one argument, which is the key, and returns the default value to
use. If you use this constructor, Gosu creates a new instance of java.util.HashMap as its wrapped
map.
Another constructor takes a map (any object
whose class implements java.util.Map)
as the first argument and the Gosu block as the second argument. Use
this alternative constructor if you have an existing map that you want
to wrap rather than create a new map.
For example, the following code assigns values in a new map to entries that do not have values in the original map:
var origMap = { 1 -> "apple", 4 -> "orange"}
var newMap = origMap.toAutoMap( \ k -> "I want ${k} blueberries")
for (i in 1..5) {
print(newMap[i])
}
This code prints:
apple
I want 2 blueberries
I want 3 blueberries
orange
I want 5 blueberries
Conversion methods
The examples in the following table are based on the following code:
var mapper = new HashMap<String, String>()
mapper.put("aKey", "aValue")
mapper.put("bKey", "bValue")
var automap = mapper.toAutoMap(\k -> k)
| Method | Returns | Description | Example |
|---|---|---|---|
clear() |
void |
Clears the AutoMap. |
Output:
|
entrySet() |
java.util.Set<java.util.Map.Entry<K,V>> |
Retrieves the key-value set for the AutoMap. |
|
hashCode() |
int |
Retrieves the hash code value. |
|
keySet() |
java.util.Set<K> |
Retrieves the set of keys for the AutoMap. |
|
put(java.lang.Object o, java.lang.Object o1) |
java.lang.Object |
Adds the new key value pair to the AutoMap. |
Output:
|
putAll(java.util.Map<K,V> map) |
void |
Adds the specified map information to the AutoMap. |
Output:
|
remove(java.lang.Object o) |
java.lang.Object |
Removes the specified object from the AutoMap. |
Output:
|
values() |
java.util.Collection<V> |
Retrieves the set of values for the AutoMap. |
|
Evaluation methods
| Method | Returns | Description |
|---|---|---|
containsKey(java.lang.Object o) |
boolean |
Determines if the specified object is a key in the AutoMap. |
containsValue(java.lang.Object o) |
boolean |
Determines if the specified object is a value in the AutoMap. |
equals() |
boolean |
Evaluates if the specified object is in the AutoMap. |
get() |
java.lang.Object |
Retrieves the specified object in the AutoMap. |
isEmpty() |
boolean |
Determines if the AutoMap is empty. |
size() |
int |
Retrieves the number of entries in the AutoMap. |
