1/*
2 * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
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
26package jdk.nashorn.internal.objects;
27
28import jdk.nashorn.internal.objects.annotations.Function;
29import jdk.nashorn.internal.objects.annotations.ScriptClass;
30import jdk.nashorn.internal.runtime.PropertyMap;
31import jdk.nashorn.internal.runtime.ScriptRuntime;
32import jdk.nashorn.internal.runtime.Undefined;
33
34import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
35
36/**
37 * ECMA6 23.1.5 Map Iterator Objects
38 */
39@ScriptClass("MapIterator")
40public class MapIterator extends AbstractIterator {
41    // initialized by nasgen
42    private static PropertyMap $nasgenmap$;
43
44    private LinkedMap.LinkedMapIterator iterator;
45
46    private final IterationKind iterationKind;
47
48    // Cached global needed for each iteration result
49    private final Global global;
50
51    /**
52     * Constructor for Map iterators.
53     * @param map the map to iterate over
54     * @param iterationKind the iteration kind
55     * @param global the current global object
56     */
57    MapIterator(final NativeMap map, final IterationKind iterationKind, final Global global) {
58        super(global.getMapIteratorPrototype(), $nasgenmap$);
59        this.iterator = map.getJavaMap().getIterator();
60        this.iterationKind = iterationKind;
61        this.global = global;
62    }
63
64    /**
65     * ES6 23.1.5.2.1 %MapIteratorPrototype%.next()
66     *
67     * @param self the self reference
68     * @param arg the argument
69     * @return the next result
70     */
71    @Function
72    public static Object next(final Object self, final Object arg) {
73        if (!(self instanceof MapIterator)) {
74            throw typeError("not.a.map.iterator", ScriptRuntime.safeToString(self));
75        }
76        return ((MapIterator)self).next(arg);
77    }
78
79    @Override
80    public String getClassName() {
81        return "Map Iterator";
82    }
83
84    @Override
85    protected  IteratorResult next(final Object arg) {
86        if (iterator == null) {
87            return makeResult(Undefined.getUndefined(), Boolean.TRUE, global);
88        }
89
90        final LinkedMap.Node node = iterator.next();
91
92        if (node == null) {
93            iterator = null;
94            return makeResult(Undefined.getUndefined(), Boolean.TRUE, global);
95        }
96
97        if (iterationKind == IterationKind.KEY_VALUE) {
98            final NativeArray array = new NativeArray(new Object[] {node.getKey(), node.getValue()});
99            return makeResult(array, Boolean.FALSE, global);
100        }
101
102        return makeResult(iterationKind == IterationKind.KEY ? node.getKey() : node.getValue(), Boolean.FALSE, global);
103    }
104
105}
106