ArrayLikeIterator.java revision 953:221a84ef44c0
190075Sobrien/*
2169689Skan * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
3146895Skan * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
490075Sobrien *
590075Sobrien * This code is free software; you can redistribute it and/or modify it
6132718Skan * under the terms of the GNU General Public License version 2 only, as
790075Sobrien * published by the Free Software Foundation.  Oracle designates this
8132718Skan * particular file as subject to the "Classpath" exception as provided
9132718Skan * by Oracle in the LICENSE file that accompanied this code.
10132718Skan *
11132718Skan * This code is distributed in the hope that it will be useful, but WITHOUT
1290075Sobrien * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13132718Skan * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14132718Skan * version 2 for more details (a copy is included in the LICENSE file that
15132718Skan * accompanied this code).
16132718Skan *
1790075Sobrien * You should have received a copy of the GNU General Public License version
18132718Skan * 2 along with this work; if not, write to the Free Software Foundation,
19132718Skan * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20169689Skan *
21169689Skan * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2290075Sobrien * or visit www.oracle.com if you need additional information or have any
2390075Sobrien * questions.
2490075Sobrien */
25117395Skan
26117395Skanpackage jdk.nashorn.internal.runtime.arrays;
27117395Skan
28117395Skanimport java.util.Iterator;
29117395Skanimport java.util.List;
30117395Skanimport jdk.nashorn.api.scripting.JSObject;
31146895Skanimport jdk.nashorn.internal.runtime.JSType;
32117395Skanimport jdk.nashorn.internal.runtime.ScriptObject;
33117395Skan
34117395Skan/**
35132718Skan * Superclass for array iterators
36117395Skan * TODO: rewrite these
37117395Skan *
38110611Skan * @param <T> element type
39110611Skan */
40110611Skanabstract public class ArrayLikeIterator<T> implements Iterator<T> {
41146895Skan
42146895Skan    /** current element index in iteration */
43146895Skan    protected long index;
44146895Skan
45146895Skan    /** should undefined elements be included in the iteration? */
46146895Skan    protected final boolean includeUndefined;
47146895Skan
48146895Skan    /**
49146895Skan     * Constructor
50146895Skan     *
51146895Skan     * @param includeUndefined should undefined elements be included in the iteration?
52146895Skan     */
53146895Skan    ArrayLikeIterator(final boolean includeUndefined) {
54146895Skan        this.includeUndefined = includeUndefined;
55146895Skan        this.index = 0;
56146895Skan    }
57146895Skan
58    /**
59     * Is this a reverse order iteration?
60     * @return true if reverse
61     */
62    public boolean isReverse() {
63        return false;
64    }
65
66    /**
67     * Go the the next valid element index of the iterator
68     * @return next index
69     */
70    protected long bumpIndex() {
71        return index++;
72    }
73
74    /**
75     * Return the next valid element index of the iterator
76     * @return next index
77     */
78    public long nextIndex() {
79        return index;
80    }
81
82    @Override
83    public void remove() {
84        throw new UnsupportedOperationException("remove");
85    }
86
87    /**
88     * Get the length of the iteration
89     * @return length
90     */
91    public abstract long getLength();
92
93    /**
94     * ArrayLikeIterator factory
95     *
96     * @param object object over which to do element iteration
97     * @return iterator
98     */
99    public static ArrayLikeIterator<Object> arrayLikeIterator(final Object object) {
100        return arrayLikeIterator(object, false);
101    }
102
103    /**
104     * ArrayLikeIterator factory (reverse order)
105     * @param object object over which to do reverse element iteration
106     * @return iterator
107     */
108    public static ArrayLikeIterator<Object> reverseArrayLikeIterator(final Object object) {
109        return reverseArrayLikeIterator(object, false);
110    }
111
112    /**
113     * ArrayLikeIterator factory
114     * @param object object over which to do reverse element iteration
115     * @param includeUndefined should undefined elements be included in the iteration
116     * @return iterator
117     */
118    public static ArrayLikeIterator<Object> arrayLikeIterator(final Object object, final boolean includeUndefined) {
119        Object obj = object;
120
121        if (ScriptObject.isArray(obj)) {
122            return new ScriptArrayIterator((ScriptObject) obj, includeUndefined);
123        }
124
125        obj = JSType.toScriptObject(obj);
126        if (obj instanceof ScriptObject) {
127            return new ScriptObjectIterator((ScriptObject)obj, includeUndefined);
128        }
129
130        if (obj instanceof JSObject) {
131            return new JSObjectIterator((JSObject)obj, includeUndefined);
132        }
133
134        if (obj instanceof List) {
135            return new JavaListIterator((List<?>)obj, includeUndefined);
136        }
137
138        if (obj != null && obj.getClass().isArray()) {
139            return new JavaArrayIterator(obj, includeUndefined);
140        }
141
142        return new EmptyArrayLikeIterator();
143    }
144
145    /**
146     * ArrayLikeIterator factory (reverse order)
147     * @param object object over which to do reverse element iteration
148     * @param includeUndefined should undefined elements be included in the iteration
149     * @return iterator
150     */
151    public static ArrayLikeIterator<Object> reverseArrayLikeIterator(final Object object, final boolean includeUndefined) {
152        Object obj = object;
153
154        if (ScriptObject.isArray(obj)) {
155            return new ReverseScriptArrayIterator((ScriptObject) obj, includeUndefined);
156        }
157
158        obj = JSType.toScriptObject(obj);
159        if (obj instanceof ScriptObject) {
160            return new ReverseScriptObjectIterator((ScriptObject)obj, includeUndefined);
161        }
162
163        if (obj instanceof JSObject) {
164            return new ReverseJSObjectIterator((JSObject)obj, includeUndefined);
165        }
166
167        if (obj instanceof List) {
168            return new ReverseJavaListIterator((List<?>)obj, includeUndefined);
169        }
170
171        if (obj != null && obj.getClass().isArray()) {
172            return new ReverseJavaArrayIterator(obj, includeUndefined);
173        }
174
175        return new EmptyArrayLikeIterator();
176    }
177
178}
179