Copilot commented on code in PR #6909:
URL: https://github.com/apache/incubator-kie/pull/6909#discussion_r3872567604


##########
drools-base/src/main/java/org/drools/base/util/CircularArrayList.java:
##########
@@ -0,0 +1,117 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.drools.base.util;
+
+import java.lang.reflect.Array;
+import java.util.Arrays;
+
+public class CircularArrayList<E> {
+    private E[] array;
+    private int head = 0;
+    // capacity is always a power of two; mask = capacity - 1 so that
+    // (index & mask) is equivalent to (index % capacity) but avoids division.
+    private final int capacity;
+    private final int mask;
+    private Class cls;
+
+    public CircularArrayList(int capacity) {
+        this(Object.class, capacity);
+    }
+
+    public CircularArrayList(Class cls, int capacity) {
+        // Round up to the nearest power of two (same strategy as HashMap).
+        int cap = capacity <= 1 ? 1 : Integer.highestOneBit(capacity - 1) << 1;
+        this.capacity = cap;
+        this.mask     = cap - 1;
+        this.array    = (E[]) new Object[cap];
+        this.cls      = cls;
+    }
+
+    public void resetHeadByOffset(int offset) {
+        head = head-offset;
+    }
+
+    public boolean set(int index, E e) {
+        array[index & mask] = e;
+        return true;
+    }
+
+    public boolean add(E e) {
+            array[head++ & mask] = e;
+            return true;
+    }
+
+    public void addEmpty(int size) {
+        for (int i = 0; i < size; i++) {
+            array[head++ & mask] = null;
+        }
+    }
+
+    public E getHead() {
+        return array[(head-1) & mask];
+    }
+
+    public E getHeadMinus(int i) {
+        return array[(head-1-i) & mask];
+    }
+
+    public E fastGet(int index) {
+        return array[index & mask];
+    }
+
+    public E get(int index) {
+        if (index < head - capacity ) {
+            throw new IndexOutOfBoundsException("Index: " + index + ", Size: " 
+ capacity);
+        }
+
+        return array[index & mask];
+    }

Review Comment:
   `get(int index)` enforces a lower bound (too-old) but does not enforce the 
upper bound (`index >= head`). As written, `get(head)` (or larger) returns an 
unrelated slot from the ring and can surface stale/incorrect data without 
throwing. Add an explicit `if (index < 0 || index >= head)` (or equivalent) 
check to make out-of-range access fail fast.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to