Java Code Examples for java.util.Map.computeIfPresent()

The following are Jave code examples for showing how to use computeIfPresent() of the java.util.Map class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: L2J-Global   File: AntiFeedManager.java   View Source Code Vote up 6 votes
/**
 * Decreasing number of active connection from player's IP address
 * @param eventId
 * @param client
 * @return true if success and false if any problem detected.
 */
public final boolean removeClient(int eventId, L2GameClient client)
{
	if (client == null)
	{
		return false; // unable to determine IP address
	}
	
	final Map<Integer, AtomicInteger> event = _eventIPs.get(eventId);
	if (event == null)
	{
		return false; // no such event registered
	}
	
	final Integer addrHash = Integer.valueOf(client.getConnectionAddress().hashCode());
	
	return event.computeIfPresent(addrHash, (k, v) ->
	{
		if ((v == null) || (v.decrementAndGet() == 0))
		{
			return null;
		}
		return v;
	}) != null;
}
 
Example 2
Project: Shuriken   File: MethodReflectorFactory.java   View Source Code Vote up 5 votes
@Contract("_, null -> fail; null, _ -> null")
private String replacePlaceholders(String source, Map<String, String> replacements) {
    Ensure.notNull(replacements, "Replacements map shouldn't be null!");
    if(source == null) return null;

    /* Find placeholders */
    List<String> foundPlaceholders = new ArrayList<>();
    StringBuilder lastPlaceholder = null;
    for(char c: source.toCharArray()) {
        if(c == '{' && lastPlaceholder == null) {
            lastPlaceholder = new StringBuilder();
        } else if(lastPlaceholder != null && c != '}') {
            lastPlaceholder.append(c);
        } else if(lastPlaceholder != null) {
            foundPlaceholders.add(lastPlaceholder.toString());
            lastPlaceholder = null;
        }
    }

    /* Amazing hack, wow */
    String[] a = new String[] { source };

    /* Replace placeholders */
    for(String placeholder: foundPlaceholders) {
        replacements.computeIfPresent(placeholder, (k, value) -> {
            a[0] = a[0].replace("{" + k + "}", value);
            return value;
        });
    }

    return a[0];
}
 
Example 3
Project: openjdk-jdk10   File: InPlaceOpsCollisions.java   View Source Code Vote up 5 votes
private static <T> void testComputeIfPresent(Map<T, T> map, String desc, T[] keys,
        BiFunction<T, T, T> mappingFunction) {
    // remove a third of the keys
    // call testComputeIfPresent for all keys[]
    // removed keys should remain absent, even keys should be mapped to $RESULT
    // no value from keys[] should be in map
    T funcResult = mappingFunction.apply(keys[0], keys[0]);
    int expectedSize1 = 0;
    removeThirdKeys(map, keys);

    for (int i = 0; i < keys.length; i++) {
        T retVal = map.computeIfPresent(keys[i], mappingFunction);
        if (i % 3 != 2) { // key present
            if (funcResult == null) { // was removed
                assertFalse(map.containsKey(keys[i]),
                        String.format("replaceIfMapped: containsKey(%s[%d])", desc, i));
            } else { // value was replaced
                assertTrue(map.containsKey(keys[i]),
                        String.format("replaceIfMapped: containsKey(%s[%d])", desc, i));
                expectedSize1++;
            }
            assertEquals(retVal, funcResult,
                    String.format("computeIfPresent: retVal(%s[%s])", desc, i));
            assertEquals(funcResult, map.get(keys[i]),
                    String.format("replaceIfMapped: get(%s[%d])", desc, i));

        } else { // odd: was removed, should not be replaced
            assertNull(retVal,
                    String.format("replaceIfMapped: retVal(%s[%d])", desc, i));
            assertNull(map.get(keys[i]),
                    String.format("replaceIfMapped: get(%s[%d])", desc, i));
            assertFalse(map.containsKey(keys[i]),
                    String.format("replaceIfMapped: containsKey(%s[%d])", desc, i));
        }
        assertFalse(map.containsValue(keys[i]),
                String.format("replaceIfMapped: !containsValue(%s[%d])", desc, i));
    }
    assertEquals(map.size(), expectedSize1,
            String.format("map expected size#1 m%d != k%d", map.size(), expectedSize1));
}