Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LinkedCaseInsensitiveMap should explicitly implement putIfAbsent and computeIfAbsent [SPR-16926] #21465

Closed
spring-projects-issues opened this issue Jun 11, 2018 · 3 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Jun 11, 2018

Juergen Hoeller opened SPR-16926 and commented

Explicit implementations of putIfAbsent and computeIfAbsent allow for reusing the converted key within those steps. Also, both variants should return the old value (if any) even for a differently cased key; this also needs to be revised in the regular put implementation.


Issue Links:

@metaruslan
Copy link

metaruslan commented Apr 25, 2021

Is it a bug that java.util.Map#putIfAbsent does not honor the case where the key is associated with a null value.
Here is the javadoc inherited from java.util.Map

If the specified key is not already associated with a value (or is mapped to null) associates it with the given value and returns null, else returns the current value.

Here is the code showing the difference with the normal map:

        Map<String, String> normalMap = new HashMap<>();
        Map<String, String> caseInsensitiveMap = new LinkedCaseInsensitiveMap<>();
        normalMap.put("key", null);
        System.out.println("normal map:" + normalMap);
        normalMap.putIfAbsent("key", "value");
        System.out.println("normal map:" + normalMap);

        caseInsensitiveMap.put("key", null);
        System.out.println("case insensitive map" + caseInsensitiveMap);
        caseInsensitiveMap.putIfAbsent("key", "value");
        System.out.println("case insensitive map" + caseInsensitiveMap);

Output:
normal map:{key=null}
normal map:{key=value}
case insensitive map{key=null}
case insensitive map{key=null}

According to the javadoc it should return null and make the change, but the code only returns null, the line: return this.targetMap.get(oldKey)
The implementation

        @Override
	@Nullable
	public V putIfAbsent(String key, @Nullable V value) {
		String oldKey = this.caseInsensitiveKeys.putIfAbsent(convertKey(key), key);
		if (oldKey != null) {
			return this.targetMap.get(oldKey);
		}
		return this.targetMap.putIfAbsent(key, value);
	}

@sbrannen
Copy link
Member

@metaruslan, this issue was closed almost 3 years ago.

If you think you have discovered a bug against a recent version of the Spring Framework, please open a new issue to describe it.

Thanks

@metaruslan
Copy link

metaruslan commented Apr 27, 2021

Done
#26868

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

4 participants