Example usage for java.util.concurrent ConcurrentMap replace

List of usage examples for java.util.concurrent ConcurrentMap replace

Introduction

In this page you can find the example usage for java.util.concurrent ConcurrentMap replace.

Prototype

boolean replace(K key, V oldValue, V newValue);

Source Link

Document

Replaces the entry for a key only if currently mapped to a given value.

Usage

From source file:Main.java

/**
 * Replaces the entry for a key only if currently mapped to a given value.
 *
 * @param map       The map to be operated on.
 * @param key       The key with which the specified value is associated.
 * @param oldValue  The value expected to be associated with the specified key.
 * @param newValue  The value to be associated with the specified key.
 * @param <K>       The class of keys in this map.
 * @param <V>       The class of values in the map.
 * @return          True if the value was replaced.
 */// w  ww .j  a v  a2  s.  c  o m
public static <K, V> boolean replace(ConcurrentMap<K, V> map, K key, V oldValue, V newValue) {
    return map != null && map.replace(key, oldValue, newValue);
}

From source file:com.ejisto.event.listener.SessionRecorderManager.java

private <K, V> boolean replace(K key, ConcurrentMap<K, Set<V>> container, Set<V> newValue) {
    int counter = 10;
    while (counter-- > 0) {
        Set<V> currentValue = container.get(key);
        Set<V> copy = new HashSet<>(currentValue);
        copy.addAll(newValue);//from  www. j a v a 2 s .  c om
        if (container.replace(key, currentValue, copy)) {
            return true;
        }
    }
    return false;
}

From source file:com.googlecode.concurrentlinkedhashmap.ConcurrentMapTest.java

@Test(dataProvider = "guardedMap", expectedExceptions = NullPointerException.class)
public void replaceConditionally_withNullKey(ConcurrentMap<Integer, Integer> map) {
    map.replace(null, 2, 3);
}

From source file:com.googlecode.concurrentlinkedhashmap.ConcurrentMapTest.java

@Test(dataProvider = "guardedMap", expectedExceptions = NullPointerException.class)
public void replaceConditionally_withNullOldValue(ConcurrentMap<Integer, Integer> map) {
    map.replace(1, null, 3);
}

From source file:com.googlecode.concurrentlinkedhashmap.ConcurrentMapTest.java

@Test(dataProvider = "guardedMap", expectedExceptions = NullPointerException.class)
public void replaceConditionally_withNullNewValue(ConcurrentMap<Integer, Integer> map) {
    map.replace(1, 2, null);
}

From source file:com.googlecode.concurrentlinkedhashmap.ConcurrentMapTest.java

@Test(dataProvider = "guardedMap", expectedExceptions = NullPointerException.class)
public void replaceConditionally_withNullKeyAndOldValue(ConcurrentMap<Integer, Integer> map) {
    map.replace(null, null, 3);
}

From source file:com.googlecode.concurrentlinkedhashmap.ConcurrentMapTest.java

@Test(dataProvider = "guardedMap", expectedExceptions = NullPointerException.class)
public void replaceConditionally_withNullKeyAndNewValue(ConcurrentMap<Integer, Integer> map) {
    map.replace(null, 2, null);
}

From source file:com.googlecode.concurrentlinkedhashmap.ConcurrentMapTest.java

@Test(dataProvider = "guardedMap", expectedExceptions = NullPointerException.class)
public void replaceConditionally_withNullOldAndNewValue(ConcurrentMap<Integer, Integer> map) {
    map.replace(1, null, null);
}

From source file:com.googlecode.concurrentlinkedhashmap.ConcurrentMapTest.java

@Test(dataProvider = "guardedMap", expectedExceptions = NullPointerException.class)
public void replaceConditionally_withNullKeyAndValues(ConcurrentMap<Integer, Integer> map) {
    map.replace(null, null, null);
}

From source file:com.googlecode.concurrentlinkedhashmap.ConcurrentMapTest.java

@Test(dataProvider = "guardedMap")
public void replaceConditionally_whenEmpty(ConcurrentMap<Integer, Integer> map) {
    assertThat(map.replace(1, 2, 3), is(false));
}