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.internal.runtime.JSType;
  30 import jdk.nashorn.internal.runtime.ScriptObject;
  31 
  32 /**
  33  * Superclass for array iterators
  34  * TODO: rewrite these
  35  *
  36  * @param <T> element type
  37  */
  38 abstract public class ArrayLikeIterator<T> implements Iterator<T> {
  39 
  40     /** current element index in iteration */
  41     protected long index;
  42 
  43     /** should undefined elements be included in the iteration? */
  44     protected final boolean includeUndefined;
  45 
  46     /**
  47      * Constructor
  48      *
  49      * @param includeUndefined should undefined elements be included in the iteration?
  50      */
  51     protected ArrayLikeIterator(final boolean includeUndefined) {
  52         this.includeUndefined = includeUndefined;
  53         this.index = 0;
  54     }
  55 
  56     /**
  57      * Is this a reverse order iteration?
  58      * @return true if reverse
  59      */
  60     public boolean isReverse() {
  61         return false;
  62     }
  63 
  64     /**
  65      * Go the the next valid element index of the iterator
  66      * @return next index
  67      */
  68     protected long bumpIndex() {
  69         return index++;
  70     }
  71 
  72     /**
  73      * Return the next valid element index of the iterator
  74      * @return next index
  75      */
  76     public long nextIndex() {
  77         return index;
  78     }
  79 
  80     @Override
  81     public void remove() {
  82         throw new UnsupportedOperationException("remove");
  83     }
  84 
  85     /**
  86      * Get the length of the iteration
  87      * @return length
  88      */
  89     public abstract long getLength();
  90 
  91     /**
  92      * ArrayLikeIterator factory
  93      *
  94      * @param object object over which to do element iteration
  95      * @return iterator
  96      */
  97     public static ArrayLikeIterator<Object> arrayLikeIterator(final Object object) {
  98         return arrayLikeIterator(object, false);
  99     }
 100 
 101     /**
 102      * ArrayLikeIterator factory (reverse order)
 103      * @param object object over which to do reverse element iteration
 104      * @return iterator
 105      */
 106     public static ArrayLikeIterator<Object> reverseArrayLikeIterator(final Object object) {
 107         return reverseArrayLikeIterator(object, false);
 108     }
 109 
 110     /**
 111      * ArrayLikeIterator factory
 112      * @param object object over which to do reverse element iteration
 113      * @param includeUndefined should undefined elements be included in the iteration
 114      * @return iterator
 115      */
 116     public static ArrayLikeIterator<Object> arrayLikeIterator(final Object object, final boolean includeUndefined) {
 117         Object obj = object;
 118 
 119         if (ScriptObject.isArray(obj)) {
 120             return new ArrayIterator((ScriptObject) obj, includeUndefined);
 121         }
 122 
 123         obj = JSType.toScriptObject(obj);
 124         if (obj instanceof ScriptObject) {
 125             return new MapIterator((ScriptObject)obj, includeUndefined);
 126         }
 127 




 128         return new EmptyArrayLikeIterator();
 129     }
 130 
 131     /**
 132      * ArrayLikeIterator factory (reverse order)
 133      * @param object object over which to do reverse element iteration
 134      * @param includeUndefined should undefined elements be included in the iteration
 135      * @return iterator
 136      */
 137     public static ArrayLikeIterator<Object> reverseArrayLikeIterator(final Object object, final boolean includeUndefined) {
 138         Object obj = object;
 139 
 140         if (ScriptObject.isArray(obj)) {
 141             return new ReverseArrayIterator((ScriptObject) obj, includeUndefined);
 142         }
 143 
 144         obj = JSType.toScriptObject(obj);
 145         if (obj instanceof ScriptObject) {
 146             return new ReverseMapIterator((ScriptObject)obj, includeUndefined);




 147         }
 148 
 149         assert !obj.getClass().isArray();
 150 
 151         return new EmptyArrayLikeIterator();
 152     }
 153 
 154 }
--- EOF ---