Paper/patches/api/0041-Misc-Utils.patch

50 lines
1.5 KiB
Diff
Raw Normal View History

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
index 0000000000000000000000000000000000000000..ebaa12ecacd169f00e184fed95720d047eda8b9d
2021-06-11 14:02:28 +02:00
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/utils/CachedSizeConcurrentLinkedQueue.java
@@ -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;
+import org.jetbrains.annotations.ApiStatus;
+import org.jspecify.annotations.NullMarked;
+import org.jspecify.annotations.Nullable;
2021-06-11 14:02:28 +02:00
+
+@NullMarked
+@ApiStatus.Internal
2021-06-11 14:02:28 +02:00
+public class CachedSizeConcurrentLinkedQueue<E> extends ConcurrentLinkedQueue<E> {
+
2021-06-11 14:02:28 +02:00
+ private final LongAdder cachedSize = new LongAdder();
+
+ @Override
+ public boolean add(final E e) {
+ final boolean result = super.add(e);
2021-06-11 14:02:28 +02:00
+ if (result) {
+ this.cachedSize.increment();
2021-06-11 14:02:28 +02:00
+ }
+ return result;
+ }
+
+ @Override
+ public @Nullable E poll() {
+ final E result = super.poll();
2021-06-11 14:02:28 +02:00
+ if (result != null) {
+ this.cachedSize.decrement();
2021-06-11 14:02:28 +02:00
+ }
+ return result;
+ }
+
+ @Override
+ public int size() {
+ return this.cachedSize.intValue();
2021-06-11 14:02:28 +02:00
+ }
+}