1 /* 2 * Copyright (c) 2010, 2013, 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 26 package jdk.nashorn.internal.runtime.arrays; 27 28 import java.util.Iterator; 29 import java.util.List; 30 import jdk.nashorn.api.scripting.JSObject; 31 import jdk.nashorn.internal.runtime.JSType; 32 import jdk.nashorn.internal.runtime.ScriptObject; 33 34 /** 35 * Superclass for array iterators 36 * TODO: rewrite these 37 * 38 * @param <T> element type 39 */ 40 abstract public class ArrayLikeIterator<T> implements Iterator<T> { 41 42 /** current element index in iteration */ 43 protected long index; 44 45 /** should undefined elements be included in the iteration? */ 46 protected final boolean includeUndefined; 47 48 /** 49 * Constructor 50 * 51 * @param includeUndefined should undefined elements be included in the iteration? 52 */ 53 ArrayLikeIterator(final boolean includeUndefined) { 54 this.includeUndefined = includeUndefined; 55 this.index = 0; 56 } 57 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 }