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

35
LINES

< > BotCompany Repo | #1035281 // FlexibleVarContext - a VarContext where you can add variables by name at runtime

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

Libraryless. Click here for Pure Java version (11222L/62K).

// A FlexibleVarContext is thread-safe AFTER the first variable has been defined
// or after you call makeThreadSafe(), whichever comes first
sclass FlexibleVarContext > VarContext {
  MapSO vars;
  
  *() {}
  *(VarContext parent) { super(parent); }
  
  O get(S name) {
    if (containsKey(vars, name))
      ret mapGet(vars, name);
    if (parent != null)
      ret parent.get(name);
    null;
  }
  
  void set(S name, O value) {
    vars = putOrCreateSyncMap(vars, name, value);
  }
  
  AutoCloseable tempSet(S name, O value) {
    initMap();
    ret main tempPut(vars, name, value);
  }
  
  void unset(S name) {
    remove(vars, name);
  }
  
  MapSO varMap() { ret vars; }

  void makeThreadSafe aka initMap() {
    vars if null = syncHashMap();
  }
}

Author comment

Began life as a copy of #1033977

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1035281
Snippet name: FlexibleVarContext - a VarContext where you can add variables by name at runtime
Eternal ID of this version: #1035281/3
Text MD5: fc467ef3e3654489e105a91c02bc3fc0
Transpilation MD5: ed99992e986c18b278bf2233566a5abf
Author: stefan
Category: javax / parsing
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2023-09-19 20:07:56
Source code size: 794 bytes / 35 lines
Pitched / IR pitched: No / No
Views / Downloads: 102 / 156
Version history: 2 change(s)
Referenced in: [show references]