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).

static class MultiMap<A,B> {
  Map<A, L<B>> data = new HashMap<A, L<B>>();
  int fullSize;
  
  MultiMap() {}
  MultiMap(bool useTreeMap) { if (useTreeMap) data = new TreeMap; }
  MultiMap(MultiMap<A, B> map) { putAll(map); }
  *(Map<A, L<B>> *data) {}

  void put(A key, B value) { synchronized(data) {
    L<B> list = data.get(key);
    if (list == null)
      data.put(key, list = _makeEmptyList());
    list.add(value);
    ++fullSize;
  }}

  void add(A key, B value) { put(key, value); }

  void addAll(A key, Collection<B> values) { putAll(key, values); }
  
  void addAllIfNotThere(A key, Collection<B> values) { synchronized(data) {
    for (B value : values)
      setPut(key, value);
  }}
  
  void setPut(A key, B value) { synchronized(data) {
    if (!containsPair(key, value))
      put(key, value);
  }}
  
  boolean containsPair(A key, B value) { synchronized(data) {
    ret get(key).contains(value);
  }}
  
  void putAll(Cl<A> keys, B value) { synchronized(data) {
    fOr (A key : keys)
      put(key, value);
  }}

  void putAll(A key, Collection<B> values) { synchronized(data) {
    if (nempty(values)) getActual(key).addAll(values);
  }}

  void putAll(Iterable<Pair<A, B>> pairs) { synchronized(data) {
    fOr (Pair<A, B> p : pairs)
      put(p.a, p.b);
  }}
  
  void removeAll(A key, Cl<B> values) { synchronized(data) {
    for (B value : values)
      remove(key, value);
  }}
  
  List<B> get(A key) { synchronized(data) {
    List<B> list = data.get(key);
    return list == null ? Collections.<B> emptyList() : list;
  }}
  
  L<B> getOpt(A key) { synchronized(data) {
    ret data.get(key);
  }}

  L<B> getAndClear(A key) { synchronized(data) {
    L<B> l = cloneList(data.get(key));
    remove(key);
    ret l;
  }}
  
  // returns actual mutable live list
  // creates the list if not there
  List<B> getActual(A key) { synchronized(data) {
    List<B> list = data.get(key);
    if (list == null)
      data.put(key, list = _makeEmptyList());
    ret list;
  }}
 
  void clean(A key) { synchronized(data) {
    L<B> list = data.get(key);
    if (list != null && list.isEmpty()) {
      fullSize -= l(list);
      data.remove(key);
    }
  }}

  Set<A> keySet() { synchronized(data) {
    return data.keySet();
  }}

  Set<A> keys() { synchronized(data) {
    return data.keySet();
  }}

  void remove(A key) { synchronized(data) {
    fullSize -= l(this.getOpt(key));
    data.remove(key);
  }}
  
  void removePair aka remove(Pair<A, B> p) {
    if (p != null) remove(p.a, p.b);
  }

  void remove(A key, B value) { synchronized(data) {
    L<B> list = data.get(key);
    if (list != null) {
      if (list.remove(value))
        fullSize--;
      if (list.isEmpty())
        data.remove(key);
    }
  }}

  void clear() { synchronized(data) {
    data.clear();
  }}

  boolean containsKey(A key) { synchronized(data) {
    return data.containsKey(key);
  }}

  B getFirst(A key) { synchronized(data) {
    L<B> list = get(key);
    return list.isEmpty() ? null : list.get(0);
  }}
  
  void addAll(MultiMap<A, B> map) { putAll(map); }
  
  void putAll(MultiMap<A, B> map) { synchronized(data) {
    for (A key : map.keySet())
      putAll(key, map.get(key));
  }}
  
  void putAll(Map<A, B> map) { synchronized(data) {
    if (map != null) for (Map.Entry<A, B> e : map.entrySet())
      put(e.getKey(), e.getValue());
  }}
  
  int keysSize aka keyCount() { synchronized(data) { ret l(data); }}
  
  // full size - note: expensive operation
  int size aka fullSize() { synchronized(data) {
    ret fullSize;
  }}
  
  // expensive operation
  L<A> reverseGet(B b) { synchronized(data) {
    new L<A> l;
    for (A key : data.keySet())
      if (data.get(key).contains(b))
        l.add(key);
    ret l;
  }}
  
  Map<A, L<B>> asMap() { synchronized(data) {
    ret cloneMap(data);
  }}
  
  bool isEmpty() { synchronized(data) { ret data.isEmpty(); }}
  
  // override in subclasses
  L<B> _makeEmptyList() {
    ret new ArrayList;
  }
  
  // returns live lists
  Collection<L<B>> allLists() {
    synchronized(data) {
      ret new L(data.values());
    }
  }
  Cl<L<B>> values() { ret allLists(); }
  
  L<B> allValues() {
    ret concatLists(data.values());
  }
  
  O mutex() { ret data; }
  
  toString { ret "mm" + str(data); }
}

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: 804 / 8612
Version history: 33 change(s)
Referenced in: #1001294 #1001370 #1001508 #1001509 #1001600 #1003674 #1003872 #1003874 #1004091 #1004305 #1006723 #1006724 #1007595 #1010083 #1010084 #1010087 #1012280 #1024456 #1032497 #3000382