This is an automated email from the ASF dual-hosted git repository.
kturner pushed a commit to branch 2.1
in repository https://gitbox.apache.org/repos/asf/accumulo.git
The following commit(s) were added to refs/heads/2.1 by this push:
new bd6b760dd6 avoids loop and wrapping iter in PreAllocatedArray (#6082)
bd6b760dd6 is described below
commit bd6b760dd6e7c24d08a9346d238bc2a917c73213
Author: Keith Turner <[email protected]>
AuthorDate: Wed Jan 28 12:44:40 2026 -0800
avoids loop and wrapping iter in PreAllocatedArray (#6082)
---
.../accumulo/core/util/PreAllocatedArray.java | 23 ++++++++++------------
.../accumulo/core/util/PreAllocatedArrayTest.java | 19 +++++++++++++++++-
2 files changed, 28 insertions(+), 14 deletions(-)
diff --git
a/core/src/main/java/org/apache/accumulo/core/util/PreAllocatedArray.java
b/core/src/main/java/org/apache/accumulo/core/util/PreAllocatedArray.java
index 9690750165..679b06f9ad 100644
--- a/core/src/main/java/org/apache/accumulo/core/util/PreAllocatedArray.java
+++ b/core/src/main/java/org/apache/accumulo/core/util/PreAllocatedArray.java
@@ -18,30 +18,27 @@
*/
package org.apache.accumulo.core.util;
-import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Iterator;
-
-import com.google.common.collect.Iterators;
+import java.util.List;
/**
- * An {@link ArrayList} implementation that represents a type-safe
pre-allocated array. This should
- * be used exactly like an array, but helps avoid type-safety issues when
mixing arrays with
- * generics. The iterator is unmodifiable.
+ * A {@link List} implementation that represents a type-safe pre-allocated
array. This should be
+ * used exactly like an array, but helps avoid type-safety issues when mixing
arrays with generics.
+ * The iterator is unmodifiable.
*/
-public class PreAllocatedArray<T> implements Iterable<T> {
+public final class PreAllocatedArray<T> implements Iterable<T> {
- private final ArrayList<T> internal;
+ private final List<T> internal;
public final int length;
/**
* Creates an instance of the given capacity, with all elements initialized
to null
*/
+ @SuppressWarnings("unchecked")
public PreAllocatedArray(final int capacity) {
+ internal = Arrays.asList((T[]) new Object[capacity]);
length = capacity;
- internal = new ArrayList<>(capacity);
- for (int i = 0; i < capacity; i++) {
- internal.add(null);
- }
}
/**
@@ -60,6 +57,6 @@ public class PreAllocatedArray<T> implements Iterable<T> {
@Override
public Iterator<T> iterator() {
- return Iterators.unmodifiableIterator(internal.iterator());
+ return internal.iterator();
}
}
diff --git
a/core/src/test/java/org/apache/accumulo/core/util/PreAllocatedArrayTest.java
b/core/src/test/java/org/apache/accumulo/core/util/PreAllocatedArrayTest.java
index 3249716ce6..b25a43aea3 100644
---
a/core/src/test/java/org/apache/accumulo/core/util/PreAllocatedArrayTest.java
+++
b/core/src/test/java/org/apache/accumulo/core/util/PreAllocatedArrayTest.java
@@ -22,6 +22,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Iterator;
@@ -50,7 +51,7 @@ public class PreAllocatedArrayTest {
@Test
public void testPreAllocatedArray_Fail() {
- assertThrows(IllegalArgumentException.class, () -> new
PreAllocatedArray<String>(-5));
+ assertThrows(NegativeArraySizeException.class, () -> new
PreAllocatedArray<String>(-5));
}
/**
@@ -113,4 +114,20 @@ public class PreAllocatedArrayTest {
// spotbugs error suppressed at class level for lambda
assertThrows(IndexOutOfBoundsException.class, () -> strings.get(-3));
}
+
+ @Test
+ public void testIteratorRemove() {
+ PreAllocatedArray<String> strings = new PreAllocatedArray<>(3);
+ strings.set(1, "data");
+ var iter = strings.iterator();
+ for (int i = 0; i < 3; i++) {
+ assertTrue(iter.hasNext());
+ iter.next();
+ assertThrows(UnsupportedOperationException.class, () -> iter.remove());
+ }
+ assertFalse(iter.hasNext());
+ assertNull(strings.get(0));
+ assertEquals("data", strings.get(1));
+ assertNull(strings.get(2));
+ }
}