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:io.mapzone.arena.csw.GetRecordsRequest.java

@Override
protected ResultSet<T> handleResponse(InputStream in, IProgressMonitor monitor) throws Exception {
    GetRecordsResponseXML response = readObject(in, GetRecordsResponseXML.class);

    return new ResultSet<T>() {
        SearchResultsXML results = response.searchResults;
        Iterator<? extends AbstractRecordXML> it = results.records.iterator();

        @Override/*from w ww .  j av a  2  s .  c  o m*/
        public Iterator<T> iterator() {
            return new Iterator<T>() {
                @Override
                public boolean hasNext() {
                    return it.hasNext();
                }

                @Override
                public T next() {
                    return (T) it.next();
                }
            };
        }

        @Override
        public int size() {
            return results.numberOfRecordsReturned.intValue();
        }

        @Override
        public void close() {
        }
    };
}

From source file:com.rackspacecloud.client.service_registry.clients.BaseClient.java

protected <T extends HasId> Iterator<T> getListIterator(final Class<T> clazz, final String path,
        final MethodOptions methodOptions, final HttpRequestBase method, final boolean parseAsJson,
        final Type type) {
    return new Iterator<T>() {

        // means we are done fetching, not done iterating.
        private boolean exhausted = false;

        private List<T> curValues;
        private String nextMarker = methodOptions.getMarker();

        // NOTE: The groundwork has been laid to let methodOptions.getLimit() become a limit for the number of total
        //       results returned. We can use any value we want for the page size now.  As for now,
        //       methodOptions.getLimit() refers to the page size, which may not be intuitive.
        // these are all the keys we are interested in when paging.
        private Set<String> constantPagingParams = Collections.unmodifiableSet(new HashSet<String>() {
            {/*from   w ww . ja  va 2 s  .  co  m*/
                addAll(methodOptions.getNonNullKeys());
                remove(MethodOptions.PaginationOptions.Marker.toString());
            }
        });

        public boolean hasNext() {
            if (this.curValues == null && !this.exhausted) {
                this.curValues = getNextPage();
            }
            return this.curValues != null && this.curValues.size() > 0;
        }

        public T next() {
            T item = this.curValues.remove(0);
            if (this.curValues.size() == 0 && !this.exhausted) {
                this.curValues = this.getNextPage();
            }
            return item;
        }

        public void remove() {
            throw new RuntimeException("Not implemented");
        }

        private List<T> getNextPage() {
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            // backfill everything from options into params except "marker". We handle that manually.
            for (String key : constantPagingParams) {
                params.add(new BasicNameValuePair(key, methodOptions.get(key)));
            }

            if (this.nextMarker != null) {
                params.add(new BasicNameValuePair("marker", this.nextMarker));
            }

            try {
                ClientResponse response = performRequest(path, params, method, parseAsJson, type);
                ContainerMeta<T> container = (ContainerMeta<T>) response.getBody();
                List<T> values = container.getValues();
                if (container.getNextMarker() == null) {
                    this.exhausted = true;
                } else {
                    nextMarker = container.getNextMarker();
                }

                return values;
            } catch (Exception ex) {
                // be careful about throwing, you're in an iterator.
                this.exhausted = true;
                return new ArrayList<T>();
            }

        }
    };
}

From source file:org.apache.ode.axis2.Axis2TestBase.java

@DataProvider(name = "configs")
protected Iterator<Object[]> createConfigData() {
    List<String> configDirList = new ArrayList<String>();
    if (!(this instanceof ODEConfigDirAware) || ((ODEConfigDirAware) this).getODEConfigDir().contains("hib")) {
        addToConfigDirList(configDirList, "org.apache.ode.hibdbs");
    }/* w w w  .  j a va  2  s .  c o  m*/
    if (!(this instanceof ODEConfigDirAware) || !((ODEConfigDirAware) this).getODEConfigDir().contains("hib")) {
        addToConfigDirList(configDirList, "org.apache.ode.jpadbs");
    }

    if (configDirList.isEmpty()) {
        // if no system property is set, fall back to default
        if (this instanceof ODEConfigDirAware) {
            configDirList.add(((ODEConfigDirAware) this).getODEConfigDir());
        } else {
            configDirList.add(DO_NOT_OVERRIDE_CONFIG);
        }
    } else {
        System.out.println(
                "Java system properties have been set to override ode configuration: " + configDirList);
    }

    final Iterator<String> itr = configDirList.iterator();
    return new Iterator<Object[]>() {
        public boolean hasNext() {
            return itr.hasNext();
        }

        public Object[] next() {
            config = itr.next();
            return new Object[] {};
        }

        public void remove() {
        }
    };
}

From source file:UnifyHash.java

public Iterator iterateHashCode(final int hash) {
    ///#ifdef JDK12
    cleanUp();//from   w  w w  . j a va  2 s .c  om
    ///#endif
    return new Iterator() {
        private int known = modCount;
        private Bucket nextBucket = buckets[Math.abs(hash % buckets.length)];
        private Object nextVal;

        {
            internalNext();
        }

        private void internalNext() {
            while (nextBucket != null) {
                if (nextBucket.hash == hash) {
                    nextVal = nextBucket.get();
                    if (nextVal != null)
                        return;
                }

                nextBucket = nextBucket.next;
            }
        }

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

        public Object next() {
            if (known != modCount)
                throw new ConcurrentModificationException();
            if (nextBucket == null)
                throw new NoSuchElementException();
            Object result = nextVal;
            nextBucket = nextBucket.next;
            internalNext();
            return result;
        }

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

From source file:org.carracoo.maven.assist.AssistProcessor.java

protected Iterator<String> iterateClassnames(final String dir) {

    return new Iterator<String>() {
        final String[] extensions = { "class" };
        final Iterator<File> classFiles = FileUtils.iterateFiles(new File(dir), extensions, true);

        @Override/* w w  w  .j  a  v  a  2  s.co m*/
        public boolean hasNext() {
            return classFiles.hasNext();
        }

        @Override
        public String next() {
            final File classFile = classFiles.next();
            try {
                final String qualifiedFileName = classFile.getCanonicalPath().substring(dir.length() + 1);
                return removeExtension(qualifiedFileName.replace(File.separator, "."));
            } catch (final IOException e) {
                throw new RuntimeException(e.getMessage());
            }
        }

        @Override
        public void remove() {
            classFiles.remove();
        }
    };
}

From source file:org.broadinstitute.gatk.utils.MathUtilsUnitTest.java

@DataProvider(name = "log1mexpData")
public Iterator<Object[]> log1mexpData() {

    final double[] inValues = new double[] { Double.NaN, 10, 1, 0, -1, -3, -10, -30, -100, -300, -1000, -3000 };
    return new Iterator<Object[]>() {

        private int i = 0;

        @Override/*  w w  w . j a  va 2  s . c  o  m*/
        public boolean hasNext() {
            return i < inValues.length;

        }

        @Override
        public Object[] next() {
            final double input = inValues[i++];
            final double output = Math.log(1 - Math.exp(input));
            return new Object[] { input, output };
        }

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

From source file:org.cinchapi.concourse.util.ConcurrentSkipListMultiset.java

@Override
public Iterator<T> iterator() {
    return new Iterator<T>() {
        // This implementation is adapted from that in Guava's
        // MultisetIteratorImpl class

        private boolean canRemove;
        private Entry<T> currentEntry;

        private final Iterator<ConcurrentSkipListMultiset<T>.SkipListEntry> entryIterator = ConcurrentSkipListMultiset.this.backing
                .values().iterator();/*from w ww. j  a  va  2s  .c om*/
        /** Count of subsequent elements equal to current element */
        private int laterCount;
        private final Multiset<T> multiset = ConcurrentSkipListMultiset.this;
        /** Count of all elements equal to current element */
        private int totalCount;

        @Override
        public boolean hasNext() {
            return laterCount > 0 || entryIterator.hasNext();
        }

        @Override
        public T next() {
            if (!hasNext()) {
                throw new NoSuchElementException();
            }
            if (laterCount == 0) {
                currentEntry = entryIterator.next();
                totalCount = laterCount = currentEntry.getCount();
            }
            laterCount--;
            canRemove = true;
            return currentEntry.getElement();
        }

        @Override
        public void remove() {
            Preconditions.checkState(canRemove, "no calls to next() since the last call to remove()");
            if (totalCount == 1) {
                entryIterator.remove();
            } else {
                multiset.remove(currentEntry.getElement());
            }
            totalCount--;
            canRemove = false;
        }
    };
}

From source file:MicroMap.java

/**
 * @see java.util.Map#entrySet()/*from  w  w w.ja v a 2s  . c  o m*/
 */
public Set entrySet() {
    return new AbstractSet() {
        public Iterator iterator() {
            return new Iterator() {
                public boolean hasNext() {
                    return index < MicroMap.this.size();
                }

                public Object next() {
                    index++;

                    return new Map.Entry() {
                        public Object getKey() {
                            return key;
                        }

                        public Object getValue() {
                            return value;
                        }

                        public Object setValue(final Object value) {
                            final Object oldValue = MicroMap.this.value;

                            MicroMap.this.value = value;

                            return oldValue;
                        }
                    };
                }

                public void remove() {
                    clear();
                }

                int index = 0;
            };
        }

        public int size() {
            return MicroMap.this.size();
        }
    };
}

From source file:clearfacts.cds.JsonDataSource.java

public void moveFirst() throws JRException {
    if (jsonTree == null || jsonTree.isMissingNode()) {
        throw new JRException("No JSON data to operate on!");
    }/*from ww w  .j a  v  a 2 s .c  om*/

    currentJsonNode = null;
    JsonNode result = getJsonData(jsonTree, selectExpression);
    if (result != null && result.isObject()) {
        final List<JsonNode> list = new ArrayList<JsonNode>();
        list.add(result);
        jsonNodesIterator = new Iterator<JsonNode>() {
            private int count = -1;

            public void remove() {
                list.remove(count);
            }

            public JsonNode next() {
                count++;
                return list.get(count);
            }

            public boolean hasNext() {
                return count < list.size() - 1;
            }
        };
    } else if (result != null && result.isArray()) {
        jsonNodesIterator = result.elements();
    }
}

From source file:com.daveoxley.cbus.Response.java

public Iterator<String> iterator() {
    return new Iterator<String>() {
        private int index = 0;

        public boolean hasNext() {
            synchronized (iterator_mutex) {
                while (array_response == null || (index >= array_response.size() && !response_generated)) {
                    try {
                        iterator_mutex.wait();
                    } catch (InterruptedException ie) {
                    }/* ww  w  . j  a  va2s .  c  om*/
                }

                if (index < array_response.size())
                    return true;

                if (response_generated)
                    return false;

                throw new IllegalStateException("Impossible");
            }
        }

        public String next() {
            synchronized (iterator_mutex) {
                if (index >= array_response.size())
                    throw new NoSuchElementException();

                return array_response.get(index++);
            }
        }

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