author | hannesw |
Wed, 21 Mar 2018 16:55:34 +0100 | |
changeset 49275 | c639a6b33c5c |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
19469
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
1 |
/* |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
2 |
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
4 |
* |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
10 |
* |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
15 |
* accompanied this code). |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
16 |
* |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
20 |
* |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
23 |
* questions. |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
24 |
*/ |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
25 |
|
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
26 |
package jdk.nashorn.internal.runtime.arrays; |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
27 |
|
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
28 |
import java.util.List; |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
29 |
|
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
30 |
/** |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
31 |
* Iterator over a Java List. |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
32 |
*/ |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
33 |
class JavaListIterator extends ArrayLikeIterator<Object> { |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
34 |
|
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
35 |
/** {@link java.util.List} to iterate over */ |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
36 |
protected final List<?> list; |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
37 |
|
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
38 |
/** length of array */ |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
39 |
protected final long length; |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
40 |
|
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
41 |
/** |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
42 |
* Constructor |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
43 |
* @param list list to iterate over |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
44 |
* @param includeUndefined should undefined elements be included in iteration |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
45 |
*/ |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
46 |
protected JavaListIterator(final List<?> list, final boolean includeUndefined) { |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
47 |
super(includeUndefined); |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
48 |
this.list = list; |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
49 |
this.length = list.size(); |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
50 |
} |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
51 |
|
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
52 |
/** |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
53 |
* Is the current index still inside the array |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
54 |
* @return true if inside the array |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
55 |
*/ |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
56 |
protected boolean indexInArray() { |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
57 |
return index < length; |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
58 |
} |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
59 |
|
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
60 |
@Override |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
61 |
public Object next() { |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
62 |
return list.get((int)bumpIndex()); |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
63 |
} |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
64 |
|
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
65 |
@Override |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
66 |
public long getLength() { |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
67 |
return length; |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
68 |
} |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
69 |
|
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
70 |
@Override |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
71 |
public boolean hasNext() { |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
72 |
return indexInArray(); |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
73 |
} |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
74 |
|
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
75 |
@Override |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
76 |
public void remove() { |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
77 |
list.remove(index); |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
78 |
} |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
diff
changeset
|
79 |
} |