Java Iterator from getIterator(final int[] p, final Object[] data)

Here you can find the source of getIterator(final int[] p, final Object[] data)

Description

Answer iterator, which iterates over specified data array according to the specified permutation, that is data[p[0]],..,data[p[data.length-1]]

License

Open Source License

Declaration

public static Iterator getIterator(final int[] p, final Object[] data) 

Method Source Code

//package com.java2s;
/*/*from  w  w  w  .  ja  va  2 s.  co  m*/
* Copyright 2004, 2005, 2006 Odysseus Software GmbH
*
* Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
*
* 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.
*/

import java.util.Iterator;
import java.util.List;

public class Main {
    /**
    * Answer iterator, which iterates over specified data array according
    * to the specified permutation, that is
    * <code>data[p[0]],..,data[p[data.length-1]]</code>
    */
    public static Iterator getIterator(final int[] p, final Object[] data) {
        return new Iterator() {
            int pos = 0;

            public boolean hasNext() {
                return pos < data.length;
            }

            public Object next() {
                return data[p[pos++]];
            }

            public void remove() {
                throw new UnsupportedOperationException("Cannot remove from immutable iterator!");
            }
        };
    }

    /**
    * Answer iterator, which iterates over specified data list according
    * to the specified permutation, that is
    * <code>data.get(p[0]),..,data.get(p[data.length-1])</code>
    */
    public static Iterator getIterator(final int[] p, final List data) {
        return new Iterator() {
            int pos = 0;

            public boolean hasNext() {
                return pos < data.size();
            }

            public Object next() {
                return data.get(p[pos++]);
            }

            public void remove() {
                throw new UnsupportedOperationException("Cannot remove from immutable iterator!");
            }
        };
    }
}

Related

  1. getEmptyIterator()
  2. getEnumeratedObjectCount(Iterator objects)
  3. getFirstChildrendIterator(List children)
  4. getIndexInIterator(Iterator iterator, int index)
  5. getIterable(final Iterator i)
  6. getIterator(final Object o, final Class classElement)
  7. getIterator(List list)
  8. getIterator(T[] array)
  9. getIteratorAsString(Iterator iter, final String separator)