Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 1997-2013, www.tinygroup.org (luo_guo@icloud.com). Licensed under the GPL, Version 3.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.gnu.org/licenses/gpl.html Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package org.tinygroup.template.rumtime;
 
 
 import java.util.*;
 
 
 public final class ForIterator implements IteratorForStatus {
     private Iterator iterator = null;
     private int index = 0;
     private int size = 0;
 
     public void setIndex(int index) {
         this. = index;
     }
 
     public void setSize(int size) {
         this. = size;
     }
 
     public ForIterator() {
     }
 
     public ForIterator(Object object) {
         if (null == object) {
         } else if (object instanceof Map) {
             Map map = (Mapobject;
              = map.entrySet().iterator();
              = map.size();
         } else if (object instanceof Collection) {
             Collection collection = (Collectionobject;
              = collection.iterator();
              = collection.size();
         } else if (object instanceof Enumeration) {
             Enumeration enumeration = (Enumerationobject;
             ArrayList<?> list = Collections.list(enumeration);
              = list.iterator();
              = list.size();
         } else if (object.getClass().isEnum()) {
             List<?> itemList = Arrays.asList(((Class<?>) object).getEnumConstants());
              = itemList.iterator();
              = itemList.size();
         } else if (object.getClass().isArray()) {
              = new ArrayIterator(object);
              = ArrayUtil.arrayLength(object);
         } else {
              = new SingletonIterator(object);
              = 1;
         }
     }
 
     public boolean hasNext() {
         if ( == null) {
             return false;
         }
         return .hasNext();
     }
 
     public Object next() {
         ++;
         return .next();
     }
 
     public void remove() {
         throw new UnsupportedOperationException();
     }
 
     public int getIndex() {
         return ;
     }
 
 
     public int getSize() {
         return ;
     }
 
 
     public boolean isFirst() {
         return  == 1;
     }
 
 
     public boolean isLast() {
        return !.hasNext();
    }
    public boolean isOdd() {
        return ( & 2) != 0;
    }
    public boolean isEven() {
        return ( % 2) == 0;
    }


    
Created by luoguo on 2014/6/5.
    static class SingletonIterator implements Iterator {
        private Object object;
        public SingletonIterator(Object object) {
            this. = object;
        }
        public boolean hasNext() {
            return  != null;
        }
        public Object next()  {
            Object result = ;
             = null;
            return result;
        }
        public void remove() {
            throw new UnsupportedOperationException();
        }
    }
    static class ArrayIterator implements Iterator {
        private final Object object;
        private int size = 0;
        private int index = 0;
        public ArrayIterator(Object object) {
            this. = object;
             = ArrayUtil.arrayLength(object);
        }
        public boolean hasNext() {
            return  < ;
        }
        public Object next()  {
            return Array.get(++);
        }
        public void remove() {
            throw new UnsupportedOperationException();
        }
    }
New to GrepCode? Check out our FAQ X