package com.qianwen.smart.core.auto.common; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; public class MultiSetMap { private final transient Map> map = new HashMap<>(); private Set createSet() { return new HashSet<>(); } public boolean put(K key, V value) { Set set = this.map.get(key); if (set == null) { set = createSet(); if (set.add(value)) { this.map.put(key, set); return true; } throw new AssertionError("New set violated the set spec"); } if (set.add(value)) return true; return false; } public boolean containsKey(K key) { return this.map.containsKey(key); } public boolean containsVal(V value) { Collection> values = this.map.values(); return values.stream().anyMatch(vs -> vs.contains(value)); } public Set keySet() { return this.map.keySet(); } public boolean putAll(K key, Set set) { if (set == null) return false; this.map.put(key, set); return true; } public Set get(K key) { return this.map.get(key); } public void clear() { this.map.clear(); } public boolean isEmpty() { return this.map.isEmpty(); } }