diff --git a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/ArrayResettableIterator.java b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/ArrayResettableIterator.java index c0e248d9d0..9de3f6422b 100644 --- a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/ArrayResettableIterator.java +++ b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/ArrayResettableIterator.java @@ -22,9 +22,9 @@ import java.util.Collection; * Provides an Array Iterator that is able to reset, allowing you to iterate over the full array. * It achieves this though by moving end position mark to the the current cursors position, * so it round robins, even with reset. - * @param + * @param */ -public class ArrayResettableIterator implements ResettableIterator { +public class ArrayResettableIterator implements ResettableIterator { private final Object[] array; private int cursor = 0; @@ -36,7 +36,7 @@ public class ArrayResettableIterator implements ResettableIterator { reset(); } - public static ResettableIterator iterator(Collection collection) { + public static ResettableIterator iterator(Collection collection) { return new ArrayResettableIterator<>(collection.toArray()); } @@ -52,11 +52,12 @@ public class ArrayResettableIterator implements ResettableIterator { } @Override - public T next() { + public E next() { if (!hasNext) { throw new IllegalStateException(); } - @SuppressWarnings("unchecked") T result = (T) array[cursor]; + @SuppressWarnings("unchecked") + E result = (E) array[cursor]; cursor++; if (cursor == array.length) { cursor = 0; diff --git a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/ConcurrentAppendOnlyChunkedList.java b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/ConcurrentAppendOnlyChunkedList.java index 4a91fe0175..7d0702601b 100644 --- a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/ConcurrentAppendOnlyChunkedList.java +++ b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/ConcurrentAppendOnlyChunkedList.java @@ -25,15 +25,15 @@ import java.util.function.IntFunction; * This collection is a concurrent append-only list that grows in chunks.
* It's safe to be used by many threads concurrently and has a max capacity of {@link Integer#MAX_VALUE}. */ -public final class ConcurrentAppendOnlyChunkedList { +public final class ConcurrentAppendOnlyChunkedList { - private static final class AtomicChunk extends AtomicReferenceArray { + private static final class AtomicChunk extends AtomicReferenceArray { - AtomicChunk next = null; - final AtomicChunk prev; + AtomicChunk next = null; + final AtomicChunk prev; final int index; - AtomicChunk(int index, AtomicChunk prev, int length) { + AtomicChunk(int index, AtomicChunk prev, int length) { super(length); this.index = index; this.prev = prev; @@ -48,9 +48,9 @@ public final class ConcurrentAppendOnlyChunkedList { private final int chunkSizeLog2; - private AtomicChunk firstBuffer = null; + private AtomicChunk firstBuffer = null; - private AtomicChunk lastBuffer = null; + private AtomicChunk lastBuffer = null; //it is both the current index of the next element to be claimed and the current size of the collection //it's using a parity bit to mark the rotation state ie size === lastIndex >> 1 @@ -86,8 +86,8 @@ public final class ConcurrentAppendOnlyChunkedList { /** * It appends {@code elements} to the collection. */ - public void addAll(T[] elements) { - for (T e : elements) { + public void addAll(E[] elements) { + for (E e : elements) { add(e); } } @@ -95,7 +95,7 @@ public final class ConcurrentAppendOnlyChunkedList { /** * Returns the element at the specified position in this collection or {@code null} if not found. */ - public T get(int index) { + public E get(int index) { if (index < 0) { return null; } @@ -104,7 +104,7 @@ public final class ConcurrentAppendOnlyChunkedList { if (index >= lastIndex) { return null; } - final AtomicChunk buffer; + final AtomicChunk buffer; final int offset; if (index >= chunkSize) { offset = index & chunkMask; @@ -121,20 +121,20 @@ public final class ConcurrentAppendOnlyChunkedList { * Implements a lock-free version of the optimization used on {@link java.util.LinkedList#get(int)} to speed up queries * ie backward search of a node if needed. */ - private AtomicChunk getChunkOf(final int index, final long lastIndex) { + private AtomicChunk getChunkOf(final int index, final long lastIndex) { final int chunkSizeLog2 = this.chunkSizeLog2; //fast division by a power of 2 final int chunkIndex = index >> chunkSizeLog2; //size is never allowed to be > Integer.MAX_VALUE final int lastChunkIndex = (int) lastIndex >> chunkSizeLog2; int distance = chunkIndex; - AtomicChunk buffer = null; + AtomicChunk buffer = null; boolean forward = true; int distanceFromLast = lastChunkIndex - chunkIndex; //it's worth to go backward from lastChunkIndex? //trying first to check against the value we already have: if it won't worth, won't make sense to load the lastBuffer if (distanceFromLast < distance) { - final AtomicChunk lastBuffer = this.lastBuffer; + final AtomicChunk lastBuffer = this.lastBuffer; //lastBuffer is a potential moving, always increasing, target ie better to re-check the distance distanceFromLast = lastBuffer.index - chunkIndex; if (distanceFromLast < distance) { @@ -161,7 +161,7 @@ public final class ConcurrentAppendOnlyChunkedList { * * @throws NullPointerException if {@code e} is {@code null} **/ - public void add(T e) { + public void add(E e) { Objects.requireNonNull(e); while (true) { final long lastIndex = this.lastIndex; @@ -174,7 +174,7 @@ public final class ConcurrentAppendOnlyChunkedList { throw new IllegalStateException("can't add more then " + Integer.MAX_VALUE + " elements"); } //load acquire the current lastBuffer - final AtomicChunk lastBuffer = this.lastBuffer; + final AtomicChunk lastBuffer = this.lastBuffer; final int offset = (int) (validLastIndex & chunkMask); //only the first attempt to add an element to a chunk can attempt to resize if (offset == 0) { @@ -190,12 +190,12 @@ public final class ConcurrentAppendOnlyChunkedList { } } - private boolean addChunkAndElement(AtomicChunk lastBuffer, long lastIndex, long validLastIndex, T element) { + private boolean addChunkAndElement(AtomicChunk lastBuffer, long lastIndex, long validLastIndex, E element) { // adding 1 will set the lower bit if (!LAST_INDEX_UPDATER.compareAndSet(this, lastIndex, lastIndex + 1)) { return false; } - final AtomicChunk newChunk; + final AtomicChunk newChunk; try { final int index = (int) (validLastIndex >> chunkSizeLog2); newChunk = new AtomicChunk<>(index, lastBuffer, chunkSize); @@ -222,7 +222,7 @@ public final class ConcurrentAppendOnlyChunkedList { return true; } - public T[] toArray(IntFunction arrayAllocator) { + public E[] toArray(IntFunction arrayAllocator) { return toArray(arrayAllocator, 0); } @@ -231,21 +231,21 @@ public final class ConcurrentAppendOnlyChunkedList { * sequence (from first to last element).
* {@code arrayAllocator} will be used to instantiate the array of the correct size with the right runtime type. */ - public T[] toArray(IntFunction arrayAllocator, int startIndex) { + public E[] toArray(IntFunction arrayAllocator, int startIndex) { if (startIndex < 0) { throw new ArrayIndexOutOfBoundsException("startIndex must be >= 0"); } final long lastIndex = getValidLastIndex(); assert lastIndex <= Integer.MAX_VALUE; final int size = (int) lastIndex; - final T[] elements = arrayAllocator.apply(size); + final E[] elements = arrayAllocator.apply(size); if (startIndex + size > elements.length) { throw new ArrayIndexOutOfBoundsException(); } //fast division by a power of 2 final int chunkSize = this.chunkSize; final int chunks = size > chunkSize ? size >> chunkSizeLog2 : 0; - AtomicChunk buffer = firstBuffer; + AtomicChunk buffer = firstBuffer; int elementIndex = startIndex; for (int i = 0; i < chunks; i++) { drain(buffer, elements, elementIndex, chunkSize); @@ -259,16 +259,16 @@ public final class ConcurrentAppendOnlyChunkedList { } //NOTE: lastIndex is being updated BEFORE setting a new value ie on reader side need to spin until a not null value is set - private static T pollElement(AtomicChunk buffer, int i) { - T e; + private static E pollElement(AtomicChunk buffer, int i) { + E e; while ((e = buffer.get(i)) == null) { } return e; } - private static void drain(AtomicChunk buffer, T[] elements, int elementNumber, int length) { + private static void drain(AtomicChunk buffer, E[] elements, int elementNumber, int length) { for (int j = 0; j < length; j++) { - final T e = pollElement(buffer, j); + final E e = pollElement(buffer, j); assert e != null; elements[elementNumber] = e; elementNumber++; diff --git a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/IterableStream.java b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/IterableStream.java index 412a770b0a..18cb711a53 100644 --- a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/IterableStream.java +++ b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/IterableStream.java @@ -24,7 +24,7 @@ public final class IterableStream { } - public static Iterable iterableOf(Stream stream) { + public static Iterable iterableOf(Stream stream) { return stream::iterator; } diff --git a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/LinkedListImpl.java b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/LinkedListImpl.java index f9a3940db5..07932636c8 100644 --- a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/LinkedListImpl.java +++ b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/LinkedListImpl.java @@ -372,30 +372,30 @@ public class LinkedListImpl implements LinkedList { throw new IllegalStateException("Cannot find iter to remove"); } - private static final class NodeHolder extends Node { + private static final class NodeHolder extends Node { - private final T val; + private final E val; //only the head is allowed to hold a null - private NodeHolder(T e) { + private NodeHolder(E e) { val = e; } @Override - protected T val() { + protected E val() { return val; } } - public static class Node { + public static class Node { - private Node next; + private Node next; - private Node prev; + private Node prev; private int iterCount; - private static Node with(final T o) { + private static Node with(final E o) { Objects.requireNonNull(o, "Only HEAD nodes are allowed to hold null values"); if (o instanceof Node) { final Node node = (Node) o; @@ -410,15 +410,15 @@ public class LinkedListImpl implements LinkedList { } @SuppressWarnings("unchecked") - protected T val() { - return (T) this; + protected E val() { + return (E) this; } - protected final LinkedListImpl.Node next() { + protected final LinkedListImpl.Node next() { return next; } - protected final LinkedListImpl.Node prev() { + protected final LinkedListImpl.Node prev() { return prev; } diff --git a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/LongHashSet.java b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/LongHashSet.java index 201bb93ba9..f384dda9ff 100644 --- a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/LongHashSet.java +++ b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/LongHashSet.java @@ -405,14 +405,14 @@ public class LongHashSet extends AbstractSet implements Serializable { */ @SuppressWarnings("unchecked") @Override - public T[] toArray(final T[] a) { + public E[] toArray(final E[] a) { final Class componentType = a.getClass().getComponentType(); if (!componentType.isAssignableFrom(Long.class)) { throw new ArrayStoreException("cannot store Longs in array of type " + componentType); } final int size = size(); - final T[] arrayCopy = a.length >= size ? a : (T[]) Array.newInstance(componentType, size); + final E[] arrayCopy = a.length >= size ? a : (E[]) Array.newInstance(componentType, size); copyValues(arrayCopy); return arrayCopy; diff --git a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/MultiIterator.java b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/MultiIterator.java index 738420c7b0..d0ed21ac2e 100644 --- a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/MultiIterator.java +++ b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/MultiIterator.java @@ -21,11 +21,11 @@ import java.util.Iterator; /** * Provides an Iterator that works over multiple underlying iterators. * - * @param type of the class of the iterator. + * @param type of the class of the iterator. */ -public class MultiIterator extends MultiIteratorBase> { +public class MultiIterator extends MultiIteratorBase> { - public MultiIterator(Iterator[] iterators) { + public MultiIterator(Iterator[] iterators) { super(iterators); } } diff --git a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/MultiIteratorBase.java b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/MultiIteratorBase.java index ab866a1318..be1ec2a5e9 100644 --- a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/MultiIteratorBase.java +++ b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/MultiIteratorBase.java @@ -21,10 +21,10 @@ import java.util.Iterator; /** * Provides an Abstract Iterator that works over multiple underlying iterators. * - * @param type of the class of the iterator. + * @param type of the class of the iterator. * @param type of the iterator */ -abstract class MultiIteratorBase> implements Iterator { +abstract class MultiIteratorBase> implements Iterator { private final I[] iterators; private int index = -1; @@ -52,7 +52,7 @@ abstract class MultiIteratorBase> implements Iterator type of the class of the iterator. + * @param type of the class of the iterator. */ -public class MultiResettableIterator extends MultiIteratorBase> implements ResettableIterator { +public class MultiResettableIterator extends MultiIteratorBase> implements ResettableIterator { - public MultiResettableIterator(ResettableIterator[] iterators) { + public MultiResettableIterator(ResettableIterator[] iterators) { super(iterators); } diff --git a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/PriorityCollection.java b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/PriorityCollection.java index 265b27ad8a..0e74cc937b 100644 --- a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/PriorityCollection.java +++ b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/PriorityCollection.java @@ -38,30 +38,30 @@ import java.util.stream.Collectors; * * Methods getArray, setArray MUST never be exposed, and all array modifications must go through these. * - * @param The type this class may hold, this is generic as can be anything that extends PriorityAware. + * @param The type this class may hold, this is generic as can be anything that extends PriorityAware. */ -public class PriorityCollection extends AbstractCollection { +public class PriorityCollection extends AbstractCollection { - private final Supplier> supplier; - private volatile PriorityHolder[] priorityHolders = newPrioritySetArrayInstance(0); + private final Supplier> supplier; + private volatile PriorityHolder[] priorityHolders = newPrioritySetArrayInstance(0); private volatile int size; - private void setArray(PriorityHolder[] priorityHolders) { + private void setArray(PriorityHolder[] priorityHolders) { this.priorityHolders = priorityHolders; } - private PriorityHolder[] getArray() { + private PriorityHolder[] getArray() { return priorityHolders; } - public PriorityCollection(Supplier> supplier) { + public PriorityCollection(Supplier> supplier) { this.supplier = supplier; } @SuppressWarnings("unchecked") - private static PriorityHolder[] newPrioritySetArrayInstance(int length) { - return (PriorityHolder[]) Array.newInstance(PriorityHolder.class, length); + private static PriorityHolder[] newPrioritySetArrayInstance(int length) { + return (PriorityHolder[]) Array.newInstance(PriorityHolder.class, length); } @Override @@ -75,20 +75,20 @@ public class PriorityCollection extends AbstractCollect } public Set getPriorites() { - PriorityHolder[] snapshot = getArray(); + PriorityHolder[] snapshot = getArray(); return Arrays.stream(snapshot).map(PriorityAware::getPriority).collect(Collectors.toSet()); } @Override - public Iterator iterator() { - Iterator[] iterators = getIterators(); + public Iterator iterator() { + Iterator[] iterators = getIterators(); return new MultiIterator<>(iterators); } - private Iterator[] getIterators() { - PriorityHolder[] snapshot = this.getArray(); + private Iterator[] getIterators() { + PriorityHolder[] snapshot = this.getArray(); int size = snapshot.length; - Iterator[] iterators = newIteratorArrayInstance(size); + Iterator[] iterators = newIteratorArrayInstance(size); for (int i = 0; i < size; i++) { iterators[i] = snapshot[i].getValues().iterator(); } @@ -96,18 +96,18 @@ public class PriorityCollection extends AbstractCollect } @SuppressWarnings("unchecked") - private static Iterator[] newIteratorArrayInstance(int length) { - return (Iterator[]) Array.newInstance(Iterator.class, length); + private static Iterator[] newIteratorArrayInstance(int length) { + return (Iterator[]) Array.newInstance(Iterator.class, length); } - public ResettableIterator resettableIterator() { - return new MultiResettableIterator(getResettableIterators()); + public ResettableIterator resettableIterator() { + return new MultiResettableIterator(getResettableIterators()); } - private ResettableIterator[] getResettableIterators() { - PriorityHolder[] snapshot = this.getArray(); + private ResettableIterator[] getResettableIterators() { + PriorityHolder[] snapshot = this.getArray(); int size = snapshot.length; - ResettableIterator[] iterators = newResettableIteratorArrayInstance(size); + ResettableIterator[] iterators = newResettableIteratorArrayInstance(size); for (int i = 0; i < size; i++) { iterators[i] = ArrayResettableIterator.iterator(snapshot[i].getValues()); } @@ -115,28 +115,28 @@ public class PriorityCollection extends AbstractCollect } @SuppressWarnings("unchecked") - private static ResettableIterator[] newResettableIteratorArrayInstance(int length) { - return (ResettableIterator[]) Array.newInstance(ResettableIterator.class, length); + private static ResettableIterator[] newResettableIteratorArrayInstance(int length) { + return (ResettableIterator[]) Array.newInstance(ResettableIterator.class, length); } @Override - public void forEach(Consumer action) { + public void forEach(Consumer action) { Objects.requireNonNull(action); - PriorityHolder[] current = getArray(); + PriorityHolder[] current = getArray(); int len = current.length; for (int i = 0; i < len; ++i) { current[i].getValues().forEach(action); } } - private Collection getCollection(int priority, boolean createIfMissing) { - PriorityHolder[] current = getArray(); + private Collection getCollection(int priority, boolean createIfMissing) { + PriorityHolder[] current = getArray(); int low = 0; int high = current.length - 1; while (low <= high) { int mid = (low + high) >>> 1; - PriorityHolder midVal = current[mid]; + PriorityHolder midVal = current[mid]; if (midVal.getPriority() > priority) low = mid + 1; @@ -147,14 +147,14 @@ public class PriorityCollection extends AbstractCollect } if (createIfMissing) { - PriorityHolder[] newArray = newPrioritySetArrayInstance(current.length + 1); + PriorityHolder[] newArray = newPrioritySetArrayInstance(current.length + 1); if (low > 0) { System.arraycopy(current, 0, newArray, 0, low); } if (current.length - low > 0) { System.arraycopy(current, low, newArray, low + 1, current.length - low); } - newArray[low] = new PriorityHolder(priority, supplier); + newArray[low] = new PriorityHolder(priority, supplier); setArray(newArray); return newArray[low].getValues(); } @@ -162,17 +162,17 @@ public class PriorityCollection extends AbstractCollect } @Override - public synchronized boolean add(T t) { + public synchronized boolean add(E e) { if (size() == Integer.MAX_VALUE) return false; - boolean result = addInternal(t); + boolean result = addInternal(e); calcSize(); return result; } - private boolean addInternal(T t) { - if (t == null) return false; - Collection priority = getCollection(t.getPriority(), true); - return priority.add(t); + private boolean addInternal(E e) { + if (e == null) return false; + Collection priority = getCollection(e.getPriority(), true); + return priority.add(e); } @Override @@ -185,7 +185,7 @@ public class PriorityCollection extends AbstractCollect private boolean removeInternal(Object o) { if (o instanceof PriorityAware) { PriorityAware priorityAware = (PriorityAware) o; - Collection priority = getCollection(priorityAware.getPriority(), false); + Collection priority = getCollection(priorityAware.getPriority(), false); boolean result = priority != null && priority.remove(priorityAware); if (priority != null && priority.isEmpty()) { removeCollection(priorityAware.getPriority()); @@ -196,22 +196,22 @@ public class PriorityCollection extends AbstractCollect } } - private Collection removeCollection(int priority) { - PriorityHolder[] current = getArray(); + private Collection removeCollection(int priority) { + PriorityHolder[] current = getArray(); int len = current.length; int low = 0; int high = len - 1; while (low <= high) { int mid = (low + high) >>> 1; - PriorityHolder midVal = current[mid]; + PriorityHolder midVal = current[mid]; if (midVal.getPriority() > priority) low = mid + 1; else if (midVal.getPriority() < priority) high = mid - 1; else { - PriorityHolder[] newArray = newPrioritySetArrayInstance(len - 1); + PriorityHolder[] newArray = newPrioritySetArrayInstance(len - 1); System.arraycopy(current, 0, newArray, 0, mid); System.arraycopy(current, mid + 1, newArray, mid, len - mid - 1); setArray(newArray); @@ -231,11 +231,11 @@ public class PriorityCollection extends AbstractCollect } @Override - public synchronized boolean addAll(Collection c) { + public synchronized boolean addAll(Collection c) { Objects.requireNonNull(c); if (size() >= Integer.MAX_VALUE - c.size()) return false; boolean modified = false; - for (T e : c) + for (E e : c) if (addInternal(e)) modified = true; calcSize(); @@ -259,8 +259,8 @@ public class PriorityCollection extends AbstractCollect public synchronized boolean retainAll(Collection c) { Objects.requireNonNull(c); boolean modified = false; - PriorityHolder[] snapshot = getArray(); - for (PriorityHolder priorityHolder : snapshot) { + PriorityHolder[] snapshot = getArray(); + for (PriorityHolder priorityHolder : snapshot) { if (priorityHolder.getValues().retainAll(c)) { modified = true; if (priorityHolder.getValues().isEmpty()) { @@ -274,8 +274,8 @@ public class PriorityCollection extends AbstractCollect @Override public synchronized void clear() { - PriorityHolder[] snapshot = getArray(); - for (PriorityHolder priorityHolder : snapshot) { + PriorityHolder[] snapshot = getArray(); + for (PriorityHolder priorityHolder : snapshot) { priorityHolder.getValues().clear(); } calcSize(); @@ -288,14 +288,14 @@ public class PriorityCollection extends AbstractCollect public boolean contains(PriorityAware priorityAware) { if (priorityAware == null) return false; - Collection prioritySet = getCollection(priorityAware.getPriority(), false); + Collection prioritySet = getCollection(priorityAware.getPriority(), false); return prioritySet != null && prioritySet.contains(priorityAware); } private void calcSize() { - PriorityHolder[] current = getArray(); + PriorityHolder[] current = getArray(); int size = 0; - for (PriorityHolder priorityHolder : current) { + for (PriorityHolder priorityHolder : current) { size += priorityHolder.getValues().size(); } this.size = size; diff --git a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/PriorityLinkedList.java b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/PriorityLinkedList.java index 92e615bf9c..c69fc5cca7 100644 --- a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/PriorityLinkedList.java +++ b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/PriorityLinkedList.java @@ -23,15 +23,15 @@ import java.util.function.ToLongFunction; * and allows adding and removing of elements at both ends, and peeking.
* Only {@link #size()} and {@link #isEmpty()} are safe to be called concurrently. */ -public interface PriorityLinkedList { +public interface PriorityLinkedList { - void addHead(T t, int priority); + void addHead(E e, int priority); - void addTail(T t, int priority); + void addTail(E e, int priority); - void addSorted(T t, int priority); + void addSorted(E e, int priority); - T poll(); + E poll(); void clear(); @@ -39,9 +39,9 @@ public interface PriorityLinkedList { * @see LinkedList#setIDSupplier(ToLongFunction) * @param supplier */ - void setIDSupplier(ToLongFunction supplier); + void setIDSupplier(ToLongFunction supplier); - T removeWithID(long id); + E removeWithID(long id); /** * Returns the size of this list.
@@ -49,7 +49,7 @@ public interface PriorityLinkedList { */ int size(); - LinkedListIterator iterator(); + LinkedListIterator iterator(); /** * Returns {@code true} if empty, {@code false} otherwise.
diff --git a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/PriorityLinkedListImpl.java b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/PriorityLinkedListImpl.java index cde928d467..8b879afd18 100644 --- a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/PriorityLinkedListImpl.java +++ b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/PriorityLinkedListImpl.java @@ -27,11 +27,11 @@ import java.util.function.ToLongFunction; *

* It implements this by maintaining an individual LinkedBlockingDeque for each priority level. */ -public class PriorityLinkedListImpl implements PriorityLinkedList { +public class PriorityLinkedListImpl implements PriorityLinkedList { private static final AtomicIntegerFieldUpdater SIZE_UPDATER = AtomicIntegerFieldUpdater.newUpdater(PriorityLinkedListImpl.class, "size"); - protected LinkedListImpl[] levels; + protected LinkedListImpl[] levels; private volatile int size; @@ -46,8 +46,8 @@ public class PriorityLinkedListImpl implements PriorityLinkedList { } - public PriorityLinkedListImpl(final int priorities, Comparator comparator) { - levels = (LinkedListImpl[]) Array.newInstance(LinkedListImpl.class, priorities); + public PriorityLinkedListImpl(final int priorities, Comparator comparator) { + levels = (LinkedListImpl[]) Array.newInstance(LinkedListImpl.class, priorities); for (int i = 0; i < priorities; i++) { levels[i] = new LinkedListImpl<>(comparator); @@ -70,45 +70,45 @@ public class PriorityLinkedListImpl implements PriorityLinkedList { } @Override - public void addHead(final T t, final int priority) { + public void addHead(final E e, final int priority) { checkHighest(priority); - levels[priority].addHead(t); + levels[priority].addHead(e); exclusiveIncrementSize(1); } @Override - public void addTail(final T t, final int priority) { + public void addTail(final E e, final int priority) { checkHighest(priority); - levels[priority].addTail(t); + levels[priority].addTail(e); exclusiveIncrementSize(1); } @Override - public void addSorted(T t, int priority) { + public void addSorted(E e, int priority) { checkHighest(priority); - levels[priority].addSorted(t); + levels[priority].addSorted(e); exclusiveIncrementSize(1); } @Override - public void setIDSupplier(ToLongFunction supplier) { - for (LinkedList list : levels) { + public void setIDSupplier(ToLongFunction supplier) { + for (LinkedList list : levels) { list.setIDSupplier(supplier); } } @Override - public T removeWithID(long id) { + public E removeWithID(long id) { // we start at 4 just as an optimization, since most times we only use level 4 as the level on messages if (levels.length > 4) { for (int l = 4; l < levels.length; l++) { - T removed = levels[l].removeWithID(id); + E removed = levels[l].removeWithID(id); if (removed != null) { exclusiveIncrementSize(-1); return removed; @@ -117,7 +117,7 @@ public class PriorityLinkedListImpl implements PriorityLinkedList { } for (int l = Math.min(3, levels.length); l >= 0; l--) { - T removed = levels[l].removeWithID(id); + E removed = levels[l].removeWithID(id); if (removed != null) { exclusiveIncrementSize(-1); return removed; @@ -129,8 +129,8 @@ public class PriorityLinkedListImpl implements PriorityLinkedList { @Override - public T poll() { - T t = null; + public E poll() { + E e = null; // We are just using a simple prioritization algorithm: // Highest priority refs always get returned first. @@ -139,12 +139,12 @@ public class PriorityLinkedListImpl implements PriorityLinkedList { // TODO - A better prioritization algorithm for (int i = highestPriority; i >= 0; i--) { - LinkedListImpl ll = levels[i]; + LinkedListImpl ll = levels[i]; if (ll.size() != 0) { - t = ll.poll(); + e = ll.poll(); - if (t != null) { + if (e != null) { exclusiveIncrementSize(-1); if (ll.size() == 0) { @@ -158,12 +158,12 @@ public class PriorityLinkedListImpl implements PriorityLinkedList { } } - return t; + return e; } @Override public void clear() { - for (LinkedListImpl list : levels) { + for (LinkedListImpl list : levels) { list.clear(); } @@ -189,17 +189,17 @@ public class PriorityLinkedListImpl implements PriorityLinkedList { } @Override - public LinkedListIterator iterator() { + public LinkedListIterator iterator() { return new PriorityLinkedListIterator(); } - private class PriorityLinkedListIterator implements LinkedListIterator { + private class PriorityLinkedListIterator implements LinkedListIterator { private int index; - private final LinkedListIterator[] cachedIters = new LinkedListIterator[levels.length]; + private final LinkedListIterator[] cachedIters = new LinkedListIterator[levels.length]; - private LinkedListIterator lastIter; + private LinkedListIterator lastIter; private int resetCount = lastReset; @@ -229,7 +229,7 @@ public class PriorityLinkedListImpl implements PriorityLinkedList { closed = true; lastIter = null; - for (LinkedListIterator iter : cachedIters) { + for (LinkedListIterator iter : cachedIters) { if (iter != null) { iter.close(); } @@ -274,7 +274,7 @@ public class PriorityLinkedListImpl implements PriorityLinkedList { } @Override - public T next() { + public E next() { if (lastIter == null) { throw new NoSuchElementException(); } diff --git a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/SparseArrayLinkedList.java b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/SparseArrayLinkedList.java index e77fcbe3c8..ab8bb734ba 100644 --- a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/SparseArrayLinkedList.java +++ b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/SparseArrayLinkedList.java @@ -34,12 +34,12 @@ import java.util.function.Predicate; * From the memory footprint's point of view, this list won't remove the last remaining array although empty to optimize * the case where its capacity would be enough to hold incoming elements, hence saving a new array allocation. */ -public final class SparseArrayLinkedList { +public final class SparseArrayLinkedList { // the whole chunk fit into 1 or 2 cache lines depending if JVM COOPS are used private static final int SPARSE_ARRAY_DEFAULT_CAPACITY = 16; - private static final class SparseArray { + private static final class SparseArray { private final Object[] elements; private int size; @@ -52,18 +52,18 @@ public final class SparseArrayLinkedList { tail = 0; } - private boolean add(T e) { + private boolean add(E e) { final int capacity = elements.length; if (tail == capacity) { return false; } - elements[tail] = (T) e; + elements[tail] = (E) e; tail++; size++; return true; } - private int remove(Predicate filter) { + private int remove(Predicate filter) { if (size == 0) { // this shouldn't happen: the chunk should be removed if empty return 0; @@ -75,7 +75,7 @@ public final class SparseArrayLinkedList { int visited = 0; final int originalSize = size; for (int i = 0, capacity = elements.length; i < capacity; i++) { - final T e = (T) elements[i]; + final E e = (E) elements[i]; if (e != null) { if (filter.test(e)) { elements[i] = null; @@ -100,7 +100,7 @@ public final class SparseArrayLinkedList { return removed; } - public int clear(Consumer consumer) { + public int clear(Consumer consumer) { final int originalSize = size; if (originalSize == 0) { return 0; @@ -108,7 +108,7 @@ public final class SparseArrayLinkedList { int visited = 0; final Object[] elements = this.elements; for (int i = 0, capacity = elements.length; i < capacity; i++) { - final T e = (T) elements[i]; + final E e = (E) elements[i]; if (e != null) { if (consumer != null) { consumer.accept(e); @@ -131,14 +131,14 @@ public final class SparseArrayLinkedList { } } - public static long removeFromSparseArrayList(List> sparseArrayList, Predicate filter) { + public static long removeFromSparseArrayList(List> sparseArrayList, Predicate filter) { if (filter == null) { return 0; } long removed = 0; - Iterator> iter = sparseArrayList.iterator(); + Iterator> iter = sparseArrayList.iterator(); while (iter.hasNext()) { - final SparseArray sparseArray = iter.next(); + final SparseArray sparseArray = iter.next(); final int justRemoved = sparseArray.remove(filter); removed += justRemoved; if (justRemoved > 0) { @@ -153,23 +153,23 @@ public final class SparseArrayLinkedList { return removed; } - public static void addToSparseArrayList(List> sparseArrayList, T e, int sparseArrayCapacity) { + public static void addToSparseArrayList(List> sparseArrayList, E e, int sparseArrayCapacity) { final int size = sparseArrayList.size(); // LinkedList::get(size-1) is fast as LinkedList::getLast if (size == 0 || !sparseArrayList.get(size - 1).add(e)) { - final SparseArray sparseArray = new SparseArray<>(sparseArrayCapacity); + final SparseArray sparseArray = new SparseArray<>(sparseArrayCapacity); sparseArray.add(e); sparseArrayList.add(sparseArray); } } - public static long clearSparseArrayList(List> sparseArrayList, Consumer consumer) { + public static long clearSparseArrayList(List> sparseArrayList, Consumer consumer) { final int size = sparseArrayList.size(); long count = 0; if (size > 0) { for (int i = 0; i < size - 1; i++) { // LinkedList::remove(0) is fast as LinkedList::getFirst - final SparseArray removed = sparseArrayList.remove(0); + final SparseArray removed = sparseArrayList.remove(0); count += removed.clear(consumer); } // LinkedList::get(0) is fast as LinkedList::getFirst @@ -178,7 +178,7 @@ public final class SparseArrayLinkedList { return count; } - private final LinkedList> list; + private final LinkedList> list; private final int sparseArrayCapacity; private long size; @@ -198,7 +198,7 @@ public final class SparseArrayLinkedList { /** * Appends {@code e} to the end of this list. */ - public void add(T e) { + public void add(E e) { Objects.requireNonNull(e, "e cannot be null"); addToSparseArrayList(list, e, sparseArrayCapacity); size++; @@ -207,7 +207,7 @@ public final class SparseArrayLinkedList { /** * Removes any element of the list matching the given predicate. */ - public long remove(Predicate filter) { + public long remove(Predicate filter) { if (size == 0) { return 0; } @@ -220,7 +220,7 @@ public final class SparseArrayLinkedList { /** * Clear while consuming (using the given {@code consumer} all the elements of this list. */ - public long clear(Consumer consumer) { + public long clear(Consumer consumer) { if (size == 0) { return 0; }