com.aspectran.core.util.LinkedCaseInsensitiveMultiValueMap Maven / Gradle / Ivy
/*
* Copyright (c) 2008-2022 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.Locale;
import java.util.Map;
import java.util.Set;
/**
* This class is a clone of org.springframework.util.LinkedCaseInsensitiveMultiValueMap
*
* {@link LinkedHashMap} variant that stores String keys in a case-insensitive
* manner, for example for key-based access in a results table.
*
* Preserves the original order as well as the original casing of keys,
* while allowing for contains, get and remove calls with any case of key.
*
* Does not support {@code null} keys.
*
* @param the value type
*/
public class LinkedCaseInsensitiveMultiValueMap implements MultiValueMap, Serializable {
private static final long serialVersionUID = 2505523262093891621L;
private final Map> targetMap;
/**
* Constructs a new, empty instance of the {@code LinkedCaseInsensitiveMultiValueMap} object.
*/
public LinkedCaseInsensitiveMultiValueMap() {
this.targetMap = new LinkedCaseInsensitiveMap<>(Locale.ENGLISH);
}
/**
* Constructs a new, empty instance of the {@code LinkedCaseInsensitiveMultiValueMap} object.
* @param initialCapacity the initial capacity
*/
public LinkedCaseInsensitiveMultiValueMap(int initialCapacity) {
this.targetMap = new LinkedCaseInsensitiveMap<>(initialCapacity, Locale.ENGLISH);
}
@Override
public V getFirst(String key) {
List values = this.targetMap.get(key);
return (values != null ? values.get(0) : null);
}
@Override
public void add(String key, V value) {
List values = this.targetMap.computeIfAbsent(key, k -> new LinkedList<>());
values.add(value);
}
@Override
public void addAll(String key, List extends V> 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(String key, V value) {
List values = new LinkedList<>();
values.add(value);
this.targetMap.put(key, values);
}
@Override
public void set(String key, V[] values) {
List list = new LinkedList<>();
if (values != null) {
Collections.addAll(list, values);
}
put(key, list);
}
@Override
public void setAll(Map values) {
for (Entry entry : values.entrySet()) {
set(entry.getKey(), entry.getValue());
}
}
@Override
public Map toSingleValueMap() {
LinkedHashMap singleValueMap = new LinkedHashMap<>(this.targetMap.size());
for (Entry> entry : this.targetMap.entrySet()) {
singleValueMap.put(entry.getKey(), entry.getValue().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
public List get(Object key) {
return this.targetMap.get(key);
}
@Override
public List put(String key, List value) {
return this.targetMap.put(key, value);
}
@Override
public List remove(Object key) {
return this.targetMap.remove(key);
}
@Override
public void putAll(@NonNull Map extends String, ? extends List> 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();
}
@Override
public boolean equals(@Nullable Object other) {
if (this == other) {
return true;
}
if (!(other instanceof LinkedCaseInsensitiveMultiValueMap)) {
return false;
}
LinkedCaseInsensitiveMultiValueMap> otherValues = (LinkedCaseInsensitiveMultiValueMap>)other;
return this.targetMap.equals(otherValues.targetMap);
}
@Override
public int hashCode() {
return this.targetMap.hashCode();
}
@Override
public String toString() {
return this.targetMap.toString();
}
}