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

47
LINES

< > BotCompany Repo | #1028138 // ValueOnDemandMap - map over a key set where values are calculated on every access

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

Libraryless. Click here for Pure Java version (2696L/17K).

sclass ValueOnDemandMap<A, B> extends AbstractMap<A, B> {
  Set<A> keys; // keys may be changed if map is changed
  IF1<A, B> makeValue;
  
  *() {}
  *(Set<A> *keys, IF1<A, B> *makeValue) {} // doesn't clone keys
  
  public B get(O a) {
    ret !containsKey(a) ? null : makeValue.get((A) a);
  }
  
  public bool containsKey(O a) {
    ret keys.contains(a);
  }
  
  public Set<Map.Entry<A, B>> entrySet() {
    ret new EntrySet;
  }
  
  final class EntrySet extends AbstractSet<Map.Entry<A,B>> {
    public final int size() { ret keys.size(); }
    public final void clear() { keys.clear(); }
    public final Iterator<Map.Entry<A,B>> iterator() {
      return mapI(keys.iterator(), key -> new Map.Entry<A, B>() {
        public A getKey() { ret key; }
        public B getValue() { ret makeValue.get(key); }
        public B setValue(B newValue) {
          throw unimplemented();
        }
      });
    }
    public final boolean contains(Object o) {
      if (o cast Map.Entry)
        ret keys.contains(o.getKey());
      false;
    }
    public final boolean remove(Object o) {
      if (o cast Map.Entry)
        ret keys.remove(o.getKey());
      false;
    }
  }

  public Set<A> keySet() { ret keys; }
  
  public int size() { ret keys.size(); }
}

Author comment

Began life as a copy of #1008575

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1028138
Snippet name: ValueOnDemandMap - map over a key set where values are calculated on every access
Eternal ID of this version: #1028138/12
Text MD5: 95661ad9b4011faa89cd994cd2fdbca1
Transpilation MD5: 1b44fe9d830feb8f1e170c857ecf974c
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-05-23 14:34:54
Source code size: 1307 bytes / 47 lines
Pitched / IR pitched: No / No
Views / Downloads: 285 / 645
Version history: 11 change(s)
Referenced in: [show references]