Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

181
LINES

< > BotCompany Repo | #1001296 - MultiMap - now synchronized (on the inner map)

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (4488L/25K).

1  
static class MultiMap<A,B> {
2  
  Map<A, L<B>> data = new HashMap<A, L<B>>();
3  
  int fullSize;
4  
  
5  
  MultiMap() {}
6  
  MultiMap(bool useTreeMap) { if (useTreeMap) data = new TreeMap; }
7  
  MultiMap(MultiMap<A, B> map) { putAll(map); }
8  
  *(Map<A, L<B>> *data) {}
9  
10  
  void put(A key, B value) { synchronized(data) {
11  
    L<B> list = data.get(key);
12  
    if (list == null)
13  
      data.put(key, list = _makeEmptyList());
14  
    list.add(value);
15  
    ++fullSize;
16  
  }}
17  
18  
  void add(A key, B value) { put(key, value); }
19  
20  
  void addAll(A key, Collection<B> values) { putAll(key, values); }
21  
  
22  
  void addAllIfNotThere(A key, Collection<B> values) { synchronized(data) {
23  
    for (B value : values)
24  
      setPut(key, value);
25  
  }}
26  
  
27  
  void setPut(A key, B value) { synchronized(data) {
28  
    if (!containsPair(key, value))
29  
      put(key, value);
30  
  }}
31  
  
32  
  boolean containsPair(A key, B value) { synchronized(data) {
33  
    ret get(key).contains(value);
34  
  }}
35  
  
36  
  void putAll(Cl<A> keys, B value) { synchronized(data) {
37  
    fOr (A key : keys)
38  
      put(key, value);
39  
  }}
40  
41  
  void putAll(A key, Collection<B> values) { synchronized(data) {
42  
    if (nempty(values)) getActual(key).addAll(values);
43  
  }}
44  
45  
  void putAll(Iterable<Pair<A, B>> pairs) { synchronized(data) {
46  
    fOr (Pair<A, B> p : pairs)
47  
      put(p.a, p.b);
48  
  }}
49  
  
50  
  void removeAll(A key, Cl<B> values) { synchronized(data) {
51  
    for (B value : values)
52  
      remove(key, value);
53  
  }}
54  
  
55  
  List<B> get(A key) { synchronized(data) {
56  
    List<B> list = data.get(key);
57  
    return list == null ? Collections.<B> emptyList() : list;
58  
  }}
59  
  
60  
  L<B> getOpt(A key) { synchronized(data) {
61  
    ret data.get(key);
62  
  }}
63  
64  
  L<B> getAndClear(A key) { synchronized(data) {
65  
    L<B> l = cloneList(data.get(key));
66  
    remove(key);
67  
    ret l;
68  
  }}
69  
  
70  
  // returns actual mutable live list
71  
  // creates the list if not there
72  
  List<B> getActual(A key) { synchronized(data) {
73  
    List<B> list = data.get(key);
74  
    if (list == null)
75  
      data.put(key, list = _makeEmptyList());
76  
    ret list;
77  
  }}
78  
 
79  
  void clean(A key) { synchronized(data) {
80  
    L<B> list = data.get(key);
81  
    if (list != null && list.isEmpty()) {
82  
      fullSize -= l(list);
83  
      data.remove(key);
84  
    }
85  
  }}
86  
87  
  Set<A> keySet() { synchronized(data) {
88  
    return data.keySet();
89  
  }}
90  
91  
  Set<A> keys() { synchronized(data) {
92  
    return data.keySet();
93  
  }}
94  
95  
  void remove(A key) { synchronized(data) {
96  
    fullSize -= l(this.getOpt(key));
97  
    data.remove(key);
98  
  }}
99  
  
100  
  void removePair aka remove(Pair<A, B> p) {
101  
    if (p != null) remove(p.a, p.b);
102  
  }
103  
104  
  void remove(A key, B value) { synchronized(data) {
105  
    L<B> list = data.get(key);
106  
    if (list != null) {
107  
      if (list.remove(value))
108  
        fullSize--;
109  
      if (list.isEmpty())
110  
        data.remove(key);
111  
    }
112  
  }}
113  
114  
  void clear() { synchronized(data) {
115  
    data.clear();
116  
  }}
117  
118  
  boolean containsKey(A key) { synchronized(data) {
119  
    return data.containsKey(key);
120  
  }}
121  
122  
  B getFirst(A key) { synchronized(data) {
123  
    L<B> list = get(key);
124  
    return list.isEmpty() ? null : list.get(0);
125  
  }}
126  
  
127  
  void addAll(MultiMap<A, B> map) { putAll(map); }
128  
  
129  
  void putAll(MultiMap<A, B> map) { synchronized(data) {
130  
    for (A key : map.keySet())
131  
      putAll(key, map.get(key));
132  
  }}
133  
  
134  
  void putAll(Map<A, B> map) { synchronized(data) {
135  
    if (map != null) for (Map.Entry<A, B> e : map.entrySet())
136  
      put(e.getKey(), e.getValue());
137  
  }}
138  
  
139  
  int keysSize aka keyCount() { synchronized(data) { ret l(data); }}
140  
  
141  
  // full size - note: expensive operation
142  
  int size aka fullSize() { synchronized(data) {
143  
    ret fullSize;
144  
  }}
145  
  
146  
  // expensive operation
147  
  L<A> reverseGet(B b) { synchronized(data) {
148  
    new L<A> l;
149  
    for (A key : data.keySet())
150  
      if (data.get(key).contains(b))
151  
        l.add(key);
152  
    ret l;
153  
  }}
154  
  
155  
  Map<A, L<B>> asMap() { synchronized(data) {
156  
    ret cloneMap(data);
157  
  }}
158  
  
159  
  bool isEmpty() { synchronized(data) { ret data.isEmpty(); }}
160  
  
161  
  // override in subclasses
162  
  L<B> _makeEmptyList() {
163  
    ret new ArrayList;
164  
  }
165  
  
166  
  // returns live lists
167  
  Collection<L<B>> allLists() {
168  
    synchronized(data) {
169  
      ret new L(data.values());
170  
    }
171  
  }
172  
  Cl<L<B>> values() { ret allLists(); }
173  
  
174  
  L<B> allValues() {
175  
    ret concatLists(data.values());
176  
  }
177  
  
178  
  O mutex() { ret data; }
179  
  
180  
  toString { ret "mm" + str(data); }
181  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 22 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, ekrmjmnbrukm, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, jtubtzbbkimh, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, ppjhyzlbdabe, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, whxojlpjdney, wtqryiryparv, xrpafgyirdlv

No comments. add comment

Snippet ID: #1001296
Snippet name: MultiMap - now synchronized (on the inner map)
Eternal ID of this version: #1001296/34
Text MD5: 173e590c61e32ae6cc20efb424cf7e11
Transpilation MD5: fc30f9614a1249f5a60e19a4ccf8e427
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-10-23 07:42:33
Source code size: 4455 bytes / 181 lines
Pitched / IR pitched: No / No
Views / Downloads: 803 / 8612
Version history: 33 change(s)
Referenced in: [show references]