All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aspectran.core.util.LinkedMultiValueMap Maven / Gradle / Ivy

/*
 * Copyright (c) 2008-2017 The Aspectran Project
 *
 * 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 com.aspectran.core.util;

import java.io.Serializable;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * Simple implementation of {@link MultiValueMap} that wraps a {@link LinkedHashMap},
 * storing multiple values in a {@link LinkedList}.
 *
 * 

This Map implementation is generally not thread-safe. It is primarily designed * for data structures exposed from request objects, for use in a single thread only. * * @since 3.0.0 */ public class LinkedMultiValueMap extends LinkedHashMap> implements MultiValueMap, Serializable { private static final long serialVersionUID = -9081293088810769730L; /** * Create a new LinkedMultiValueMap that wraps a {@link LinkedHashMap}. */ public LinkedMultiValueMap() { super(); } /** * Create a new LinkedMultiValueMap that wraps a {@link LinkedHashMap} * with the given initial capacity. * * @param initialCapacity the initial capacity */ public LinkedMultiValueMap(int initialCapacity) { super(initialCapacity); } /** * 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) { super(otherMap); } @Override public void add(K key, V value) { List values = get(key); if (values == null) { values = new LinkedList<>(); put(key, values); } values.add(value); } @Override public V getFirst(K key) { List values = get(key); return (values != null ? values.get(0) : null); } @Override public void set(K key, V value) { List values = new LinkedList<>(); values.add(value); put(key, values); } @Override public void setAll(Map values) { for (Map.Entry entry : values.entrySet()) { set(entry.getKey(), entry.getValue()); } } @Override public void put(K key, V[] values) { List list = new LinkedList<>(); Collections.addAll(list, values); put(key, list); } @Override public Map toSingleValueMap() { LinkedHashMap singleValueMap = new LinkedHashMap<>(size()); for (Map.Entry> entry : entrySet()) { singleValueMap.put(entry.getKey(), entry.getValue().get(0)); } return singleValueMap; } /** * Create a regular copy of this Map. * * @return a shallow copy of this Map, reusing this Map's value-holding List entries * @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 * @see #clone() */ public LinkedMultiValueMap deepCopy() { LinkedMultiValueMap copy = new LinkedMultiValueMap<>(size()); for (Map.Entry> entry : entrySet()) { copy.put(entry.getKey(), new LinkedList<>(entry.getValue())); } return copy; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy