Libraryless. Click here for Pure Java version (2090L/13K).
1 | static <A> void addToListWithMaxSize(L<A> l, A a, int maxSize) { |
2 | if (l == null || maxSize <= 0) ret; |
3 | synchronized(collectionMutex(l)) { |
4 | int n; |
5 | while ((n = l.size()) >= maxSize) { |
6 | l.remove(0); |
7 | if (l.size() != n-1) |
8 | fail("List remove failed!? " + l.size() + " / " + n + " / " + objectToStringWithClassName(l)); |
9 | } |
10 | l.add(a); |
11 | } |
12 | } |
download show line numbers debug dex old transpilations
Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1015974 |
Snippet name: | addToListWithMaxSize - drops elements from start if list is too large; note: should be a LinkedList or CircularArrayList |
Eternal ID of this version: | #1015974/8 |
Text MD5: | 568c01d67cdf345420014374a7be0e7f |
Transpilation MD5: | 8da8a72403c1d8c1ac091cc8679d062f |
Author: | stefan |
Category: | javax / collections |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2019-09-05 13:17:48 |
Source code size: | 376 bytes / 12 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 483 / 597 |
Version history: | 7 change(s) |
Referenced in: | [show references] |