001    // Copyright 2004, 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.contrib.table.model.common;
016    
017    import java.util.Iterator;
018    import java.util.NoSuchElementException;
019    
020    /**
021     * @author mindbridge
022     */
023    public class ArrayIterator implements Iterator
024    {
025            private Object[] m_arrValues;
026            private int m_nFrom;
027            private int m_nTo;
028            private int m_nCurrent;
029    
030            public ArrayIterator(Object[] arrValues)
031            {
032                    this(arrValues, 0, arrValues.length);
033            }
034    
035            public ArrayIterator(Object[] arrValues, int nFrom, int nTo)
036            {
037                    m_arrValues = arrValues;
038                    m_nFrom = nFrom;
039                    m_nTo = nTo;
040    
041                    if (m_nFrom < 0)
042                            m_nFrom = 0;
043                    if (m_nTo < m_nFrom)
044                            m_nTo = m_nFrom;
045                    if (m_nTo > m_arrValues.length)
046                            m_nTo = m_arrValues.length;
047    
048                    m_nCurrent = m_nFrom;
049            }
050    
051            /**
052             * @see java.util.Iterator#hasNext()
053             */
054            public boolean hasNext()
055            {
056                    return m_nCurrent < m_nTo;
057            }
058    
059            /**
060             * @see java.util.Iterator#next()
061             */
062            public Object next()
063            {
064                    //System.out.println("index: " + m_nCurrent + "   size: " + m_arrValues.length + "  to: " + m_nTo);
065                    if (!hasNext())
066                            throw new NoSuchElementException();
067                    return m_arrValues[m_nCurrent++];
068            }
069    
070            /**
071             * @see java.util.Iterator#remove()
072             */
073            public void remove()
074            {
075                    throw new UnsupportedOperationException();
076            }
077    
078    }