diff --git a/org.springframework.core/src/main/java/org/springframework/util/DefaultMultiValueMap.java b/org.springframework.core/src/main/java/org/springframework/util/DefaultMultiValueMap.java new file mode 100644 index 00000000000..ef82902e272 --- /dev/null +++ b/org.springframework.core/src/main/java/org/springframework/util/DefaultMultiValueMap.java @@ -0,0 +1,115 @@ +/* + * Copyright 2002-2009 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.util; + +import java.util.Collection; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * Default implementation of {@link MultiValueMap} that wraps a plain {@code Map}. + * + * @author Arjen Poutsma + * @since 3.0 + */ +public class DefaultMultiValueMap implements MultiValueMap { + + private final Map> wrappee; + + public DefaultMultiValueMap(Map> wrappee) { + Assert.notNull(wrappee, "'wrappee' must not be null"); + this.wrappee = wrappee; + } + + /* + * MultiValueMap implementation + */ + + public void add(K key, V value) { + List values = wrappee.get(key); + if (values == null) { + values = new LinkedList(); + wrappee.put(key, values); + } + values.add(value); + } + + public V getFirst(K key) { + List values = wrappee.get(key); + return values != null ? values.get(0) : null; + } + + public void set(K key, V value) { + List values = new LinkedList(); + values.add(value); + wrappee.put(key, values); + } + + /* + * Map implementation + */ + + public int size() { + return wrappee.size(); + } + + public boolean isEmpty() { + return wrappee.isEmpty(); + } + + public boolean containsKey(Object key) { + return wrappee.containsKey(key); + } + + public boolean containsValue(Object value) { + return wrappee.containsValue(value); + } + + public List get(Object key) { + return wrappee.get(key); + } + + public List put(K key, List value) { + return wrappee.put(key, value); + } + + public List remove(Object key) { + return wrappee.remove(key); + } + + public void putAll(Map> m) { + wrappee.putAll(m); + } + + public void clear() { + wrappee.clear(); + } + + public Set keySet() { + return wrappee.keySet(); + } + + public Collection> values() { + return wrappee.values(); + } + + public Set>> entrySet() { + return wrappee.entrySet(); + } +} diff --git a/org.springframework.core/src/main/java/org/springframework/util/MultiValueMap.java b/org.springframework.core/src/main/java/org/springframework/util/MultiValueMap.java new file mode 100644 index 00000000000..684fbe669bb --- /dev/null +++ b/org.springframework.core/src/main/java/org/springframework/util/MultiValueMap.java @@ -0,0 +1,54 @@ +/* + * Copyright 2002-2009 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.util; + +import java.util.List; +import java.util.Map; + +/** + * Extension of the {@code Map} interface that stores multiple values. + * + * @author Arjen Poutsma + * @since 3.0 + */ +public interface MultiValueMap extends Map> { + + /** + * Adds the given single value to the current list of values for the given key. + * + * @param key the key + * @param value the value to be added + */ + void add(K key, V value); + + /** + * Returns the first value for the given key. + * + * @param key the key + * @return the first value for the specified key, or null + */ + V getFirst(K key); + + /** + * Sets the given single value under the given key. + * + * @param key the key + * @param value the value to set + */ + void set(K key, V value); + +} diff --git a/org.springframework.web/src/main/java/org/springframework/web/http/HttpHeaders.java b/org.springframework.web/src/main/java/org/springframework/web/http/HttpHeaders.java index 8b174c31304..f895e55caf8 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/http/HttpHeaders.java +++ b/org.springframework.web/src/main/java/org/springframework/web/http/HttpHeaders.java @@ -32,6 +32,7 @@ import java.util.Set; import org.springframework.core.CollectionFactory; import org.springframework.util.Assert; import org.springframework.util.MediaType; +import org.springframework.util.MultiValueMap; import org.springframework.util.StringUtils; /** @@ -47,7 +48,7 @@ import org.springframework.util.StringUtils; * @author Arjen Poutsma * @since 3.0 */ -public final class HttpHeaders implements Map> { +public final class HttpHeaders implements MultiValueMap { private static String ACCEPT = "Accept";