void forEach(Consumer c){
checkNotNull(c);
for (T item : items) {
c.accept(item);
}
}public static void sort(byte[] array, int fromIndex, int toIndex) {
checkPositionIndexes(fromIndex, toIndex, array.length);
nativeNumberSort(array, fromIndex, toIndex);
}
private static native void nativeNumberSort(Object array, int fromIndex, int toIndex) /*-{
var temp = array.slice(fromIndex, toIndex);
temp.sort(function(a, b) {
return a - b;
});
var n = toIndex - fromIndex;
@com.google.gwt.lang.Array::nativeArraycopy(Ljava/lang/Object;ILjava/lang/Object;II)(temp, 0, array, fromIndex, n)
}-*/;
Sometimes I am not sure if we should use a normal or a critical check.Basically my understanding is that we should use a normal check if the code would also fail (but obviously with a JS error instead of a required Java Exception) with an error if the check was not present. If the code would fail pretty late and debugging will be difficult we better use a critical check instead to help debugging. However if the code will not fail if a check has been compiled out then we should always use a critical check, especially if JavaDoc requires the code to fail. Is that correct?
Here are some examples I struggle with:1.) checkNotNull() if JavaDoc requires NPE to be thrown in all cases.void forEach(Consumer c){
checkNotNull(c);
for (T item : items) {
c.accept(item);
}
}
JavaDoc says NPE must be thrown if consumer is null. If checks are compiled out, for the special case that items is empty it would not fail with a normal check, so we should use a criticalCheckNotNull() right?
2.) checking position indexes during index operations on arrays/listspublic static void sort(byte[] array, int fromIndex, int toIndex) {
checkPositionIndexes(fromIndex, toIndex, array.length);
nativeNumberSort(array, fromIndex, toIndex);
}
private static native void nativeNumberSort(Object array, int fromIndex, int toIndex) /*-{
var temp = array.slice(fromIndex, toIndex);
temp.sort(function(a, b) {
return a - b;
});
var n = toIndex - fromIndex;
@com.google.gwt.lang.Array::nativeArraycopy(Ljava/lang/Object;ILjava/lang/Object;II)(temp, 0, array, fromIndex, n)
}-*/;
The above is some existing code in GWT. If checks are compiled out nativeNumberSort() could get indexes with fromIndex > toIndex. JavaScript slice() will then return an empty temp array that will be "sorted" and passed on to nativeArracopy() which in turn won't fail as well. So at the end the check should have been a critical check, shouldn't it?-- J.
--
You received this message because you are subscribed to the Google Groups "GWT Contributors" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-web-toolkit-contributors/b3bc70c4-be2c-4fd1-a750-c5ada03caeed%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Hm ok, I think I got it. I would say my Arrays.sort() example should actually use a critical check then because array.slice() can do lots unexpected things (negative indexes for either argument works but results an unexpected subset of array items to be sorted, toIndex can be larger than fromIndex which basically results in no sorting at all) compared to Java. A critical check would make sure that follow up code can expect the array to be sorted they way its meant to be.
I guess in some cases its just tough to draw the line.
---- J.
You received this message because you are subscribed to the Google Groups "GWT Contributors" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-web-toolkit-contributors/28e222a3-9138-447c-aeaa-a2daca59ccdd%40googlegroups.com.