Skip to content

Commit e09605b

Browse files
committed
commented
Comments. Array setter for extension methods. Array null check removed: npe anyway.
1 parent 3cd7ecd commit e09605b

1 file changed

Lines changed: 74 additions & 1 deletion

File tree

src/main/java/com/scriptbasic/executors/rightvalues/BasicArrayValue.java

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,76 @@
99
public class BasicArrayValue extends AbstractRightValue {
1010
private static final Integer INCREMENT_GAP = 100;
1111
private Object[] array = new Object[INCREMENT_GAP];
12+
1213
private int maxIndex = -1;
1314
// TODO implement a function in the interpreter that can limit the
1415
// allocation of arrays
1516
// perhaps only the size of the individual arrays
1617
private ExtendedInterpreter interpreter;
1718

19+
/**
20+
* Set the array object. This method is available as a convenience method
21+
* for extension methods and is not used by the interpreter. This method can
22+
* be used when the array is available from some calculation and it would be
23+
* waste of resource to copy the elements of the array one by one calling
24+
* {@ref #set(Integer, Object)}.
25+
*
26+
* @param array
27+
* the array
28+
*
29+
* @throws NullPointerException
30+
* when the array is null
31+
*/
32+
public void setArray(Object[] array) {
33+
if (array == null) {
34+
throw new NullPointerException(
35+
"BasicArrayValue embedded array cann ot be null");
36+
}
37+
this.array = array;
38+
}
39+
40+
/**
41+
* Set the interpreter that this array belongs to.
42+
* <p>
43+
* Note that this method is used only from the code where the interpreter
44+
* calls an extension method that returns a BasicArrayValue. In that case
45+
* the parameter less constructor of this class is called by the extension
46+
* method and thus the BasicArrayValue does not know the interpreter and can
47+
* not request suggestion from the interpreter to perform resizing or throw
48+
* exception.
49+
* <p>
50+
* When the parameterless version of the constructor becomes deprecated this
51+
* setter will also become deprecated.
52+
*
53+
* @param interpreter
54+
*/
1855
public void setInterpreter(ExtendedInterpreter interpreter) {
1956
this.interpreter = interpreter;
2057
}
2158

59+
/**
60+
* This constructor can be used by extension classes to instantiate a new
61+
* BasicArrayValue when the extension function does not have access to the
62+
* interpreter.
63+
* <p>
64+
* Note that in later versions this constructor will be deprecated as soon
65+
* as the interface of the extensions will make it possible to pass the
66+
* interpreter along to the extension methods.
67+
*/
2268
public BasicArrayValue() {
2369
this.interpreter = null;
2470
}
2571

72+
/**
73+
* Create a new BasicArrayValue and remember the interpreter.
74+
* <p>
75+
* The interpreter can determine the maximum size allowed for arrays and
76+
* therefore may suggest for a BasicArrayValue not to extend its size, but
77+
* rather throw exception. This is to prevent allocating extraordinary large
78+
* arrays in an interpreter by mistake.
79+
*
80+
* @param interpreter
81+
*/
2682
public BasicArrayValue(ExtendedInterpreter interpreter) {
2783
this.interpreter = interpreter;
2884
}
@@ -31,11 +87,18 @@ private void assertArraySize(Integer index) throws ExecutionException {
3187
if (index < 0) {
3288
throw new BasicRuntimeException("Array index can not be negative");
3389
}
34-
if (array == null || array.length <= index) {
90+
if (array.length <= index) {
3591
array = Arrays.copyOf(array, index + INCREMENT_GAP);
3692
}
3793
}
3894

95+
/**
96+
* Get the length of the array. This is not the length of the underlying
97+
* object array but the size that the BASIC program should feel.
98+
*
99+
* @return the length of the array, which is n+1, where n is the maximal
100+
* index of the array the BASIC program ever used.
101+
*/
39102
public Long getLength() {
40103
return new Long(maxIndex + 1);
41104
}
@@ -56,6 +119,16 @@ public void set(Integer index, Object object) throws ExecutionException {
56119
}
57120
}
58121

122+
/**
123+
* Get the {@code index}-th element of the array. Note that this method does
124+
* NOT convert the value to an ordinary Java object. Thus when calling this
125+
* method from an extension method be prepared to convert the value to
126+
* ordinary Java object yourself.
127+
*
128+
* @param index
129+
* @return the array element.
130+
* @throws ExecutionException
131+
*/
59132
public Object get(Integer index) throws ExecutionException {
60133
assertArraySize(index);
61134
return array[index];

0 commit comments

Comments
 (0)