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 --- /dev/null +++ b/src/main/java/com/destroystokyo/paper/utils/CachedSizeConcurrentLinkedQueue.java @@ -0,0 +1,37 @@ +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; + +@NullMarked +@ApiStatus.Internal +public class CachedSizeConcurrentLinkedQueue<E> extends ConcurrentLinkedQueue<E> { + + private final LongAdder cachedSize = new LongAdder(); + + @Override + public boolean add(final E e) { + final boolean result = super.add(e); + if (result) { + this.cachedSize.increment(); + } + return result; + } + + @Override + public @Nullable E poll() { + final E result = super.poll(); + if (result != null) { + this.cachedSize.decrement(); + } + return result; + } + + @Override + public int size() { + return this.cachedSize.intValue(); + } +}