From 777fb9ce5ba7d4b80bd51598b1616cba8656242d Mon Sep 17 00:00:00 2001 From: walterhiggins Date: Sun, 17 Mar 2013 11:57:48 +0000 Subject: [PATCH] Add support for java.util.Collection to utils.foreach() --- docs/api.md | 4 ++-- src/main/javascript/utils/utils.js | 8 +++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/docs/api.md b/docs/api.md index aa5155a..4c901eb 100644 --- a/docs/api.md +++ b/docs/api.md @@ -1065,7 +1065,7 @@ Miscellaneous utility functions and classes to help with programming. utils.foreach() function ======================== The utils.foreach() function is a utility function for iterating over -an array of objects and processing each one in turn. Where +an array of objects (or a java.util.Collection of objects) and processing each object in turn. Where utils.foreach() differs from other similar functions found in javascript libraries, is that utils.foreach can process the array immediately or can process it *nicely* by processing one item at a @@ -1079,7 +1079,7 @@ package for scheduling processing of arrays. Parameters ---------- - * array : The array to be processed + * array : The array to be processed - It can be a javascript array, a java array or java.util.Collection * callback : The function to be called to process each item in the array. The callback function should have the following signature `callback(item, index, object, array)`. That is the callback will diff --git a/src/main/javascript/utils/utils.js b/src/main/javascript/utils/utils.js index 0ba1453..0597041 100644 --- a/src/main/javascript/utils/utils.js +++ b/src/main/javascript/utils/utils.js @@ -25,7 +25,7 @@ var utils = utils || { utils.foreach() function ======================== The utils.foreach() function is a utility function for iterating over -an array of objects and processing each one in turn. Where +an array of objects (or a java.util.Collection of objects) and processing each object in turn. Where utils.foreach() differs from other similar functions found in javascript libraries, is that utils.foreach can process the array immediately or can process it *nicely* by processing one item at a @@ -39,7 +39,7 @@ package for scheduling processing of arrays. Parameters ---------- - * array : The array to be processed + * array : The array to be processed - It can be a javascript array, a java array or java.util.Collection * callback : The function to be called to process each item in the array. The callback function should have the following signature `callback(item, index, object, array)`. That is the callback will @@ -97,7 +97,9 @@ without hogging CPU usage... foreach (a, processItem, null, 10, onDone); ***/ - foreach: function(array, callback, object, delay, onCompletion){ + foreach: function(array, callback, object, delay, onCompletion) { + if (array instanceof java.util.Collection) + array = array.toArray(); var i = 0; var len = array.length; if (delay){