author | attila |
Wed, 04 Jun 2014 13:08:57 +0200 | |
changeset 24778 | 2ff5d7041566 |
parent 17765 | 6b45f57bebc4 |
permissions | -rw-r--r-- |
17765
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
1 |
/* |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
2 |
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
17765
diff
changeset
|
4 |
* |
17765
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
17765
diff
changeset
|
8 |
* |
17765
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
13 |
* accompanied this code). |
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
17765
diff
changeset
|
14 |
* |
17765
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
17765
diff
changeset
|
18 |
* |
17765
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
21 |
* questions. |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
22 |
*/ |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
23 |
|
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
24 |
/** |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
25 |
* JDK-8015267: have a List/Deque adapter for JS array-like objects |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
26 |
* |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
27 |
* @test |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
28 |
* @run |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
29 |
*/ |
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
17765
diff
changeset
|
30 |
|
17765
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
31 |
var a = ['a', 'b', 'c', 'd'] |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
32 |
|
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
33 |
var l = Java.to(a, java.util.List) |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
34 |
print(l instanceof java.util.List) |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
35 |
print(l instanceof java.util.Deque) |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
36 |
|
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
37 |
print(l[0]) |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
38 |
print(l[1]) |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
39 |
print(l[2]) |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
40 |
print(l[3]) |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
41 |
|
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
42 |
print(l.size()) |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
43 |
|
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
44 |
l.push('x') |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
45 |
print(a) |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
46 |
|
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
47 |
l.addLast('y') |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
48 |
print(a) |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
49 |
|
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
50 |
print(l.pop()) |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
51 |
print(l.removeLast()) |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
52 |
print(a) |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
53 |
|
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
54 |
l.add('e') |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
55 |
l.add(5, 'f') |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
56 |
print(a) |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
57 |
|
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
58 |
l.add(0, 'z') |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
59 |
print(a) |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
60 |
|
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
61 |
l.add(2, 'x') |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
62 |
print(a) |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
63 |
|
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
64 |
l[7] = 'g' |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
65 |
print(a) |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
66 |
|
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
17765
diff
changeset
|
67 |
try { l.add(15, '') } catch(e) { print(e.class) } |
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
17765
diff
changeset
|
68 |
try { l.remove(15) } catch(e) { print(e.class) } |
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
17765
diff
changeset
|
69 |
try { l.add(-1, '') } catch(e) { print(e.class) } |
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
17765
diff
changeset
|
70 |
try { l.remove(-1) } catch(e) { print(e.class) } |
17765
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
71 |
|
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
72 |
l.remove(7) |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
73 |
l.remove(2) |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
74 |
l.remove(0) |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
75 |
print(a) |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
76 |
|
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
77 |
print(l.peek()) |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
78 |
print(l.peekFirst()) |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
79 |
print(l.peekLast()) |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
80 |
|
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
81 |
print(l.element()) |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
82 |
print(l.getFirst()) |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
83 |
print(l.getLast()) |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
84 |
|
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
85 |
l.offer('1') |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
86 |
l.offerFirst('2') |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
87 |
l.offerLast('3') |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
88 |
print(a) |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
89 |
|
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
90 |
a = ['1', '2', 'x', '3', '4', 'x', '5', '6', 'x', '7', '8'] |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
91 |
print(a) |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
92 |
var l = Java.to(a, java.util.List) |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
93 |
l.removeFirstOccurrence('x') |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
94 |
print(a) |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
95 |
l.removeLastOccurrence('x') |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
96 |
print(a) |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
97 |
|
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
98 |
var empty = Java.to([], java.util.List) |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
99 |
try { empty.pop() } catch(e) { print(e.class) } |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
100 |
try { empty.removeFirst() } catch(e) { print(e.class) } |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
101 |
try { empty.removeLast() } catch(e) { print(e.class) } |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
102 |
|
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
103 |
try { empty.element() } catch(e) { print(e.class) } |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
104 |
try { empty.getFirst() } catch(e) { print(e.class) } |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
105 |
try { empty.getLast() } catch(e) { print(e.class) } |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
106 |
|
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
107 |
print(empty.peek()) |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
108 |
print(empty.peekFirst()) |
6b45f57bebc4
8015267: Allow conversion of JS arrays to Java List/Deque
attila
parents:
diff
changeset
|
109 |
print(empty.peekLast()) |