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

41
LINES

< > BotCompany Repo | #1029397 // CloseablesHolder - temporarily hold resources

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

Libraryless. Click here for Pure Java version (9091L/50K).

sclass CloseablesHolder > Meta is AutoCloseable, IResourceHolder {
  new LinkedHashSet<AutoCloseable> closeables;
  WeakRef<Thread> createdInThread = weakRef(currentThread());
  
  public synchronized <A extends AutoCloseable> void addAll(Iterable<A> l) {
    fOr (var a : l) add(a);
  }
  
  public synchronized <A extends AutoCloseable> A add(A a) {
    if (a != null)
      closeables.add(a);
    ret a;
  }
  
  // remove from list, don't close
  synchronized <A extends AutoCloseable> A remove aka takeOut(A a) {
    closeables.remove(a);
    ret a;
  }
  
  // close and remove
  synchronized <A extends AutoCloseable> A close(A a) {
    if (a == null) ret a;
    closeables.remove(a);
    main close(a);
    ret a;
  }
  
  close {
    for (AutoCloseable c : reversed(takeAll()))
      main close(c);
  }
  
  public synchronized void forget aka clear() {
    closeables.clear();
  }
  
  public synchronized Cl<AutoCloseable> takeAll() {
    ret cloneAndClear(closeables);
  }
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1029397
Snippet name: CloseablesHolder - temporarily hold resources
Eternal ID of this version: #1029397/18
Text MD5: 10a3c6ae9d8cfe46bf97d5e2c480302f
Transpilation MD5: 7da750866ee3fc42e0db0605d4c56081
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-05-08 23:16:57
Source code size: 1026 bytes / 41 lines
Pitched / IR pitched: No / No
Views / Downloads: 349 / 716
Version history: 17 change(s)
Referenced in: [show references]