Reformat and make minor adjustments to keep everyone happy.
Change-Id: I32371650660080adfac970d9530abb04609de7f6
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
import java.util.function.Function;
abstract class KryoIOPool<T> {
+ private final ConcurrentLinkedQueue<SoftReference<T>> queue = new ConcurrentLinkedQueue<>();
- private final ConcurrentLinkedQueue<SoftReference<T>> queue = new ConcurrentLinkedQueue<>();
-
- private T borrow(final int bufferSize) {
- T element;
- SoftReference<T> reference;
- while ((reference = queue.poll()) != null) {
- if ((element = reference.get()) != null) {
- return element;
- }
+ private T borrow(final int bufferSize) {
+ T element;
+ SoftReference<T> 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> R run(final Function<T, R> function, final int bufferSize) {
- final T element = borrow(bufferSize);
- try {
- return function.apply(element);
- } finally {
- if (recycle(element)) {
- queue.offer(new SoftReference<>(element));
- }
+ <R> R run(final Function<T, R> function, final int bufferSize) {
+ final T element = borrow(bufferSize);
+ try {
+ return function.apply(element);
+ } finally {
+ if (recycle(element)) {
+ queue.offer(new SoftReference<>(element));
+ }
+ }
}
- }
}
import com.esotericsoftware.kryo.io.Input;
-class KryoInputPool extends KryoIOPool<Input> {
+final class KryoInputPool extends KryoIOPool<Input> {
+ 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
- }
}
*/
package io.atomix.utils.serializer;
-class KryoOutputPool extends KryoIOPool<ByteArrayOutput> {
+final class KryoOutputPool extends KryoIOPool<ByteArrayOutput> {
+ 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
- }
}