SetIterator.java revision 1626:d99fa86747ee
173134Ssobomax/*
273134Ssobomax * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
373134Ssobomax * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
473134Ssobomax *
573134Ssobomax * This code is free software; you can redistribute it and/or modify it
673134Ssobomax * under the terms of the GNU General Public License version 2 only, as
773134Ssobomax * published by the Free Software Foundation.  Oracle designates this
873134Ssobomax * particular file as subject to the "Classpath" exception as provided
973134Ssobomax * by Oracle in the LICENSE file that accompanied this code.
1073134Ssobomax *
1173134Ssobomax * This code is distributed in the hope that it will be useful, but WITHOUT
1273134Ssobomax * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1373134Ssobomax * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1473134Ssobomax * version 2 for more details (a copy is included in the LICENSE file that
1573134Ssobomax * accompanied this code).
1673134Ssobomax *
1773134Ssobomax * You should have received a copy of the GNU General Public License version
1873134Ssobomax * 2 along with this work; if not, write to the Free Software Foundation,
1973134Ssobomax * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2073134Ssobomax *
2193520Sobrien * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2293520Sobrien * or visit www.oracle.com if you need additional information or have any
2393520Sobrien * questions.
2473134Ssobomax */
2573134Ssobomax
2673134Ssobomaxpackage jdk.nashorn.internal.objects;
2773134Ssobomax
2873134Ssobomaximport jdk.nashorn.internal.objects.annotations.Function;
2973134Ssobomaximport jdk.nashorn.internal.objects.annotations.ScriptClass;
3073134Ssobomaximport jdk.nashorn.internal.runtime.PropertyMap;
3173134Ssobomaximport jdk.nashorn.internal.runtime.ScriptRuntime;
3273134Ssobomaximport jdk.nashorn.internal.runtime.Undefined;
3373134Ssobomax
3473134Ssobomaximport static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
3573134Ssobomax
3673134Ssobomax/**
3773134Ssobomax * ECMA6 23.2.5 Set Iterator Objects
3873134Ssobomax */
3973134Ssobomax@ScriptClass("SetIterator")
40131275Seikpublic class SetIterator extends AbstractIterator {
41131275Seik
4296613Ssobomax    // initialized by nasgen
4373144Ssobomax    private static PropertyMap $nasgenmap$;
44103726Swollman
4573134Ssobomax    private LinkedMap.LinkedMapIterator iterator;
4673134Ssobomax
4773134Ssobomax    private final IterationKind iterationKind;
48131275Seik
4973134Ssobomax    // Cached global needed for every iteration result
5073134Ssobomax    private final Global global;
5173134Ssobomax
5273134Ssobomax    SetIterator(final NativeSet set, final IterationKind iterationKind, final Global global) {
5373134Ssobomax        super(global.getSetIteratorPrototype(), $nasgenmap$);
5473134Ssobomax        this.iterator = set.getJavaMap().getIterator();
5573134Ssobomax        this.iterationKind = iterationKind;
5673134Ssobomax        this.global = global;
5773134Ssobomax    }
5873134Ssobomax
5973134Ssobomax    /**
6074258Ssobomax     * ES6 23.2.5.2.1 %SetIteratorPrototype%.next()
6184745Ssobomax     *
6284745Ssobomax     * @param self the self reference
6373134Ssobomax     * @param arg the argument
6473134Ssobomax     * @return the next result
6573134Ssobomax     */
66131285Seik    @Function
6773134Ssobomax    public static Object next(final Object self, final Object arg) {
6896613Ssobomax        if (!(self instanceof SetIterator)) {
6973134Ssobomax            throw typeError("not.a.set.iterator", ScriptRuntime.safeToString(self));
7096613Ssobomax        }
7196613Ssobomax        return ((SetIterator)self).next(arg);
7296613Ssobomax    }
7396613Ssobomax
7473134Ssobomax    @Override
7573134Ssobomax    public String getClassName() {
7673134Ssobomax        return "Set Iterator";
7773134Ssobomax    }
7884745Ssobomax
7973134Ssobomax    @Override
8073134Ssobomax    protected  IteratorResult next(final Object arg) {
8173134Ssobomax        if (iterator == null) {
8273134Ssobomax            return makeResult(Undefined.getUndefined(), Boolean.TRUE, global);
8373134Ssobomax        }
8473134Ssobomax
8574258Ssobomax        final LinkedMap.Node node = iterator.next();
8674699Ssobomax
8774699Ssobomax        if (node == null) {
8874699Ssobomax            iterator = null;
8974699Ssobomax            return makeResult(Undefined.getUndefined(), Boolean.TRUE, global);
9096392Salfred        }
9174699Ssobomax
9274699Ssobomax        if (iterationKind == IterationKind.KEY_VALUE) {
9374699Ssobomax            final NativeArray array = new NativeArray(new Object[] {node.getKey(), node.getKey()});
9474699Ssobomax            return makeResult(array, Boolean.FALSE, global);
9574699Ssobomax        }
9674699Ssobomax
9774699Ssobomax        return makeResult(node.getKey(), Boolean.FALSE, global);
9874258Ssobomax    }
9974258Ssobomax}
10074258Ssobomax