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

71
LINES

< > BotCompany Repo | #1032095 // WeakCompactLinkedHashSet [should work]

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

Libraryless. Click here for Pure Java version (3800L/21K).

1  
sclass WeakCompactLinkedHashSet<A> extends AbstractSet<A> {
2  
  new CompactLinkedHashSet<MyWeakReference<A>> set;
3  
  new ReferenceQueue<A> queue;
4  
  
5  
  sclass MyWeakReference<A> extends WeakReference<A> {
6  
    int hash;
7  
    
8  
    *(A o) {
9  
      super(o);
10  
      hash = o.hashCode();
11  
    }
12  
    
13  
    *(A o, ReferenceQueue<A> queue) {
14  
      super(o, queue);
15  
      hash = o.hashCode();
16  
    }
17  
    
18  
    public int hashCode() { ret hash; }
19  
    public bool equals(O o) {
20  
      ret o instanceof MyWeakReference
21  
        && eq(get(), ((MyWeakReference) o).get());
22  
    }
23  
  }
24  
  
25  
  // Make sure we get reaped once a minute (or whatever the policy is)
26  
  *() {
27  
    _registerWeakCollection(this);
28  
  }
29  
  
30  
  void reap() {
31  
    MyWeakReference ref;
32  
    while ((ref = (MyWeakReference) queue.poll()) != null)
33  
      set.removeIfSame(ref);
34  
  }
35  
  
36  
  public int size() { reap(); ret set.size(); }
37  
  
38  
  public Iterator<A> iterator() {
39  
    final Iterator<MyWeakReference<A>> it = set.iterator();
40  
    ret iteratorFromFunction(() -> {
41  
      while (it.hasNext()) {
42  
        MyWeakReference<A> ref = it.next();
43  
        A a = ref.get();
44  
        if (a != null) ret a;
45  
        // todo: clean up entry
46  
      }
47  
      null;
48  
    });
49  
  }
50  
  
51  
  public bool contains(O o) {
52  
    reap();
53  
    ret set.contains(new MyWeakReference(o));
54  
  }
55  
  
56  
  public bool add(A a) {
57  
    reap();
58  
    ret set.add(new MyWeakReference(a, queue));
59  
  }
60  
  
61  
  public bool remove(O o) {
62  
    reap();
63  
    ret set.remove(new MyWeakReference(o));
64  
  }
65  
  
66  
  A find(O o) {
67  
    reap();
68  
    MyWeakReference<A> ref = set.find(new MyWeakReference(o));
69  
    ret ref == null ? null : ref.get();
70  
  }
71  
}

Author comment

Began life as a copy of #1012721

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx

No comments. add comment

Snippet ID: #1032095
Snippet name: WeakCompactLinkedHashSet [should work]
Eternal ID of this version: #1032095/3
Text MD5: ccb6625bcb0aec45681d49eb49620f33
Transpilation MD5: 42ced2d54936f16de10d76ce420286b5
Author: stefan
Category: javax / collections
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-08-11 14:42:16
Source code size: 1677 bytes / 71 lines
Pitched / IR pitched: No / No
Views / Downloads: 84 / 201
Version history: 2 change(s)
Referenced in: [show references]