< prev index next >
src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/LinkedMap.java
Print this page
@@ -60,11 +60,11 @@
* A node of a linked list that is used as value in our map. The linked list uses insertion order
* and allows fast iteration over its element even while the map is modified.
*/
static class Node {
private final Object key;
- private final Object value;
+ private volatile Object value;
private volatile boolean alive = true;
private volatile Node prev;
private volatile Node next;
@@ -101,10 +101,18 @@
* @return the value
*/
public Object getValue() {
return value;
}
+
+ /**
+ * Set the node's value
+ * @param value the new value
+ */
+ void setValue(final Object value) {
+ this.value = value;
+ }
}
/**
* An iterator over the elements in the map.
*/
@@ -148,16 +156,18 @@
* Add a key-value pair to the map.
* @param key the key
* @param value the value
*/
public void set(final Object key, final Object value) {
- final Node newNode = new Node(key, value);
- final Node oldNode = data.put(key, newNode);
- if (oldNode != null) {
- unlink(oldNode);
+ Node node = data.get(key);
+ if (node != null) {
+ node.setValue(value);
+ } else {
+ node = new Node(key, value);
+ data.put(key, node);
+ link(node);
}
- link(newNode);
}
/**
* Get the value associated with {@code key}.
* @param key the key
< prev index next >