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 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      *
  50      * @param includeUndefined should undefined elements be included in the iteration?
  51      */
  52     protected ArrayLikeIterator(final boolean includeUndefined) {
  53         this.includeUndefined = includeUndefined;
  54         this.index = 0;
  55     }
  56 
  57     /**
  58      * Is this a reverse order iteration?
  59      * @return true if reverse
  60      */
  61     public boolean isReverse() {
  62         return false;
  63     }
  64 
  65     /**
  66      * Go the the next valid element index of the iterator
  67      * @return next index
  68      */
  69     protected long bumpIndex() {
  70         return index++;
  71     }
  72 
  73     /**
  74      * Return the next valid element index of the iterator
  75      * @return next index
  76      */
  77     public long nextIndex() {
  78         return index;
  79     }
  80 
  81     @Override
  82     public void remove() {
  83         throw new UnsupportedOperationException("remove");
  84     }
  85 
  86     /**
  87      * Get the length of the iteration
  88      * @return length
  89      */
  90     public abstract long getLength();
  91 
  92     /**
  93      * ArrayLikeIterator factory
  94      *
  95      * @param object object over which to do element iteration
  96      * @return iterator
  97      */
  98     public static ArrayLikeIterator<Object> arrayLikeIterator(final Object object) {
  99         return arrayLikeIterator(object, false);
 100     }
 101 
 102     /**
 103      * ArrayLikeIterator factory (reverse order)
 104      * @param object object over which to do reverse element iteration
 105      * @return iterator
 106      */
 107     public static ArrayLikeIterator<Object> reverseArrayLikeIterator(final Object object) {
 108         return reverseArrayLikeIterator(object, false);
 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 }