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

154
LINES

< > BotCompany Repo | #1034711 // G22Variable - a project-wide variable (name + any Java value, optionally persistent)

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

Transpiled version (67608L) is out of date.

1  
concept G22Variable extends ConceptWithChangeListeners is IPersistenceInfo, AutoCloseable {
2  
  settableWithVar S name;
3  
  volatile O value;
4  
  settableWithVar bool persistent;
5  
  
6  
  // dispose of resource when project is closed
7  
  // or variable is deleted/overwritten
8  
  settable bool autoClose;
9  
  
10  
  // big = value is stored in separate file
11  
  settableWithVar bool big;
12  
  
13  
  transient Lock loadLock = lock();
14  
  
15  
  // field "value" is persisted iff field "persistent" is set
16  
  public Map _persistenceInfo() { ret litmap(value := persistent && !big); }
17  
  
18  
  public transient simplyCached FieldVar<O> varValue() {
19  
    ret new FieldVar<O>(this, "value", () -> value(), value -> value(value));
20  
  }
21  
  
22  
  void setValueIfNull(O defaultValue) {
23  
    if (defaultValue != null && value == null) value(defaultValue);
24  
  }
25  
  
26  
  O cachedValue() { ret value; }
27  
  
28  
  O get aka value() {
29  
    if (value != null) ret value;
30  
    if (big) ret getBigValue();
31  
    ret value;
32  
  }
33  
  
34  
  O getOnce() {
35  
    if (value != null) ret value;
36  
    if (big) ret loadBigValueImpl();
37  
    ret value;
38  
  }
39  
  
40  
  G22Utils g22utils() {
41  
    ret main g22utils(this);
42  
  }
43  
  
44  
  File bigFile() {
45  
    ret newFile(assertNotNull(g22utils().projectDir()), id + "-" + sanitizeFileName(name) + ".structgz");
46  
  }
47  
  
48  
  O getBigValue() {
49  
    O o;
50  
    {
51  
      lock loadLock;
52  
      o = loadBigValueImpl();
53  
      setValueField(o);
54  
    }
55  
    g22utils().bigVariableLoaded(this);
56  
    ret o;
57  
  }
58  
  
59  
  O loadBigValueImpl() {
60  
    ret unstructureGZFile(bigFile(), g22utils().classFinder());
61  
  }
62  
  
63  
  void setBigValue(O value) {
64  
    if (value == null) {
65  
      if (hasBigValue()) {
66  
        deleteBigValue();
67  
        change();
68  
      }
69  
    } else {
70  
      unload();
71  
      File f = bigFile();
72  
      saveGZStructureToFile(f, value);
73  
      change();
74  
    }
75  
  }
76  
  
77  
  void deleteBigValue() {
78  
    deleteFile(bigFile());
79  
  }
80  
  
81  
  bool hasBigValue() {
82  
    ret fileExists(bigFile());
83  
  }
84  
  
85  
  void makeBig {
86  
    if (big) ret;
87  
    setBigValue(value);
88  
    big(true);
89  
    persistent(true);
90  
  }
91  
  
92  
  void makeSmall {
93  
    if (!big) ret;
94  
    get(); // will cache the value
95  
    big(false);
96  
    deleteBigValue();
97  
  }
98  
  
99  
  selfType value aka setValue aka set(O o) {
100  
    if (big)
101  
      setBigValue(o);
102  
    else if (!eq(value, o)) {
103  
      setValueField(o);
104  
      change();
105  
    }
106  
    this;
107  
  }
108  
  
109  
  // Always overwrite even if eq to old value
110  
  selfType set_force(O o) {
111  
    if (big)
112  
      setBigValue(o);
113  
    else {
114  
      setValueField(o);
115  
      change();
116  
    }
117  
    this;
118  
  }
119  
  
120  
  bool notNull aka has() {
121  
    ret big ? hasBigValue() : value != null;
122  
  }
123  
  
124  
  void unload() {
125  
    if (big) setValueField(null);
126  
  }
127  
  
128  
  O setValueField(O o) {
129  
    if (autoClose && o != value)
130  
      dispose value;
131  
    ret value = o;
132  
  }
133  
  
134  
  close {
135  
    if (autoClose)
136  
      dispose value;
137  
  }
138  
  
139  
  void delete :: before { close(); }
140  
  
141  
  void changeInsideOfValue {
142  
    if (big)
143  
      value(value);
144  
    change();
145  
  }
146  
  
147  
  void clear {
148  
    set(null);
149  
  }
150  
  
151  
  selfType makePersistent() {
152  
    ret persistent(true);
153  
  }
154  
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1034711
Snippet name: G22Variable - a project-wide variable (name + any Java value, optionally persistent)
Eternal ID of this version: #1034711/41
Text MD5: f2ade3b467ea9e04adbd054fbf354912
Author: stefan
Category: javax / gazelle 22
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2023-08-07 15:35:08
Source code size: 3153 bytes / 154 lines
Pitched / IR pitched: No / No
Views / Downloads: 300 / 600
Version history: 40 change(s)
Referenced in: [show references]