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

org.tio.utils.collection.MultiValueMap Maven / Gradle / Ivy

There is a newer version: 1.0.8
Show newest version
/*
 * Copyright (c) 2019-2029, Dreamlu 卢春梦 ([email protected] & dreamlu.net).
 *
 * 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.tio.utils.collection;

import java.util.*;

/**
 * 多值得 map
 *
 * @param  key 泛型
 * @param  value 泛型
 * @author L.cm
 */
public class MultiValueMap implements Map> {
	private final Map> targetMap;

	public MultiValueMap() {
		this(new LinkedHashMap<>());
	}

	public MultiValueMap(Map> targetMap) {
		this.targetMap = Objects.requireNonNull(targetMap);
	}

	public void add(K key, V value) {
		Set values = this.targetMap.computeIfAbsent(key, k -> new LinkedHashSet<>());
		values.add(value);
	}

	public void addAll(K key, Set values) {
		Set currentValues = this.targetMap.computeIfAbsent(key, k -> new LinkedHashSet<>());
		currentValues.addAll(values);
	}

	public void set(K key, V value) {
		Set values = new LinkedHashSet<>();
		values.add(value);
		this.targetMap.put(key, values);
	}

	public void setAll(Map values) {
		values.forEach(this::set);
	}

	@Override
	public int size() {
		return this.targetMap.size();
	}

	@Override
	public boolean isEmpty() {
		return this.targetMap.isEmpty();
	}

	@Override
	public boolean containsKey(Object o) {
		return this.targetMap.containsKey(o);
	}

	@Override
	public boolean containsValue(Object o) {
		return this.targetMap.containsValue(o);
	}

	@Override
	public Set get(Object o) {
		return this.targetMap.get(o);
	}

	@Override
	public Set put(K k, Set vs) {
		return this.targetMap.put(k, vs);
	}

	@Override
	public Set remove(Object o) {
		return this.targetMap.remove(o);
	}

	@Override
	public void putAll(Map> map) {
		this.targetMap.putAll(map);
	}

	@Override
	public void clear() {
		this.targetMap.clear();
	}

	@Override
	public Set keySet() {
		return this.targetMap.keySet();
	}

	@Override
	public Collection> values() {
		return this.targetMap.values();
	}

	@Override
	public Set>> entrySet() {
		return this.targetMap.entrySet();
	}

	@Override
	public boolean equals(Object o) {
		return this.targetMap.equals(o);
	}

	@Override
	public int hashCode() {
		return this.targetMap.hashCode();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy