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

28
LINES

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

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

Libraryless. Click here for Pure Java version (2802L/16K).

sclass CloseablesHolder implements AutoCloseable, IResourceHolder {
  new LinkedHashSet<AutoCloseable> closeables;
  
  public <A extends AutoCloseable> A add(A a) {
    if (a != null)
      closeables.add(a);
    ret a;
  }
  
  // remove from list, don't close
  <A extends AutoCloseable> A remove aka takeOut(A a) {
    closeables.remove(a);
    ret a;
  }
  
  // close and remove
  <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(cloneAndClear(closeables)))
      main close(c);
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 6 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, xrpafgyirdlv

No comments. add comment

Snippet ID: #1029397
Snippet name: CloseablesHolder - temporarily hold resources
Eternal ID of this version: #1029397/10
Text MD5: e4f80339c1e13ead44f575e9895644c1
Transpilation MD5: 9ffb96e1465ee8f16af6376f5b05ec1e
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-05-01 15:17:31
Source code size: 656 bytes / 28 lines
Pitched / IR pitched: No / No
Views / Downloads: 78 / 208
Version history: 9 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1029397 & http://1029397.tinybrain.de