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

1  
// A FlexibleVarContext is thread-safe AFTER the first variable has been defined
2  
// or after you call makeThreadSafe(), whichever comes first
3  
sclass FlexibleVarContext > VarContext {
4  
  MapSO vars;
5  
  
6  
  *() {}
7  
  *(VarContext parent) { super(parent); }
8  
  
9  
  O get(S name) {
10  
    if (containsKey(vars, name))
11  
      ret mapGet(vars, name);
12  
    if (parent != null)
13  
      ret parent.get(name);
14  
    null;
15  
  }
16  
  
17  
  void set(S name, O value) {
18  
    vars = putOrCreateSyncMap(vars, name, value);
19  
  }
20  
  
21  
  AutoCloseable tempSet(S name, O value) {
22  
    initMap();
23  
    ret main tempPut(vars, name, value);
24  
  }
25  
  
26  
  void unset(S name) {
27  
    remove(vars, name);
28  
  }
29  
  
30  
  MapSO varMap() { ret vars; }
31  
32  
  void makeThreadSafe aka initMap() {
33  
    vars if null = syncHashMap();
34  
  }
35  
}

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: 106 / 163
Version history: 2 change(s)
Referenced in: [show references]