From dd89a87282535d48c69a5e91191bd4aa669b0c79 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Thu, 9 May 2024 00:08:32 +0200 Subject: [PATCH] Fix checkstyle in Kryo{IO,Input,Output}Pool Reformat and make minor adjustments to keep everyone happy. Change-Id: I32371650660080adfac970d9530abb04609de7f6 Signed-off-by: Robert Varga --- .../atomix/utils/serializer/KryoIOPool.java | 43 +++++++++---------- .../utils/serializer/KryoInputPool.java | 28 ++++++------ .../utils/serializer/KryoOutputPool.java | 33 +++++++------- 3 files changed, 52 insertions(+), 52 deletions(-) diff --git a/atomix-storage/src/main/java/io/atomix/utils/serializer/KryoIOPool.java b/atomix-storage/src/main/java/io/atomix/utils/serializer/KryoIOPool.java index 6324631d6c..acd33e5194 100644 --- a/atomix-storage/src/main/java/io/atomix/utils/serializer/KryoIOPool.java +++ b/atomix-storage/src/main/java/io/atomix/utils/serializer/KryoIOPool.java @@ -20,32 +20,31 @@ import java.util.concurrent.ConcurrentLinkedQueue; import java.util.function.Function; abstract class KryoIOPool { + private final ConcurrentLinkedQueue> queue = new ConcurrentLinkedQueue<>(); - private final ConcurrentLinkedQueue> queue = new ConcurrentLinkedQueue<>(); - - private T borrow(final int bufferSize) { - T element; - SoftReference reference; - while ((reference = queue.poll()) != null) { - if ((element = reference.get()) != null) { - return element; - } + private T borrow(final int bufferSize) { + T element; + SoftReference reference; + while ((reference = queue.poll()) != null) { + if ((element = reference.get()) != null) { + return element; + } + } + return create(bufferSize); } - return create(bufferSize); - } - protected abstract T create(final int bufferSize); + abstract T create(int bufferSize); - protected abstract boolean recycle(final T element); + abstract boolean recycle(T element); - R run(final Function function, final int bufferSize) { - final T element = borrow(bufferSize); - try { - return function.apply(element); - } finally { - if (recycle(element)) { - queue.offer(new SoftReference<>(element)); - } + R run(final Function function, final int bufferSize) { + final T element = borrow(bufferSize); + try { + return function.apply(element); + } finally { + if (recycle(element)) { + queue.offer(new SoftReference<>(element)); + } + } } - } } diff --git a/atomix-storage/src/main/java/io/atomix/utils/serializer/KryoInputPool.java b/atomix-storage/src/main/java/io/atomix/utils/serializer/KryoInputPool.java index 0eeb8dfc89..4f5ac46f77 100644 --- a/atomix-storage/src/main/java/io/atomix/utils/serializer/KryoInputPool.java +++ b/atomix-storage/src/main/java/io/atomix/utils/serializer/KryoInputPool.java @@ -17,21 +17,21 @@ package io.atomix.utils.serializer; import com.esotericsoftware.kryo.io.Input; -class KryoInputPool extends KryoIOPool { +final class KryoInputPool extends KryoIOPool { + static final int MAX_POOLED_BUFFER_SIZE = 512 * 1024; - static final int MAX_POOLED_BUFFER_SIZE = 512 * 1024; - - @Override - protected Input create(int bufferSize) { - return new Input(bufferSize); - } + @Override + Input create(final int bufferSize) { + return new Input(bufferSize); + } - @Override - protected boolean recycle(Input input) { - if (input.getBuffer().length < MAX_POOLED_BUFFER_SIZE) { - input.setInputStream(null); - return true; + @Override + boolean recycle(final Input input) { + if (input.getBuffer().length >= MAX_POOLED_BUFFER_SIZE) { + // discard + return false; + } + input.setInputStream(null); + return true; } - return false; // discard - } } diff --git a/atomix-storage/src/main/java/io/atomix/utils/serializer/KryoOutputPool.java b/atomix-storage/src/main/java/io/atomix/utils/serializer/KryoOutputPool.java index 6b1737fd12..e77316c932 100644 --- a/atomix-storage/src/main/java/io/atomix/utils/serializer/KryoOutputPool.java +++ b/atomix-storage/src/main/java/io/atomix/utils/serializer/KryoOutputPool.java @@ -15,23 +15,24 @@ */ package io.atomix.utils.serializer; -class KryoOutputPool extends KryoIOPool { +final class KryoOutputPool extends KryoIOPool { + private static final int MAX_BUFFER_SIZE = 768 * 1024; + static final int MAX_POOLED_BUFFER_SIZE = 512 * 1024; - private static final int MAX_BUFFER_SIZE = 768 * 1024; - static final int MAX_POOLED_BUFFER_SIZE = 512 * 1024; - - @Override - protected ByteArrayOutput create(int bufferSize) { - return new ByteArrayOutput(bufferSize, MAX_BUFFER_SIZE, new BufferAwareByteArrayOutputStream(bufferSize)); - } + @Override + ByteArrayOutput create(final int bufferSize) { + return new ByteArrayOutput(bufferSize, MAX_BUFFER_SIZE, new BufferAwareByteArrayOutputStream(bufferSize)); + } - @Override - protected boolean recycle(ByteArrayOutput output) { - if (output.getByteArrayOutputStream().getBufferSize() < MAX_POOLED_BUFFER_SIZE) { - output.getByteArrayOutputStream().reset(); - output.clear(); - return true; + @Override + boolean recycle(final ByteArrayOutput output) { + final var baos = output.getByteArrayOutputStream(); + if (baos.getBufferSize() >= MAX_POOLED_BUFFER_SIZE) { + // discard + return false; + } + baos.reset(); + output.clear(); + return true; } - return false; // discard - } } -- 2.36.6