com.iohao.game.common.kit.collect.SetMultiMap Maven / Gradle / Ivy
/*
* ioGame
* Copyright (C) 2021 - present 渔民小镇 ([email protected]、[email protected]) . All Rights Reserved.
* # iohao.com . 渔民小镇
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*/
package com.iohao.game.common.kit.collect;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
/**
* 线程安全的 SetMultiMap
*
* value 为 set 集合实现
*
* for example
* {@code
* SetMultiMap map = SetMultiMap.of();
* map.put(1, "a");
* map.put(1, "a");
* map.put(1, "b");
*
* map.size(); // size == 1
* map.sizeValue(); // sizeValue == 2
*
* Set set2 = map.get(2); // is null
* Set set2 = map.of(2); // is empty set
*
* set2.add("2 - a");
* set2.add("2 - a");
*
* map.sizeValue(); // sizeValue == 3
*
* map.containsValue("a"); // true
* map.containsValue("b"); // true
*
* var collection = map.clearAll(1);
* collection.isEmpty(); // true
* map.size(); // size == 2
*
* Set keySet = this.map.keySet();
* }
*
*
* @author 渔民小镇
* @date 2023-12-07
*/
public interface SetMultiMap extends MultiMap {
@Override
Map> asMap();
/**
* 根据 key 来 get 一个元素,如果不存在就创建集合
*
* get 的元素集合一定不为 null,如果不存在就新创建。
*
* 首次创建该 key 的集合时,会调用 consumer 并将新创建集合传入 consumer 中。
*
* 开发者有需要初始化的内容,可以通过 consumer 来实现
*
*
* @param key key
* @param consumer consumer
* @return 集合
*/
Set ofIfAbsent(K key, Consumer> consumer);
@Override
default Set of(K key) {
return this.ofIfAbsent(key, null);
}
@Override
default Set get(K key) {
return asMap().get(key);
}
Set>> entrySet();
/**
* 创建 SetMultiMap(框架内置实现)请使用 {@link SetMultiMap#of()} 代替
*
* @param k
* @param v
* @return SetMultiMap
*/
static SetMultiMap create() {
return of();
}
/**
* 创建 SetMultiMap(框架内置实现)
*
* @param k
* @param v
* @return SetMultiMap
*/
static SetMultiMap of() {
return new NonBlockingSetMultiMap<>();
}
}