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

43
LINES

< > BotCompany Repo | #1001972 // PersistentLog (synchronized)

JavaX fragment (include)

// a persistent list that only grows (or is clear()ed)
// Note: don't put in static initializer (programID not set yet)
static class PersistentLog<A> extends AbstractList<A> {
  new L<A> l;
  File file;
  
  PersistentLog(S fileName) {
    this(getProgramFile(fileName));
  }
  
  PersistentLog(S progID, S fileName) {
    this(getProgramFile(progID, fileName));
  }
  
  *(File *file) {
    for (S s : scanLog(file)) pcall {
      l.add((A) unstructure(s));
    }
  }
  
  public synchronized int size() {
    ret l.size();
  }
  
  public synchronized A get(int i) {
    ret l.get(i);
  }
  
  public synchronized bool add(A a) {
    l.add(a);
    logQuoted(file, structure(a));
    ret true;
  }
  
  S fileContents() {
    ret loadTextFile(file);
  }
  
  public synchronized void clear() {
    l.clear();
    file.delete();
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1001972
Snippet name: PersistentLog (synchronized)
Eternal ID of this version: #1001972/1
Text MD5: a5c8704e7f65abed300bd0a1481e8786
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-04-28 17:47:01
Source code size: 876 bytes / 43 lines
Pitched / IR pitched: No / No
Views / Downloads: 738 / 2264
Referenced in: [show references]