9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package jdk.nashorn.internal.runtime.arrays;
27
28 import java.util.Iterator;
29 import jdk.nashorn.internal.runtime.JSType;
30 import jdk.nashorn.internal.runtime.ScriptObject;
31
32 /**
33 * Superclass for array iterators
34 * TODO: rewrite these
35 *
36 * @param <T> element type
37 */
38 abstract public class ArrayLikeIterator<T> implements Iterator<T> {
39
40 /** current element index in iteration */
41 protected long index;
42
43 /** should undefined elements be included in the iteration? */
44 protected final boolean includeUndefined;
45
46 /**
47 * Constructor
48 *
108 }
109
110 /**
111 * ArrayLikeIterator factory
112 * @param object object over which to do reverse element iteration
113 * @param includeUndefined should undefined elements be included in the iteration
114 * @return iterator
115 */
116 public static ArrayLikeIterator<Object> arrayLikeIterator(final Object object, final boolean includeUndefined) {
117 Object obj = object;
118
119 if (ScriptObject.isArray(obj)) {
120 return new ArrayIterator((ScriptObject) obj, includeUndefined);
121 }
122
123 obj = JSType.toScriptObject(obj);
124 if (obj instanceof ScriptObject) {
125 return new MapIterator((ScriptObject)obj, includeUndefined);
126 }
127
128 return new EmptyArrayLikeIterator();
129 }
130
131 /**
132 * ArrayLikeIterator factory (reverse order)
133 * @param object object over which to do reverse element iteration
134 * @param includeUndefined should undefined elements be included in the iteration
135 * @return iterator
136 */
137 public static ArrayLikeIterator<Object> reverseArrayLikeIterator(final Object object, final boolean includeUndefined) {
138 Object obj = object;
139
140 if (ScriptObject.isArray(obj)) {
141 return new ReverseArrayIterator((ScriptObject) obj, includeUndefined);
142 }
143
144 obj = JSType.toScriptObject(obj);
145 if (obj instanceof ScriptObject) {
146 return new ReverseMapIterator((ScriptObject)obj, includeUndefined);
147 }
148
149 assert !obj.getClass().isArray();
150
151 return new EmptyArrayLikeIterator();
152 }
153
154 }
|
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package jdk.nashorn.internal.runtime.arrays;
27
28 import java.util.Iterator;
29 import jdk.nashorn.api.scripting.ScriptObjectMirror;
30 import jdk.nashorn.internal.runtime.JSType;
31 import jdk.nashorn.internal.runtime.ScriptObject;
32
33 /**
34 * Superclass for array iterators
35 * TODO: rewrite these
36 *
37 * @param <T> element type
38 */
39 abstract public class ArrayLikeIterator<T> implements Iterator<T> {
40
41 /** current element index in iteration */
42 protected long index;
43
44 /** should undefined elements be included in the iteration? */
45 protected final boolean includeUndefined;
46
47 /**
48 * Constructor
49 *
109 }
110
111 /**
112 * ArrayLikeIterator factory
113 * @param object object over which to do reverse element iteration
114 * @param includeUndefined should undefined elements be included in the iteration
115 * @return iterator
116 */
117 public static ArrayLikeIterator<Object> arrayLikeIterator(final Object object, final boolean includeUndefined) {
118 Object obj = object;
119
120 if (ScriptObject.isArray(obj)) {
121 return new ArrayIterator((ScriptObject) obj, includeUndefined);
122 }
123
124 obj = JSType.toScriptObject(obj);
125 if (obj instanceof ScriptObject) {
126 return new MapIterator((ScriptObject)obj, includeUndefined);
127 }
128
129 if (obj instanceof ScriptObjectMirror) {
130 return new ScriptObjectMirrorIterator((ScriptObjectMirror)obj, includeUndefined);
131 }
132
133 return new EmptyArrayLikeIterator();
134 }
135
136 /**
137 * ArrayLikeIterator factory (reverse order)
138 * @param object object over which to do reverse element iteration
139 * @param includeUndefined should undefined elements be included in the iteration
140 * @return iterator
141 */
142 public static ArrayLikeIterator<Object> reverseArrayLikeIterator(final Object object, final boolean includeUndefined) {
143 Object obj = object;
144
145 if (ScriptObject.isArray(obj)) {
146 return new ReverseArrayIterator((ScriptObject) obj, includeUndefined);
147 }
148
149 obj = JSType.toScriptObject(obj);
150 if (obj instanceof ScriptObject) {
151 return new ReverseMapIterator((ScriptObject)obj, includeUndefined);
152 }
153
154 if (obj instanceof ScriptObjectMirror) {
155 return new ReverseScriptObjectMirrorIterator((ScriptObjectMirror)obj, includeUndefined);
156 }
157
158 assert !obj.getClass().isArray();
159
160 return new EmptyArrayLikeIterator();
161 }
162
163 }
|