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 (4554L/26K).

sclass CloseablesHolder extends Meta implements 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;
  }
  
  public void 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/17
Text MD5: 7e706a29f343bd3f59e58c93ef92d1b4
Transpilation MD5: 56c02003ff5c5492a7e2aaaa072c3bde
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-10-11 12:20:58
Source code size: 1052 bytes / 41 lines
Pitched / IR pitched: No / No
Views / Downloads: 141 / 405
Version history: 16 change(s)
Referenced in: [show references]