Subversion Repositories pub

Compare Revisions

Ignore whitespace Rev 67 → Rev 68

/pluggablejs/trunk/src/net/outlyer/plugins/utils/LanguageExtensions.java
28,7 → 28,6
 
import java.lang.reflect.Array;
import java.util.Arrays;
import net.outlyer.plugins.Functor;
import net.outlyer.plugins.SandboxAccessor;
 
/**
39,8 → 38,16
public class LanguageExtensions extends LanguageExtensions_V1
implements SandboxAccessor {
 
public static final int revision = 2;
public final int interfaceRevision;
 
public LanguageExtensions() {
this(2);
}
protected LanguageExtensions(int revision) {
interfaceRevision = revision;
}
 
// The array() family is mostly redundant since rhino translates
// automatically as required from JavaScript arrays to Java arrays
// but there are some edge-cases in which they might be desirable
49,7 → 56,7
* Create an array of the same class as the provided object.
* @param tplt Template for the array, must be non-null and non-Void
* @param size Size of the array to create (can be 0)
* @since {@link #revision} 2
* @since {@link #interfaceRevision} 2
*/
public <T> T[] array(final T tplt, int size) {
if (null == tplt || Void.class == tplt.getClass()) {
62,7 → 69,7
* Create an array of the provided class.
* @param c Class of the array elements, must be non-null and different from Void.class
* @param size Size of the array (can be 0)
* @since {@link #revision} 2
* @since {@link #interfaceRevision} 2
*/
public <T> T[] array(final Class<T> c, int size) {
return (T[]) Array.newInstance(c, size);
76,7 → 83,7
* <code>initValues.length</code> instead) (i.e., use 0 to
* get the same size as initValues)
* @param initValues Initial values to put in the array
* @since {@link #revision} 2
* @since {@link #interfaceRevision} 2
*/
public <T> T[] array(final T[] initValues, int size) {
if (null == initValues) {
95,7 → 102,7
/**
* Shorthand form, allows getting a Java array from a JavaScript array.
* @param initValues
* @since {@link #revision} 2
* @since {@link #interfaceRevision} 2
*/
public <T> T[] array(final T[] initValues) {
return array(initValues, initValues.length);
162,36 → 169,4
}
return sb.toString();
}
 
/**
* Apply a function to each element of an iterable collection.
* @see #for_each(Object[], Functor)
* @param c Collection to which to apply
* @param f Function to apply
* @since {@link #revision} 2
*/
public <T> void for_each(final Iterable<T> c, final Functor<Object, T> f) {
for (final T elem : c) {
f.apply(elem);
}
}
/**
* Apply a function to each element of an array
* A JavaScript function can be used transparently as a functor, e.g.:
* <br />
* <code>var arr = lang.array(new Array(1, 2, 3));<br />
* lang.for_each(arr, function(x) { err.println("Element: "+x); });</code>>
* Will produce:
* <pre>Element: 1<br />Element: 2<br />Element: 3</pre>
* @param a Array to which to apply
* @param f Function to apply
* @since {@link #revision} 2
*/
public <T> void for_each(final T[] a, final Functor<Object, T> f) {
if (null == a || null == f) {
return; // FIXME: Error handling
}
for_each(Arrays.asList(a), f);
}
}