From df8e9638ee88472717a0e44d2ac7cc7416e4caa1 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 7 Jul 2015 00:24:39 +0200 Subject: [PATCH] LinkedMultiValueMap explicitly supports deep copies Issue: SPR-13201 --- .../util/LinkedMultiValueMap.java | 42 ++++++++++++++++--- 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/util/LinkedMultiValueMap.java b/spring-core/src/main/java/org/springframework/util/LinkedMultiValueMap.java index f980b91c23..747f78c71f 100644 --- a/spring-core/src/main/java/org/springframework/util/LinkedMultiValueMap.java +++ b/spring-core/src/main/java/org/springframework/util/LinkedMultiValueMap.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -59,9 +59,12 @@ public class LinkedMultiValueMap implements MultiValueMap, Serializa } /** - * Copy constructor: Create a new LinkedMultiValueMap with the same mappings - * as the specified Map. + * Copy constructor: Create a new LinkedMultiValueMap with the same mappings as + * the specified Map. Note that this will be a shallow copy; its value-holding + * List entries will get reused and therefore cannot get modified independently. * @param otherMap the Map whose mappings are to be placed in this Map + * @see #clone() + * @see #deepCopy() */ public LinkedMultiValueMap(Map> otherMap) { this.targetMap = new LinkedHashMap>(otherMap); @@ -103,7 +106,7 @@ public class LinkedMultiValueMap implements MultiValueMap, Serializa @Override public Map toSingleValueMap() { LinkedHashMap singleValueMap = new LinkedHashMap(this.targetMap.size()); - for (Entry> entry : targetMap.entrySet()) { + for (Entry> entry : this.targetMap.entrySet()) { singleValueMap.put(entry.getKey(), entry.getValue().get(0)); } return singleValueMap; @@ -148,8 +151,8 @@ public class LinkedMultiValueMap implements MultiValueMap, Serializa } @Override - public void putAll(Map> m) { - this.targetMap.putAll(m); + public void putAll(Map> map) { + this.targetMap.putAll(map); } @Override @@ -173,6 +176,33 @@ public class LinkedMultiValueMap implements MultiValueMap, Serializa } + /** + * Create a regular copy of this Map. + * @return a shallow copy of this Map, reusing this Map's value-holding List entries + * @since 4.2 + * @see LinkedMultiValueMap#LinkedMultiValueMap(Map) + * @see #deepCopy() + */ + @Override + public LinkedMultiValueMap clone() { + return new LinkedMultiValueMap(this); + } + + /** + * Create a deep copy of this Map. + * @return a copy of this Map, including a copy of each value-holding List entry + * @since 4.2 + * @see #clone() + */ + public LinkedMultiValueMap deepCopy() { + LinkedMultiValueMap copy = new LinkedMultiValueMap(this.targetMap.size()); + for (Map.Entry> entry : this.targetMap.entrySet()) { + copy.put(entry.getKey(), new LinkedList(entry.getValue())); + } + return copy; + } + + @Override public boolean equals(Object obj) { return this.targetMap.equals(obj);