0,0 → 1,197 |
package net.outlyer.plugins.utils; |
|
/* |
* Copyright (c) 2008, Toni Corvera. All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions are met: |
* |
* * Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* * Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* |
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, |
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
|
// $Id$ |
|
import java.lang.reflect.Array; |
import java.util.Arrays; |
import net.outlyer.plugins.Functor; |
import net.outlyer.plugins.SandboxAccessor; |
|
/** |
* Extra facilities found in other languages or more complete |
* versions of available facilities. |
* Recommended name: lang |
*/ |
public class LanguageExtensions extends LanguageExtensions_V1 |
implements SandboxAccessor { |
|
public static final int revision = 2; |
|
// 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 |
|
/** |
* 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 |
*/ |
public <T> T[] array(final T tplt, int size) { |
if (null == tplt || Void.class == tplt.getClass()) { |
return null; |
} |
return (T[]) Array.newInstance(tplt.getClass(), size); |
} |
|
/** |
* 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 |
*/ |
public <T> T[] array(final Class<T> c, int size) { |
return (T[]) Array.newInstance(c, size); |
} |
|
/** |
* Create an array and copy a set of elements to it. |
* Note that a JavaScript array will produce a java.lang.Object array |
* @param size Size of the array to create (if less than |
* <code>initValues.length</code>, will use |
* <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 |
*/ |
public <T> T[] array(final T[] initValues, int size) { |
if (null == initValues) { |
return null; |
} |
final T[] a = Arrays.copyOf(initValues, Math.max(size, initValues.length)); |
if (null == a) { |
return null; |
} |
for (int i=0; i<initValues.length; ++i) { |
a[i] = initValues[i]; |
} |
return a; |
} |
|
/** |
* Shorthand form, allows getting a Java array from a JavaScript array. |
* @param initValues |
* @since {@link #revision} 2 |
*/ |
public <T> T[] array(final T[] initValues) { |
return array(initValues, initValues.length); |
} |
|
public void var_dump(final Object obj) { |
System.out.println(var_dump_(obj)); |
} |
|
private String var_dump_(final Object obj) { |
final StringBuilder sb = new StringBuilder(); |
if (null == obj) { |
sb.append("null"); |
} |
else if (obj.getClass() == String.class) { |
sb.append("String(").append(((String)obj).length()) |
.append(") \"").append(obj).append("\""); |
} |
else if (Number.class.isInstance(obj)) { |
final Class c = obj.getClass(); |
String v; |
if (c == Double.class) { // Most probable |
v = String.format("double(%f)", (Double)obj); |
} |
else if (c == Float.class) { |
v = String.format("float(%f)", (Float)obj); |
} |
else if (c == Long.class) { |
v = String.format("long(%d)", (Long)obj); |
} |
else if (c == Integer.class) { |
v = String.format("int(%d)", (Integer)obj); |
} |
else if (c == Short.class) { |
v = String.format("short(%d)", (Short)obj); |
} |
else if (c == Byte.class) { |
v = String.format("byte(%d)", (Byte)obj); |
} |
else { |
v = String.format("%s(%f)", c.getSimpleName(), |
((Number)obj).doubleValue()); |
} |
sb.append(v); |
} |
else if (obj.getClass().isArray()) { |
sb.append("array(").append(java.lang.reflect.Array.getLength(obj)) |
.append(") {\n"); |
/* |
for (final Object elem : java.util.Arrays.asList(obj)) { |
sb.append(var_dump_(elem)); |
} |
*/ |
int len = java.lang.reflect.Array.getLength(obj); |
for (int i=0; i<len; ++i) { |
sb.append(" [").append(i).append("]=>\n ") |
.append(var_dump_(java.lang.reflect.Array.get(obj, i))) |
.append("\n"); |
} |
sb.append("}"); |
} |
else { |
sb.append(obj); |
} |
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); |
} |
} |
Property changes: |
Added: svn:keywords |
+Rev Id Date |
\ No newline at end of property |