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:com.spotify.heroic.metric.TagValues.java

public Iterator<Map.Entry<String, String>> iterator() {
    final Iterator<String> values = this.values.iterator();

    return new Iterator<Map.Entry<String, String>>() {
        @Override/*from   w  w  w .  j  av  a 2 s.  co  m*/
        public boolean hasNext() {
            return values.hasNext();
        }

        @Override
        public Map.Entry<String, String> next() {
            return Pair.of(key, values.next());
        }
    };
}

From source file:org.dbg4j.core.DebugUtils.java

/**
 * Get method signature in format <code>ReturnType ClassName:MethodName(&lt;parameter types&gt;)</code>
 *
 * @param method//from   ww w  .j av a  2s .co  m
 * @return
 */
@Nonnull
public static String getMethodSignature(@Nonnull final Method method) {
    StringBuilder sb = new StringBuilder(method.getReturnType().getSimpleName()).append(" ")
            .append(method.getName()).append("(");
    if (method.getParameterTypes() != null && method.getParameterTypes().length > 0) {
        sb.append(StringUtils.join(new Iterator<String>() {
            private int i = 0;

            @Override
            public boolean hasNext() {
                return method.getParameterTypes() != null && i < method.getParameterTypes().length;
            }

            @Override
            public String next() {
                return method.getParameterTypes()[i++].getSimpleName();
            }

            @Override
            public void remove() {
            }
        }, ", "));
    }
    return sb.append(")").toString();
}

From source file:com.simplymeasured.prognosticator.HiveJDBCQueryImpl.java

@Override
public Iterator<Map<String, Object>> runQuery(String resultTable, String queryStatement,
        Map<String, Object> parameters) {
    final ArrayBlockingQueue<Map<String, Object>> rowQueue = new ArrayBlockingQueue<Map<String, Object>>(1000);

    ThreadedQueryRunnable runnable = new ThreadedQueryRunnable(dataSource, queryStatement, parameters,
            rowQueue);/*from  w w  w .  j  a v a  2 s .  c  o m*/

    executorService.submit(runnable);

    return new Iterator<Map<String, Object>>() {
        private boolean done = false;
        private Map<String, Object> cachedRow = null;
        private final Map<String, Object> emptyMap = Collections.emptyMap();

        @Override
        public boolean hasNext() {
            try {
                if (done)
                    return false;

                cachedRow = rowQueue.take();

                if (cachedRow == null || cachedRow == emptyMap) {
                    done = true;
                    return false;
                }

                return true;
            } catch (InterruptedException ie) {
                throw new RuntimeException("Iterator thread killed!", ie);
            }
        }

        @Override
        public Map<String, Object> next() {
            if (done || cachedRow == emptyMap) {
                throw new IllegalStateException("End of iterator reached");
            } else if (cachedRow == null) {
                boolean hasMore = hasNext();

                if (!hasMore) {
                    done = true;
                    throw new IllegalStateException("End of iterator reached");
                }
            }

            return cachedRow;
        }

        @Override
        public void remove() {
            // intentionally non-op
        }
    };
}

From source file:org.codehaus.plexus.archiver.tar.PlexusIoTarFileResourceCollection.java

protected Iterator<PlexusIoResource> getEntries() throws IOException {
    final File f = getFile();
    if (f == null) {
        throw new IOException("The tar archive file has not been set.");
    }/*from www .j  ava2s .  c  om*/
    if (tarFile == null)
        tarFile = newTarFile(f);
    final Enumeration en = tarFile.getEntries();
    return new Iterator<PlexusIoResource>() {
        public boolean hasNext() {
            return en.hasMoreElements();
        }

        public PlexusIoResource next() {
            final TarArchiveEntry entry = (TarArchiveEntry) en.nextElement();
            return entry.isSymbolicLink() ? new TarSymlinkResource(tarFile, entry)
                    : new TarResource(tarFile, entry);
        }

        public void remove() {
            throw new UnsupportedOperationException("Removing isn't implemented.");
        }
    };
}

From source file:org.artifactory.common.wicket.component.table.groupable.GroupedItemsStrategy.java

@Override
public <T> Iterator<Item<T>> getItems(final IItemFactory<T> factory, final Iterator<IModel<T>> newModels,
        Iterator<Item<T>> existingItems) {
    return new Iterator<Item<T>>() {
        private int index = 0;

        private Object lastGroupValue;
        private Item<T> lastGroupItem;
        private IModel<T> lastGroupModel;

        @Override/*from  w  ww .  j  a  v a 2 s  .  c  o  m*/
        public void remove() {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean hasNext() {
            return lastGroupModel != null || newModels.hasNext();
        }

        @Override
        @SuppressWarnings({ "unchecked" })
        public Item<T> next() {
            // returned group item in last iteration, return saved model item
            if (lastGroupModel != null) {
                Item<T> item = newRowItem(lastGroupModel);
                lastGroupModel = null;
                return item;
            }

            IModel<T> model = newModels.next();
            GroupableDataProvider provider = table.getGroupableDataProvider();
            SortParam groupParam = provider.getGroupParam();
            if (groupParam != null && model != null) {
                String property = groupParam.getProperty();
                IChoiceRenderer renderer = provider.getGroupRenderer(property);
                T modelObject = model.getObject();
                Object value = renderer.getIdValue(modelObject, index);
                if (!value.equals(lastGroupValue)) {
                    lastGroupValue = value;
                    lastGroupModel = model;
                    GroupableTable modificationTable = (GroupableTable) SerializationUtils.clone(table);
                    lastGroupItem = modificationTable.newGroupRowItem("group" + index, index, model);
                    Item cellItem = modificationTable.newGroupCellItem("cells", 0, model);
                    lastGroupItem.add(cellItem);
                    modificationTable.populateGroupItem(cellItem, "cell", property, model);
                    return lastGroupItem;
                }
            }
            return newRowItem(model);
        }

        @SuppressWarnings({ "unchecked" })
        private Item<T> newRowItem(IModel<T> model) {
            Item<T> item = factory.newItem(index, model);
            if (lastGroupItem != null && !table.isGroupExpanded(lastGroupItem)
                    && table.getGroupableDataProvider().getGroupParam() != null) {
                item.add(new CssClass("row-collapsed"));
            }
            index++;
            return item;
        }
    };
}

From source file:org.apache.kylin.storage.hbase.coprocessor.endpoint.HbaseServerKVIterator.java

@Override
public Iterator<Pair<ImmutableBytesWritable, ImmutableBytesWritable>> iterator() {
    return new Iterator<Pair<ImmutableBytesWritable, ImmutableBytesWritable>>() {

        ImmutableBytesWritable key = new ImmutableBytesWritable();
        ImmutableBytesWritable value = new ImmutableBytesWritable();
        Pair<ImmutableBytesWritable, ImmutableBytesWritable> pair = new Pair<>(key, value);

        private boolean hasMore = true;

        @Override//w  w  w . ja v a 2 s.c  o m
        public boolean hasNext() {
            return hasMore;
        }

        @Override
        public Pair<ImmutableBytesWritable, ImmutableBytesWritable> next() {
            if (hasNext()) {
                try {
                    hasMore = innerScaner.nextRaw(results);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }

                if (results.size() < 1)
                    throw new IllegalStateException("Hbase row contains less than 1 cell");

                Cell c = results.get(0);
                key.set(c.getRowArray(), c.getRowOffset(), c.getRowLength());
                value.set(c.getValueArray(), c.getValueOffset(), c.getValueLength());

                results.clear();
                return pair;
            } else {
                return null;
            }
        }

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

From source file:net.ssehub.easy.instantiation.rt.core.model.confModel.IDecisionVariableIdentifier.java

@Override
protected Iterator<String> getIDIterator(final String id) {
    return new Iterator<String>() {
        private String[] segments = id.split(SEPARATOR);
        private int index = 1;

        @Override//ww  w .  j  av  a 2s. com
        public boolean hasNext() {
            return segments.length > index;
        }

        @Override
        public String next() {
            String id;
            if (1 == index) {
                id = segments[0] + SEPARATOR + segments[1];
                index++;
            } else {
                id = segments[index++];
            }

            return id;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException(
                    "Removing segments are not supported. Tried this on: " + id);
        }
    };
}

From source file:org.datagator.api.client.SpooledRowBuffer.java

@Override
public Iterator<Object[]> iterator() {
    if (rowsCount <= cacheLimit) {
        return this.cache.iterator();
    } else {//from   w  w w  . j a  v a2  s .co  m
        try {
            flush();
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
        return new Iterator<Object[]>() {

            private final JsonParser jp;
            private int rowIndex = 0;
            private final TypeReference<ArrayList<Object>> tr = new TypeReference<ArrayList<Object>>() {
            };

            {
                try {
                    // TODO lock cache file
                    cacheFile.seek(0);
                    FileReader reader = new FileReader(cacheFile.getFD());
                    jp = Entity.json.createParser(reader);
                    JsonToken token = jp.nextToken(); // START_ARRAY
                    if (!token.equals(JsonToken.START_ARRAY)) {
                        throw new RuntimeException("Corrupted cache file");
                    }
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
            }

            @Override
            public boolean hasNext() {
                return rowIndex < rowsCount;
            }

            @Override
            public Object[] next() {
                if (!hasNext()) {
                    throw new NoSuchElementException("No such elememnt.");
                }
                try {
                    rowIndex += 1;
                    ArrayList<Object> buffer = jp.readValueAs(tr);
                    return buffer.toArray();
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
            }

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

        };
    }
}

From source file:org.apache.kylin.job.hadoop.invertedindex.IICLI.java

public static Iterable<IIRow> readSequenceKVs(Configuration hconf, String path) throws IOException {
    final Reader reader = new Reader(hconf, SequenceFile.Reader.file(new Path(path)));
    return new Iterable<IIRow>() {
        @Override//w  w w  . java2s  . c  om
        public Iterator<IIRow> iterator() {
            return new Iterator<IIRow>() {
                ImmutableBytesWritable k = new ImmutableBytesWritable();
                ImmutableBytesWritable v = new ImmutableBytesWritable();
                IIRow pair = new IIRow(k, v, null);

                @Override
                public boolean hasNext() {
                    boolean hasNext = false;
                    try {
                        hasNext = reader.next(k, v);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    } finally {
                        if (hasNext == false) {
                            IOUtils.closeQuietly(reader);
                        }
                    }
                    return hasNext;
                }

                @Override
                public IIRow next() {
                    return pair;
                }

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

From source file:com.google.jenkins.plugins.persistentmaster.volume.zip.ZipExtractor.java

@Override
public Iterator<Volume.Entry> iterator() {
    final Enumeration<ZipArchiveEntry> entriesInPhysicalOrder = zipFile.getEntriesInPhysicalOrder();
    return new Iterator<Volume.Entry>() {
        @Override// ww w.  ja va2s .c o m
        public boolean hasNext() {
            Preconditions.checkState(!closed, "Volume closed");
            return entriesInPhysicalOrder.hasMoreElements();
        }

        @Override
        public Volume.Entry next() {
            Preconditions.checkState(!closed, "Volume closed");
            // nextElement() will throw NoSuchElementException if no next
            // element exists
            return new ZipVolumeEntry(zipFile, entriesInPhysicalOrder.nextElement());
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException("remove is not supported by this iterator");
        }
    };
}