java.util.Arrays Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dragome-js-jre Show documentation
Show all versions of dragome-js-jre Show documentation
Dragome SDK module: js-jre
package java.util;
import com.dragome.commons.javascript.ScriptHelper;
public class Arrays
{
/**
* Returns a fixed-size list backed by the specified array.
* @param a the array by which the list will be backed.
* @return list view of the specified array.
*/
public static List asList(T... a)
{
ArrayList result= new ArrayList();
for (T t : a)
result.add(t);
return result;
}
/**
* Returns true if the two specified arrays of Objects are equal to one another.
*/
public static boolean equals(Object[] a1, Object[] a2)
{
if (a1 == null && a2 == null)
return true;
if (a1 == null || a2 == null || a1.length != a2.length)
return false;
int count= a1.length;
for (int i= 0; i < count; i++)
{
Object e1= a1[i];
Object e2= a2[i];
if (!(e1 == null ? e2 == null : e1.equals(e2)))
return false;
}
return true;
}
private static class NaturalOrder> implements Comparator
{
public int compare(T o1, T o2)
{
return o1 != null ? o1.compareTo(o2) : o2 != null ? o2.compareTo(o1) : 0;
}
}
/**
* Sorts the specified array of objects into ascending order, according to the natural ordering of its elements.
*
* Important: This is a restriction of the Java API sort(Object[]) function.
*/
public static void sort(Object[] array)
{
ScriptHelper.put("array", array, null);
ScriptHelper.eval("array.sort()", null);
}
public static void sort(Object[] array, int fromIndex, int toIndex)
{
ScriptHelper.put("array", array, null);
ScriptHelper.put("fromIndex", fromIndex, null);
ScriptHelper.put("toIndex", toIndex, null);
Object subarray= ScriptHelper.eval("array.slice(fromIndex, toIndex)", null);
ScriptHelper.put("subarray", subarray, null);
ScriptHelper.eval("subarray.sort()", null);
ScriptHelper.eval("for (var i= fromIndex; i < toIndex; ++i) array[i]= subarray[i - fromIndex]", null);
}
public static void sort(T[] a, int fromIndex, int toIndex, Comparator super T> comparator)
{
T[] subarray= (T[]) new Object[toIndex - fromIndex];
for (int i= fromIndex; i < toIndex; ++i)
subarray[i - fromIndex]= a[i];
sort(subarray, comparator);
for (int i= fromIndex; i < toIndex; ++i)
a[i]= subarray[i - fromIndex];
}
/**
* Sorts the specified array of objects according to the order induced by the specified comparator.
*/
public static void sort(T[] array, Comparator super T> comparator)
{
ScriptHelper.put("array", array, null);
if (comparator == null)
comparator= new NaturalOrder();
ScriptHelper.put("c", comparator, null);
ScriptHelper.eval("array.sort(function(o1, o2) {return c.$compare___java_lang_Object__java_lang_Object$int(o1, o2)})", null);
}
public static int hashCode(Object array[])
{
if (array == null)
return 0;
int result= 1;
for (Object element : array)
result= 31 * result + (element == null ? 0 : element.hashCode());
return result;
}
public static void sort(double[] a)
{
sort(a);
}
public static boolean deepEquals0(Object a, Object b)
{
// TODO Auto-generated method stub
return false;
}
}