Libraryless. Click here for Pure Java version (10231L/57K).
1 | // ID 0 represents a null reference |
2 | // From there we count upwards. IDs are not reused when an object is GCed |
3 | sclass EphemeralObjectIDs { |
4 | long idCounter; |
5 | new WeakValueMap<Long, O> idToObject; |
6 | Map<O, Long> objectToID = weakIdentityMap(); |
7 | |
8 | selfType runnablesReferenceQueue(RunnablesReferenceQueue queue) { |
9 | idToObject.queue(queue); |
10 | this; |
11 | } |
12 | |
13 | synchronized long remember(O o) { |
14 | if (o == null) ret 0; |
15 | try object objectToID.get(o); |
16 | long id = ++idCounter; |
17 | idToObject.put(id, o); |
18 | objectToID.put(o, id); |
19 | ret id; |
20 | } |
21 | |
22 | synchronized O get(long id) { |
23 | if (id == 0) null; |
24 | ret idToObject.get(id); |
25 | } |
26 | |
27 | synchronized bool wasGarbageCollected(long id) { |
28 | ret id > 0 && id <= idCounter && get(id) == null; |
29 | } |
30 | } |
download show line numbers debug dex old transpilations
Travelled to 3 computer(s): ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1035367 |
Snippet name: | EphemeralObjectIDs - gives objects a temporary ID to reference them (using a weak reference) |
Eternal ID of this version: | #1035367/5 |
Text MD5: | 6d1359e692c0b15d83888401622da04a |
Transpilation MD5: | d456038002644810c653d74f877bd2b0 |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-05-02 18:20:18 |
Source code size: | 786 bytes / 30 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 120 / 205 |
Version history: | 4 change(s) |
Referenced in: | [show references] |