-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathArray.java
More file actions
81 lines (72 loc) · 1.84 KB
/
Array.java
File metadata and controls
81 lines (72 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package snap.webapi;
import java.util.ArrayList;
import java.util.List;
/**
* This class is a wrapper for Web API Array (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array).
*/
public class Array<E> extends JSProxy {
/**
* Constructor.
*/
public Array(Object arrayJS)
{
super(arrayJS);
}
/**
* Constructor.
*/
public Array(Object[] theObjects)
{
super(WebEnv.get().newArrayJSForLength(theObjects.length));
for (int i = 0; i < theObjects.length; i++)
set(i, theObjects[i]);
}
/**
* The length of the array.
*/
public int getLength() { return getMemberInt("length"); }
/**
* Returns value at given index.
*/
public E get(int index) { return (E) getSlot(index); }
/**
* Sets the given value at given index.
*/
public void set(int index, Object aValue)
{
Object value = aValue;
if (aValue instanceof JSProxy)
value = ((JSProxy) aValue)._jsObj;
setSlot(index, value);
}
/**
* Returns a list for this array.
*/
public List<E> toList()
{
int length = getLength();
List<E> list = new ArrayList<>(length);
for (int i = 0; i < length; i++)
list.add(get(i));
return list;
}
/**
* Returns array of objects as given class.
*/
public <T> T[] toArray(T[] anArray)
{
int length = getLength();
for (int i = 0; i < length; i++)
anArray[i] = (T) get(i);
return anArray;
}
/**
* Returns array of objects as given class.
*/
public <T> T[] toArray(Class<T> aClass)
{
int length = getLength();
T[] array = (T[]) java.lang.reflect.Array.newInstance(aClass, length);
return toArray(array);
}
}