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

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

There is a newer version: 8.1.5
Show newest version
/*
 * Copyright (c) 2008-2023 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 com.aspectran.core.lang.NonNull;
import com.aspectran.core.lang.Nullable;

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

/**
 * 

This class is a clone of org.springframework.util.LinkedMultiValueMap

* * 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.

* * @param the key type * @param the value element type */ public class LinkedMultiValueMap implements MultiValueMap, Serializable, Cloneable { private static final long serialVersionUID = -9081293088810769730L; private final Map> targetMap; /** * Create a new LinkedMultiValueMap that wraps a {@link LinkedHashMap}. */ public LinkedMultiValueMap() { this.targetMap = new LinkedHashMap<>(); } /** * Create a new LinkedMultiValueMap that wraps a {@link LinkedHashMap} * with the given initial capacity. * * @param initialCapacity the initial capacity */ public LinkedMultiValueMap(int initialCapacity) { this.targetMap = new LinkedHashMap<>(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) { this.targetMap = new LinkedHashMap<>(otherMap); } // MultiValueMap implementation @Override @Nullable public V getFirst(K key) { List values = this.targetMap.get(key); return (values != null && !values.isEmpty() ? values.get(0) : null); } @Override public void add(K key, @Nullable V value) { List values = this.targetMap.computeIfAbsent(key, k -> new LinkedList<>()); values.add(value); } @Override public void addAll(K key, List values) { List currentValues = this.targetMap.computeIfAbsent(key, k -> new LinkedList<>()); currentValues.addAll(values); } @Override public void addAll(MultiValueMap values) { for (Entry> entry : values.entrySet()) { addAll(entry.getKey(), entry.getValue()); } } @Override public void set(K key, @Nullable V value) { List values = new LinkedList<>(); values.add(value); this.targetMap.put(key, values); } @Override public void set(K key, V[] values) { List list = new LinkedList<>(); if (values != null) { Collections.addAll(list, values); } put(key, list); } @Override public void setAll(Map values) { values.forEach(this::set); } @Override public Map toSingleValueMap() { LinkedHashMap singleValueMap = new LinkedHashMap<>(this.targetMap.size()); this.targetMap.forEach((key, values) -> { if (values != null && !values.isEmpty()) { singleValueMap.put(key, values.get(0)); } }); return singleValueMap; } // Map implementation @Override public int size() { return this.targetMap.size(); } @Override public boolean isEmpty() { return this.targetMap.isEmpty(); } @Override public boolean containsKey(Object key) { return this.targetMap.containsKey(key); } @Override public boolean containsValue(Object value) { return this.targetMap.containsValue(value); } @Override @Nullable public List get(Object key) { return this.targetMap.get(key); } @Override @Nullable public List put(K key, List value) { return this.targetMap.put(key, value); } @Override @Nullable public List remove(Object key) { return this.targetMap.remove(key); } @Override public void putAll(@NonNull Map> map) { this.targetMap.putAll(map); } @Override public void clear() { this.targetMap.clear(); } @Override @NonNull public Set keySet() { return this.targetMap.keySet(); } @Override @NonNull public Collection> values() { return this.targetMap.values(); } @Override @NonNull public Set>> entrySet() { return this.targetMap.entrySet(); } /** * Create a deep copy of this Map. * * @return a copy of this Map, including a copy of each value-holding List entry * (consistently using an independent modifiable {@link LinkedList} for each entry) * along the lines of {@code MultiValueMap.addAll} semantics * @see #addAll(MultiValueMap) * @see #clone() */ public LinkedMultiValueMap deepCopy() { LinkedMultiValueMap copy = new LinkedMultiValueMap<>(this.targetMap.size()); this.targetMap.forEach((key, value) -> copy.put(key, new LinkedList<>(value))); return copy; } /** * Create a regular copy of this Map. * * @return a shallow copy of this Map, reusing this Map's value-holding List entries * (even if some entries are shared or unmodifiable) along the lines of standard * {@code Map.put} semantics * @see #put(Object, List) * @see #putAll(Map) * @see LinkedMultiValueMap#LinkedMultiValueMap(Map) * @see #deepCopy() */ @Override @SuppressWarnings("MethodDoesntCallSuperMethod") public LinkedMultiValueMap clone() { return new LinkedMultiValueMap<>(this); } @Override @SuppressWarnings("EqualsWhichDoesntCheckParameterClass") public boolean equals(@Nullable Object obj) { return this.targetMap.equals(obj); } @Override public int hashCode() { return this.targetMap.hashCode(); } @Override public String toString() { return this.targetMap.toString(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy