2021-06-11 14:02:28 +02:00
|
|
|
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
|
|
From: vemacs <d@nkmem.es>
|
|
|
|
Date: Wed, 23 Nov 2016 12:53:43 -0500
|
|
|
|
Subject: [PATCH] Misc Utils
|
|
|
|
|
|
|
|
|
|
|
|
diff --git a/src/main/java/com/destroystokyo/paper/utils/CachedSizeConcurrentLinkedQueue.java b/src/main/java/com/destroystokyo/paper/utils/CachedSizeConcurrentLinkedQueue.java
|
|
|
|
new file mode 100644
|
2024-09-30 20:44:36 +02:00
|
|
|
index 0000000000000000000000000000000000000000..ebaa12ecacd169f00e184fed95720d047eda8b9d
|
2021-06-11 14:02:28 +02:00
|
|
|
--- /dev/null
|
|
|
|
+++ b/src/main/java/com/destroystokyo/paper/utils/CachedSizeConcurrentLinkedQueue.java
|
2024-09-30 20:44:36 +02:00
|
|
|
@@ -0,0 +1,37 @@
|
2021-06-11 14:02:28 +02:00
|
|
|
+package com.destroystokyo.paper.utils;
|
|
|
|
+
|
|
|
|
+import java.util.concurrent.ConcurrentLinkedQueue;
|
|
|
|
+import java.util.concurrent.atomic.LongAdder;
|
2024-09-30 20:44:36 +02:00
|
|
|
+import org.jetbrains.annotations.ApiStatus;
|
|
|
|
+import org.jspecify.annotations.NullMarked;
|
|
|
|
+import org.jspecify.annotations.Nullable;
|
2021-06-11 14:02:28 +02:00
|
|
|
+
|
2024-09-30 20:44:36 +02:00
|
|
|
+@NullMarked
|
|
|
|
+@ApiStatus.Internal
|
2021-06-11 14:02:28 +02:00
|
|
|
+public class CachedSizeConcurrentLinkedQueue<E> extends ConcurrentLinkedQueue<E> {
|
2024-09-30 20:44:36 +02:00
|
|
|
+
|
2021-06-11 14:02:28 +02:00
|
|
|
+ private final LongAdder cachedSize = new LongAdder();
|
|
|
|
+
|
|
|
|
+ @Override
|
2024-09-30 20:44:36 +02:00
|
|
|
+ public boolean add(final E e) {
|
|
|
|
+ final boolean result = super.add(e);
|
2021-06-11 14:02:28 +02:00
|
|
|
+ if (result) {
|
2024-09-30 20:44:36 +02:00
|
|
|
+ this.cachedSize.increment();
|
2021-06-11 14:02:28 +02:00
|
|
|
+ }
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
2024-09-30 20:44:36 +02:00
|
|
|
+ public @Nullable E poll() {
|
|
|
|
+ final E result = super.poll();
|
2021-06-11 14:02:28 +02:00
|
|
|
+ if (result != null) {
|
2024-09-30 20:44:36 +02:00
|
|
|
+ this.cachedSize.decrement();
|
2021-06-11 14:02:28 +02:00
|
|
|
+ }
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public int size() {
|
2024-09-30 20:44:36 +02:00
|
|
|
+ return this.cachedSize.intValue();
|
2021-06-11 14:02:28 +02:00
|
|
|
+ }
|
|
|
|
+}
|