Skip to content

Commit 07b9feb

Browse files
committed
ShapeRefC: add a length field with accessor and runtime checks
1 parent f7f2048 commit 07b9feb

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

src/main/java/com/github/stephengold/joltjni/ShapeRefCArray.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ of this software and associated documentation files (the "Software"), to deal
2828
* @author xI-Mx-Ix
2929
*/
3030
public class ShapeRefCArray extends JoltPhysicsObject {
31+
// *************************************************************************
32+
// fields
33+
34+
/**
35+
* length (in references)
36+
*/
37+
final private int length;
3138
// *************************************************************************
3239
// constructors
3340

@@ -39,6 +46,7 @@ public class ShapeRefCArray extends JoltPhysicsObject {
3946
public ShapeRefCArray(int length) {
4047
assert length > 0 : "length=" + length;
4148

49+
this.length = length;
4250
long arrayVa = create(length);
4351
setVirtualAddress(arrayVa, () -> free(arrayVa));
4452
}
@@ -48,24 +56,40 @@ public ShapeRefCArray(int length) {
4856
/**
4957
* Access the reference at the specified index.
5058
*
51-
* @param elementIndex the index (≥0)
59+
* @param elementIndex the index (≥0, <length)
5260
* @return a new JVM object with the pre-existing native object assigned
5361
*/
5462
public ShapeRefC get(int elementIndex) {
63+
assert elementIndex >= 0 && elementIndex < length :
64+
"Out of range: index=" + elementIndex + " length=" + length;
65+
5566
long arrayVa = va();
5667
long refVa = getRef(arrayVa, elementIndex);
5768
ShapeRefC result = new ShapeRefC(this, refVa);
5869

5970
return result;
6071
}
6172

73+
/**
74+
* Return the length of the array.
75+
*
76+
* @return the length (in references, &gt;0)
77+
*/
78+
public int length() {
79+
return length;
80+
}
81+
6282
/**
6383
* Store the specified reference at the specified index.
6484
*
65-
* @param elementIndex the index at which to store the reference (&ge;0)
85+
* @param elementIndex the index at which to store the reference (&ge;0,
86+
* &lt;length)
6687
* @param ref the reference to store (not {@code null}, unaffected)
6788
*/
6889
public void set(int elementIndex, ShapeRefC ref) {
90+
assert elementIndex >= 0 && elementIndex < length :
91+
"Out of range: index=" + elementIndex + " length=" + length;
92+
6993
long arrayVa = va();
7094
long refVa = ref.va();
7195
setRef(arrayVa, elementIndex, refVa);

0 commit comments

Comments
 (0)