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.zkybase.api.mapper.ApplicationMapperTestCase.java

private void setUpTestObjects() {
    this.applicationEntityIterator = new Iterator<ApplicationNode>() {
        private int count = APP_COUNT;

        @Override//from  w  w w  .j a  v  a  2  s.  c o m
        public boolean hasNext() {
            return (count-- > 0);
        }

        @Override
        public ApplicationNode next() {
            return applicationEntity;
        }

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

    when(applicationEntities.iterator()).thenReturn(applicationEntityIterator);

    when(applicationEntity.getId()).thenReturn(APPLICATION_ID);
    when(applicationEntity.getName()).thenReturn(APPLICATION_NAME);

    when(applicationDto.getId()).thenReturn(APPLICATION_ID);
    when(applicationDto.getName()).thenReturn(APPLICATION_NAME);
}

From source file:com.netflix.astyanax.thrift.ThriftAllRowsImpl.java

/**
 * Each call to .iterator() returns a new context starting at the beginning
 * of the column family.//from w  w  w.  ja  v a 2 s . c  o  m
 */
@Override
public Iterator<Row<K, C>> iterator() {
    return new Iterator<Row<K, C>>() {
        private KeyRange range;
        private org.apache.cassandra.thrift.KeySlice lastRow;
        private List<org.apache.cassandra.thrift.KeySlice> list = null;
        private Iterator<org.apache.cassandra.thrift.KeySlice> iter = null;
        private boolean bContinueSearch = true;
        private boolean bIgnoreTombstones = true;

        {
            String startToken = query.getStartToken() == null ? partitioner.getMinToken()
                    : query.getStartToken();
            String endToken = query.getEndToken() == null ? partitioner.getMaxToken() : query.getEndToken();

            range = new KeyRange().setCount(query.getBlockSize()).setStart_token(startToken)
                    .setEnd_token(endToken);

            if (query.getIncludeEmptyRows() == null) {
                if (query.getPredicate().isSetSlice_range()
                        && query.getPredicate().getSlice_range().getCount() == 0) {
                    bIgnoreTombstones = false;
                }
            } else {
                bIgnoreTombstones = !query.getIncludeEmptyRows();
            }
        }

        @Override
        public boolean hasNext() {
            // Get the next block
            while (iter == null || (!iter.hasNext() && bContinueSearch)) {
                if (lastRow != null) {
                    // Determine the start token for the next page
                    String token = partitioner.getTokenForKey(ByteBuffer.wrap(lastRow.getKey()));
                    if (query.getRepeatLastToken()) {
                        // Start token is non-inclusive
                        range.setStart_token(partitioner.getTokenMinusOne(token));
                    } else {
                        range.setStart_token(token);
                    }
                }

                // Get the next block of rows from cassandra, exit if none returned
                list = query.getNextBlock(range);
                if (list == null || list.isEmpty()) {
                    return false;
                }

                // Since we may trim tombstones set a flag indicating whether a complete
                // block was returned so we can know to try to fetch the next one
                bContinueSearch = (list.size() == query.getBlockSize());

                // Trim the list from tombstoned rows, i.e. rows with no columns
                iter = list.iterator();
                if (iter == null || !iter.hasNext()) {
                    return false;
                }

                KeySlice previousLastRow = lastRow;
                lastRow = Iterables.getLast(list);

                if (query.getRepeatLastToken() && previousLastRow != null) {
                    iter.next();
                    iter.remove();
                }

                if (iter.hasNext() && bIgnoreTombstones) {
                    // Discard any tombstones
                    while (iter.hasNext()) {
                        KeySlice row = iter.next();
                        if (row.getColumns().isEmpty()) {
                            iter.remove();
                        }
                    }

                    // Get the iterator again
                    iter = list.iterator();
                }
            }
            return iter.hasNext();
        }

        @Override
        public Row<K, C> next() {
            org.apache.cassandra.thrift.KeySlice row = iter.next();
            return new ThriftRowImpl<K, C>(columnFamily.getKeySerializer().fromBytes(row.getKey()),
                    ByteBuffer.wrap(row.getKey()), new ThriftColumnOrSuperColumnListImpl<C>(row.getColumns(),
                            columnFamily.getColumnSerializer()));
        }

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

From source file:com.bigdata.rdf.sail.webapp.client.EntityContentProvider.java

@Override
public Iterator<ByteBuffer> iterator() {
    try {//from ww  w  . j a  v  a 2  s .c  o  m
        final InputStream instr;

        if (m_entity.isStreaming()) {
            instr = m_entity.getContent();
        } else {
            // If Apache Entity does not stream then we need to double buffer to an output stream - not ideal
            final ByteArrayOutputStream streambuf = new ByteArrayOutputStream();
            m_entity.writeTo(streambuf);

            instr = new ByteArrayInputStream(streambuf.toByteArray());
        }

        return new Iterator<ByteBuffer>() {

            boolean eof = false;
            int bufindex = 0;

            @Override
            public boolean hasNext() {
                return !eof;
            }

            @Override
            public ByteBuffer next() {
                final byte[] buf = new byte[1024];
                try {
                    final int rdlen = instr.read(buf);

                    // System.out.println("Returning ByteBuffer[" + bufindex++ + "] length: " + rdlen);

                    if (rdlen == -1) {
                        eof = true;
                        return ByteBuffer.wrap(buf, 0, 0);
                    } else {
                        return ByteBuffer.wrap(buf, 0, rdlen);
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

            @Override
            public void remove() {
                throw new UnsupportedOperationException();
            }
        };
    } catch (Exception e) {
        throw new RuntimeException("Unexpected", e);
    }
}

From source file:de.zib.gndms.kit.configlet.PublishConfiglet.java

/**
 *  Updates {@link PublishConfiglet#publishingSites} with the newest publishers. Should be invoked by {@link PublishConfiglet#update}
 *//*from  w ww .  ja va2s .  c  om*/
private void configPublishingSites() {
    try {
        final ConfigProvider mapConfig = getMapConfig().getDynArrayOption("publishers");
        publishingSites = new Iterable<String>() {
            public Iterator<String> iterator() {
                final Iterator<String> keys = mapConfig.dynArrayKeys();
                return new Iterator<String>() {
                    public boolean hasNext() {
                        return keys.hasNext();
                    }

                    public String next() {
                        return mapConfig.getOption(keys.next(), "").trim();
                    }

                    public void remove() {
                        keys.remove();
                    }
                };
            }
        };
    } catch (ParseException e) {
        getLog().warn(e);
    } catch (MandatoryOptionMissingException e) {
        getLog().warn(e);
    }
}

From source file:CollectionUtilities.java

public static Iterator singletonIterator(final Object item) {
    return new Iterator() {
        private boolean gotItem = false;

        public boolean hasNext() {
            return !this.gotItem;
        }/*from w  w w . ja  v  a 2s .  c om*/

        public Object next() {
            if (this.gotItem) {
                throw new NoSuchElementException();
            }
            this.gotItem = true;
            return item;
        }

        public void remove() {
            if (!this.gotItem) {
                this.gotItem = true;
            } else {
                throw new NoSuchElementException();
            }
        }
    };
}

From source file:org.datalorax.populace.core.walk.inspector.ListInspector.java

private Iterator<RawElement> toRawElements(final List<Object> list) {
    final int size = list.size();

    return new Iterator<RawElement>() {
        int index = 0;

        @Override/*  w w  w . j  a  va  2  s .c om*/
        public boolean hasNext() {
            return index < size;
        }

        @Override
        public RawElement next() {
            if (index >= size) {
                throw new NoSuchElementException();
            }
            return new ListElement(index++, list);
        }
    };
}

From source file:org.dataconservancy.archive.impl.elm.fs.FsMetadataStore.java

public Iterable<Metadata> getAll(final String... types) {

    final List<String> accepted = Arrays.asList(types);

    return new Iterable<Metadata>() {

        @SuppressWarnings("unchecked")
        public Iterator<Metadata> iterator() {

            final Iterator<File> fileIterator = FileUtils.iterateFiles(new File(getBaseDir()), getSuffix(),
                    true);//from   ww  w  . j ava  2 s .c  o m

            return new Iterator<Metadata>() {

                private Metadata next;

                {
                    advanceNext();
                }

                public boolean hasNext() {
                    return next != null;
                }

                public Metadata next() {
                    Metadata toReturn = next;
                    advanceNext();
                    return toReturn;
                }

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

                private void advanceNext() {

                    while (fileIterator.hasNext()) {
                        File candidate = fileIterator.next();
                        Metadata m = new FsMetadata(candidate);
                        if (accepted.isEmpty() || accepted.contains(m.getType())) {
                            next = m;
                            return;
                        }
                    }
                    next = null;
                }
            };
        }
    };
}

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

@Override
public Firehose connect(StringInputRowParser firehoseParser, File temporaryDirectory) throws IOException {
    if (objects == null) {
        objects = ImmutableList.copyOf(Preconditions.checkNotNull(initObjects(), "initObjects"));
    }/*ww w.j av  a  2  s.  c  om*/
    final Iterator<T> iterator = objects.iterator();
    return new FileIteratingFirehose(new Iterator<LineIterator>() {
        @Override
        public boolean hasNext() {
            return iterator.hasNext();
        }

        @Override
        public LineIterator next() {
            if (!hasNext()) {
                throw new NoSuchElementException();
            }
            final T object = iterator.next();
            try {
                return IOUtils.lineIterator(wrapObjectStream(object, openObjectStream(object)), Charsets.UTF_8);
            } catch (Exception e) {
                LOG.error(e, "Exception reading object[%s]", object);
                throw Throwables.propagate(e);
            }
        }
    }, firehoseParser);
}

From source file:at.molindo.notify.model.BeanParams.java

@Override
public Iterator<ParamValue> iterator() {
    return new Iterator<ParamValue>() {

        private final Iterator<PropertyDescriptor> _iter = getDescriptorsIter();
        private ParamValue _next = findNext();

        private ParamValue findNext() {
            while (_iter.hasNext()) {
                PropertyDescriptor pd = _iter.next();
                if (pd.getWriteMethod() == null || pd.getReadMethod() == null) {
                    continue;
                }/* w w w  .ja va  2s  .c om*/
                Object value = invoke(pd.getReadMethod());
                if (value == null) {
                    continue;
                }
                return Param.p(pd.getPropertyType(), pd.getName()).paramValue(value);
            }
            return null;
        }

        @Override
        public boolean hasNext() {
            return _next != null;
        }

        @Override
        public ParamValue next() {
            if (!hasNext()) {
                throw new NoSuchElementException();
            }
            ParamValue next = _next;
            _next = findNext();
            return next;
        }

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

From source file:org.languagetool.dev.errorcorpus.PedlerCorpus.java

@Override
public Iterator<ErrorSentence> iterator() {
    return new Iterator<ErrorSentence>() {
        @Override//  w  w w  . j  a v a 2  s.c  om
        public boolean hasNext() {
            return pos < lines.size();
        }

        @Override
        public ErrorSentence next() {
            String line = lines.get(pos++);
            ErrorSentence sentence = getIncorrectSentence(line);
            return sentence;
        }

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