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

20
LINES

< > BotCompany Repo | #1021582 // setWithNotifyOnAdd - returns synchronized set that calls callback on successful add() (where element wasn't there before)

JavaX fragment (include)

1  
static <A> Set<A> setWithNotifyOnAdd(final VF1<A> onAdd) {
2  
  ret setWithNotifyOnAdd(onAdd, new HashSet);
3  
}
4  
5  
static <A> Set<A> setWithNotifyOnAdd(final VF1<A> onAdd, Set<A> set) {
6  
  ret new NotifyingSet<A>(set) {
7  
    void change() {}
8  
    
9  
    public bool add(A a) {
10  
      if (!super.add(a)) false;
11  
      ret true with callF(onAdd, a);
12  
    }
13  
    
14  
    public bool addAll(Collection<? extends A> l) {
15  
      bool change = false;
16  
      for (A a : l) if (add(a)) change = true;
17  
      ret change;
18  
    }
19  
  };
20  
}

Author comment

Began life as a copy of #1020632

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, cfunsshuasjs, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1021582
Snippet name: setWithNotifyOnAdd - returns synchronized set that calls callback on successful add() (where element wasn't there before)
Eternal ID of this version: #1021582/7
Text MD5: aef1e8332b91015e13250647724e6ab4
Author: stefan
Category: javax / collections
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-02-19 11:44:30
Source code size: 520 bytes / 20 lines
Pitched / IR pitched: No / No
Views / Downloads: 254 / 266
Version history: 6 change(s)
Referenced in: [show references]