yangys
2024-03-27 e48aa2ac8dea1be5db11c63edf0b912c4ad5ce65
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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<K, V> {
  private final transient Map<K, Set<V>> map = new HashMap<>();
  
  private Set<V> createSet() {
    return new HashSet<>();
  }
  
  public boolean put(K key, V value) {
    Set<V> 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<Set<V>> values = this.map.values();
    return values.stream().anyMatch(vs -> vs.contains(value));
  }
  
  public Set<K> keySet() {
    return this.map.keySet();
  }
  
  public boolean putAll(K key, Set<V> set) {
    if (set == null)
      return false; 
    this.map.put(key, set);
    return true;
  }
  
  public Set<V> get(K key) {
    return this.map.get(key);
  }
  
  public void clear() {
    this.map.clear();
  }
  
  public boolean isEmpty() {
    return this.map.isEmpty();
  }
}