001/*
002 * Copyright (c) 2004-2013 Tada AB and other contributors, as listed below.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the The BSD 3-Clause License
006 * which accompanies this distribution, and is available at
007 * http://opensource.org/licenses/BSD-3-Clause
008 *
009 * Contributors:
010 *   Tada AB
011 */
012package org.postgresql.pljava.example;
013
014import java.sql.SQLException;
015import java.util.Iterator;
016import java.util.NoSuchElementException;
017import java.util.Random;
018
019/**
020 * Provides a {@link #createIterator function} producing as many rows as
021 * requested, each with a random int.
022 */
023public class RandomInts implements Iterator<Integer> {
024    public static Iterator<Integer> createIterator(int rowCount)
025            throws SQLException {
026        return new RandomInts(rowCount);
027    }
028
029    private final int m_rowCount;
030    private final Random m_random;
031
032    private int m_currentRow;
033
034    public RandomInts(int rowCount) throws SQLException {
035        m_rowCount = rowCount;
036        m_random = new Random(System.currentTimeMillis());
037    }
038
039    @Override
040    public boolean hasNext() {
041        return m_currentRow < m_rowCount;
042    }
043
044    @Override
045    public Integer next() {
046        if (m_currentRow < m_rowCount) {
047            ++m_currentRow;
048            return Integer.valueOf(m_random.nextInt());
049        }
050        throw new NoSuchElementException();
051    }
052
053    @Override
054    public void remove() {
055        throw new UnsupportedOperationException();
056    }
057}