Example usage for java.util Iterator Iterator

List of usage examples for java.util Iterator Iterator

Introduction

In this page you can find the example usage for java.util Iterator Iterator.

Prototype

Iterator

Source Link

Usage

From source file:org.apache.vxquery.context.StaticContextImpl.java

@Override
public Iterator<Function> listFunctions() {
    return new Iterator<Function>() {
        private Iterator<Function[]> faIter = functionMap.values().iterator();
        private Function[] fa = null;
        private int faIdx = 0;

        @Override/*from  w  w  w.ja va  2s. co  m*/
        public boolean hasNext() {
            fetchNext();
            return fa != null;
        }

        @Override
        public Function next() {
            fetchNext();
            if (fa == null) {
                throw new NoSuchElementException();
            }
            return fa[faIdx++];
        }

        private void fetchNext() {
            while (true) {
                if (fa != null && faIdx < fa.length) {
                    if (fa[faIdx] != null) {
                        break;
                    }
                    ++faIdx;
                } else {
                    if (faIter.hasNext()) {
                        fa = faIter.next();
                        faIdx = 0;
                    } else {
                        fa = null;
                        break;
                    }
                }
            }
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }
    };
}

From source file:edu.isi.wings.execution.engine.api.impl.pegasus.PegasusExecutionEngine.java

/**
 * Returns an iterator which can be used to traverse a file one line at a time.
 *
 * @param fileName/*from   ww w. j  av a 2 s . com*/
 * @param skip     Skip first <i>skip</i> lines
 * @return
 * @throws IOException
 */
public Iterator<String> lineIterator(String fileName, int skip) throws IOException {
    final BufferedReader r = new BufferedReader(new FileReader(new File(fileName)));

    for (int i = 0; i < skip; ++i) {
        String s = r.readLine();
        if (s == null) {
            break;
        }
    }

    Iterator<String> it = new Iterator<String>() {
        private String line = null;
        private boolean read = true;

        @Override
        public boolean hasNext() {
            try {
                if (read) {
                    line = r.readLine();
                    read = false;
                }

                if (line == null) {
                    close();
                }

                return line != null;

            } catch (IOException e) {
                close();
                return false;
            }
        }

        @Override
        public String next() {
            if (hasNext()) {
                read = true;
                return line;
            } else {
                return null;
            }
        }

        private void close() {
            if (r != null) {
                try {
                    r.close();
                } catch (IOException e) {
                    log.error(e.getMessage(), e);
                }
            }
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }
    };

    return it;
}

From source file:org.apache.openjpa.jdbc.sql.JoinSet.java

public Iterator iterator() {
    return new Iterator() {
        private Node _next = null;
        private int _idx = -1;

        public boolean hasNext() {
            if (_next != null)
                return true;

            while (++_idx < _graph.size()) {
                _next = (Node) _graph.get(_idx);
                while (_next != null && !_next.forward)
                    _next = _next.next;//from w w w .j  a  v  a2  s  .  c  o  m
                if (_next != null)
                    return true;
            }
            return false;
        }

        public Object next() {
            if (!hasNext())
                throw new NoSuchElementException();
            Join j = _next.join;
            do {
                _next = _next.next;
            } while (_next != null && !_next.forward);
            return j;
        }

        public void remove() {
            throw new UnsupportedOperationException();
        }
    };
}

From source file:SortUtils.java

/**
 * 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>
 *///from  w  w w. j a v a 2  s  . c  o  m
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!");
        }
    };
}

From source file:corner.orm.gae.impl.GaeCachedEntityServiceImpl.java

/**
 *
 * @see corner.orm.gae.impl.JpaEntityServiceImpl#paginate(java.lang.Class, java.lang.Object, java.lang.String, corner.orm.model.PaginationOptions)
 *///  w w  w  . j av a2  s.  c  om
@Override
public <T> PaginationList<T> paginate(final Class<T> persistClass, Object conditions, String order,
        PaginationOptions options) {
    PaginationList paginationList = super.paginate(persistClass, conditions, order, options);
    Object collectionObject = paginationList.collectionObject();
    final Iterator it = typeCoercer.coerce(collectionObject, Iterator.class);
    paginationList.overrideCollectionObject(new Iterator() {
        @Override
        public boolean hasNext() {
            return it.hasNext();
        }

        @Override
        public Object next() {
            Object id = it.next();
            return get(persistClass, id);
        }

        @Override
        public void remove() {
            it.remove();
        }
    });
    return paginationList;
}

From source file:com.linkedin.pinot.core.startree.StarTreeDataSorter.java

/**
 * Returns iterator over dimension and metric byte buffers.
 *
 * @param startDocId/*from w  w  w.ja  v  a  2s .  com*/
 * @param endDocId
 * @return
 * @throws IOException
 */
public Iterator<Pair<byte[], byte[]>> iterator(int startDocId, int endDocId) throws IOException {
    final int length = endDocId - startDocId;
    final int startOffset = startDocId * totalSizeInBytes;
    return new Iterator<Pair<byte[], byte[]>>() {
        int pointer = 0;

        @Override
        public boolean hasNext() {
            return pointer < length;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }

        @Override
        public Pair<byte[], byte[]> next() {
            byte[] dimBuff = new byte[dimensionSizeInBytes];
            byte[] metBuff = new byte[metricSizeInBytes];

            mappedByteBuffer.copyTo(startOffset + (pointer * totalSizeInBytes), dimBuff, 0,
                    dimensionSizeInBytes);
            if (metricSizeInBytes > 0) {
                mappedByteBuffer.copyTo(startOffset + (pointer * totalSizeInBytes) + dimensionSizeInBytes,
                        metBuff, 0, metricSizeInBytes);
            }

            pointer = pointer + 1;
            return Pair.of(dimBuff, metBuff);
        }
    };
}

From source file:org.apache.kylin.dict.lookup.cache.RocksDBLookupTableCacheTest.java

private static ILookupTable getLookupTableWithRandomData(final long rowNum) {
    return new ILookupTable() {
        Random random = new Random();

        @Override//w  ww.j av  a2  s. com
        public String[] getRow(Array<String> key) {
            return new String[0];
        }

        @Override
        public void close() throws IOException {

        }

        @Override
        public Iterator<String[]> iterator() {
            return new Iterator<String[]>() {
                private int iterCnt = 0;

                @Override
                public boolean hasNext() {
                    return iterCnt < rowNum;
                }

                @Override
                public String[] next() {
                    iterCnt++;
                    return genRandomRow(iterCnt);
                }

                @Override
                public void remove() {

                }
            };
        }

        private String[] genRandomRow(int id) {
            return new String[] { "keyyyyy" + id, String.valueOf(random.nextDouble()),
                    String.valueOf(random.nextDouble()), "Andorra" + id };
        }
    };
}

From source file:logicProteinHypernetwork.analysis.reactions.ComplexMultigraph.java

public Iterator<Protein> iterator() {
    return new Iterator<Protein>() {

        private Iterator<Entry<Integer, Protein>> entries = verticesToProteins.entrySet().iterator();

        public boolean hasNext() {
            return entries.hasNext();
        }//www.  j ava  2 s.c o m

        public Protein next() {
            return entries.next().getValue();
        }

        public void remove() {
            throw new UnsupportedOperationException("Not supported.");
        }
    };
}

From source file:com.conwet.silbops.model.Subscription.java

/**
 * Returns the set of attribute-constraint pairs
 *
 * @return the set of attribute-constraint pairs
 *//* w w  w  .  j  a  v a 2  s  .  co m*/
@Override
public Set<Entry<Attribute, Constraint>> entries() {

    return new AbstractSet<Entry<Attribute, Constraint>>() {

        @Override
        public Iterator<Entry<Attribute, Constraint>> iterator() {

            return new Iterator<Entry<Attribute, Constraint>>() {

                private Iterator<Entry<Attribute, Set<Constraint>>> attrIt = constraints.entrySet().iterator();
                private Iterator<Constraint> constrIt = null;
                private Attribute current = null;

                @Override
                public boolean hasNext() {

                    return attrIt.hasNext() || (constrIt != null && constrIt.hasNext());
                }

                @Override
                public Entry<Attribute, Constraint> next() {

                    if (current == null) {

                        Entry<Attribute, Set<Constraint>> entry = attrIt.next();
                        current = entry.getKey();
                        constrIt = entry.getValue().iterator();
                    }

                    final Attribute attr = current;
                    final Constraint cons = constrIt.next();

                    if (!constrIt.hasNext()) {
                        // set null to go to next attribute
                        current = null;
                    }

                    return new SimpleImmutableEntry<>(attr, cons);
                }

                @Override
                public void remove() {

                    throw new UnsupportedOperationException("Read-only iterator");
                }
            };
        }

        @Override
        public int size() {

            int size = 0;

            for (Set<Constraint> constrSet : constraints.values()) {

                size += constrSet.size();
            }

            return size;
        }
    };
}

From source file:io.druid.data.input.impl.PrefetchableTextFilesFirehoseFactory.java

@Override
public Firehose connect(StringInputRowParser firehoseParser, File temporaryDirectory) throws IOException {
    if (maxCacheCapacityBytes == 0 && maxFetchCapacityBytes == 0) {
        return super.connect(firehoseParser, temporaryDirectory);
    }// w w  w .j  a v a2  s.c o m

    if (objects == null) {
        objects = ImmutableList.copyOf(Preconditions.checkNotNull(initObjects(), "objects"));
    }

    Preconditions.checkState(temporaryDirectory.exists(), "temporaryDirectory[%s] does not exist",
            temporaryDirectory);
    Preconditions.checkState(temporaryDirectory.isDirectory(), "temporaryDirectory[%s] is not a directory",
            temporaryDirectory);

    // fetchExecutor is responsible for background data fetching
    final ExecutorService fetchExecutor = createFetchExecutor();

    return new FileIteratingFirehose(new Iterator<LineIterator>() {
        // When prefetching is enabled, fetchFiles and nextFetchIndex are updated by the fetchExecutor thread, but
        // read by both the main thread (in hasNext()) and the fetchExecutor thread (in fetch()). To guarantee that
        // fetchFiles and nextFetchIndex are updated atomically, this lock must be held before updating
        // them.
        private final Object fetchLock = new Object();
        private final LinkedBlockingQueue<FetchedFile> fetchFiles = new LinkedBlockingQueue<>();

        // Number of bytes currently fetched files.
        // This is updated when a file is successfully fetched or a fetched file is deleted.
        private final AtomicLong fetchedBytes = new AtomicLong(0);
        private final boolean cacheInitialized;
        private final boolean prefetchEnabled;

        private Future<Void> fetchFuture;
        private int cacheIterateIndex;
        // nextFetchIndex indicates which object should be downloaded when fetch is triggered.
        private int nextFetchIndex;

        {
            cacheInitialized = totalCachedBytes > 0;
            prefetchEnabled = maxFetchCapacityBytes > 0;

            if (cacheInitialized) {
                nextFetchIndex = cacheFiles.size();
            }
            if (prefetchEnabled) {
                fetchIfNeeded(totalCachedBytes);
            }
        }

        private void fetchIfNeeded(long remainingBytes) {
            if ((fetchFuture == null || fetchFuture.isDone()) && remainingBytes <= prefetchTriggerBytes) {
                fetchFuture = fetchExecutor.submit(() -> {
                    fetch();
                    return null;
                });
            }
        }

        /**
         * Fetch objects to a local disk up to {@link PrefetchableTextFilesFirehoseFactory#maxFetchCapacityBytes}.
         * This method is not thread safe and must be called by a single thread.  Note that even
         * {@link PrefetchableTextFilesFirehoseFactory#maxFetchCapacityBytes} is 0, at least 1 file is always fetched.
         * This is for simplifying design, and should be improved when our client implementations for cloud storages
         * like S3 support range scan.
         */
        private void fetch() throws Exception {
            for (int i = nextFetchIndex; i < objects.size()
                    && fetchedBytes.get() <= maxFetchCapacityBytes; i++) {
                final ObjectType object = objects.get(i);
                LOG.info("Fetching object[%s], fetchedBytes[%d]", object, fetchedBytes.get());
                final File outFile = File.createTempFile(FETCH_FILE_PREFIX, null, temporaryDirectory);
                fetchedBytes.addAndGet(download(object, outFile, 0));
                synchronized (fetchLock) {
                    fetchFiles.put(new FetchedFile(object, outFile));
                    nextFetchIndex++;
                }
            }
        }

        /**
         * Downloads an object. It retries downloading {@link PrefetchableTextFilesFirehoseFactory#maxFetchRetry}
         * times and throws an exception.
         *
         * @param object   an object to be downloaded
         * @param outFile  a file which the object data is stored
         * @param tryCount current retry count
         *
         * @return number of downloaded bytes
         *
         * @throws IOException
         */
        private long download(ObjectType object, File outFile, int tryCount) throws IOException {
            try (final InputStream is = openObjectStream(object);
                    final CountingOutputStream cos = new CountingOutputStream(new FileOutputStream(outFile))) {
                IOUtils.copy(is, cos);
                return cos.getCount();
            } catch (IOException e) {
                final int nextTry = tryCount + 1;
                if (!Thread.currentThread().isInterrupted() && nextTry < maxFetchRetry) {
                    LOG.error(e, "Failed to download object[%s], retrying (%d of %d)", object, nextTry,
                            maxFetchRetry);
                    outFile.delete();
                    return download(object, outFile, nextTry);
                } else {
                    LOG.error(e, "Failed to download object[%s], retries exhausted, aborting", object);
                    throw e;
                }
            }
        }

        @Override
        public boolean hasNext() {
            synchronized (fetchLock) {
                return (cacheInitialized && cacheIterateIndex < cacheFiles.size()) || !fetchFiles.isEmpty()
                        || nextFetchIndex < objects.size();
            }
        }

        @Override
        public LineIterator next() {
            if (!hasNext()) {
                throw new NoSuchElementException();
            }

            // If fetch() fails, hasNext() always returns true because nextFetchIndex must be smaller than the number
            // of objects, which means next() is always called. The below method checks that fetch() threw an exception
            // and propagates it if exists.
            checkFetchException();

            final OpenedObject openedObject;

            try {
                // Check cache first
                if (cacheInitialized && cacheIterateIndex < cacheFiles.size()) {
                    final FetchedFile fetchedFile = cacheFiles.get(cacheIterateIndex++);
                    openedObject = new OpenedObject(fetchedFile, getNoopCloser());
                } else if (prefetchEnabled) {
                    openedObject = openObjectFromLocal();
                } else {
                    openedObject = openObjectFromRemote();
                }

                final InputStream stream = wrapObjectStream(openedObject.object, openedObject.objectStream);

                return new ResourceCloseableLineIterator(new InputStreamReader(stream, Charsets.UTF_8),
                        openedObject.resourceCloser);
            } catch (IOException e) {
                throw Throwables.propagate(e);
            }
        }

        private void checkFetchException() {
            if (fetchFuture != null && fetchFuture.isDone()) {
                try {
                    fetchFuture.get();
                    fetchFuture = null;
                } catch (InterruptedException | ExecutionException e) {
                    throw Throwables.propagate(e);
                }
            }
        }

        private OpenedObject openObjectFromLocal() throws IOException {
            final FetchedFile fetchedFile;
            final Closeable resourceCloser;

            if (!fetchFiles.isEmpty()) {
                // If there are already fetched files, use them
                fetchedFile = fetchFiles.poll();
                resourceCloser = cacheIfPossibleAndGetCloser(fetchedFile, fetchedBytes);
                fetchIfNeeded(fetchedBytes.get());
            } else {
                // Otherwise, wait for fetching
                try {
                    fetchIfNeeded(fetchedBytes.get());
                    fetchedFile = fetchFiles.poll(fetchTimeout, TimeUnit.MILLISECONDS);
                    if (fetchedFile == null) {
                        // Check the latest fetch is failed
                        checkFetchException();
                        // Or throw a timeout exception
                        throw new RuntimeException(new TimeoutException());
                    }
                    resourceCloser = cacheIfPossibleAndGetCloser(fetchedFile, fetchedBytes);
                    // trigger fetch again for subsequent next() calls
                    fetchIfNeeded(fetchedBytes.get());
                } catch (InterruptedException e) {
                    throw Throwables.propagate(e);
                }
            }
            return new OpenedObject(fetchedFile, resourceCloser);
        }

        private OpenedObject openObjectFromRemote() throws IOException {
            final OpenedObject openedObject;
            final Closeable resourceCloser = getNoopCloser();

            if (totalCachedBytes < maxCacheCapacityBytes) {
                LOG.info("Caching object[%s]", objects.get(nextFetchIndex));
                try {
                    // Since maxFetchCapacityBytes is 0, at most one file is fetched.
                    fetch();
                    FetchedFile fetchedFile = fetchFiles.poll();
                    if (fetchedFile == null) {
                        throw new ISE("Cannot fetch object[%s]", objects.get(nextFetchIndex));
                    }
                    cacheIfPossible(fetchedFile);
                    fetchedBytes.addAndGet(-fetchedFile.length());
                    openedObject = new OpenedObject(fetchedFile, resourceCloser);
                } catch (Exception e) {
                    throw Throwables.propagate(e);
                }
            } else {
                final ObjectType object = objects.get(nextFetchIndex++);
                LOG.info("Reading object[%s]", object);
                openedObject = new OpenedObject(object, openObjectStream(object), resourceCloser);
            }
            return openedObject;
        }
    }, firehoseParser, () -> {
        fetchExecutor.shutdownNow();
        try {
            Preconditions.checkState(fetchExecutor.awaitTermination(fetchTimeout, TimeUnit.MILLISECONDS));
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new ISE("Failed to shutdown fetch executor during close");
        }
    });
}