Example usage for java.lang Iterable Iterable

List of usage examples for java.lang Iterable Iterable

Introduction

In this page you can find the example usage for java.lang Iterable Iterable.

Prototype

Iterable

Source Link

Usage

From source file:com.clxcommunications.xms.PagedFetcher.java

/**
 * Returns an iterable object that fetches and traverses all matching pages.
 * Each iteration will result in a network fetch.
 * <p>/*  w  w  w.  j ava 2s  .  c om*/
 * This iterator will always yield at least one page, which might be empty.
 * <p>
 * Since the returned iterator will perform asynchronous network traffic it
 * is possible that the {@link Iterator#next()} method throws
 * {@link RuntimeException} having as cause an {@link ExecutionException}.
 * 
 * @return a non-null iterable
 * @throws RuntimeApiException
 *             if the background page fetching failed
 */
@Nonnull
public Iterable<Page<T>> pages() {

    return new Iterable<Page<T>>() {

        @Override
        public Iterator<Page<T>> iterator() {

            return new Iterator<Page<T>>() {

                private Page<T> page = null;
                private int seenElements = 0;

                @Override
                public boolean hasNext() {
                    if (page == null) {
                        return true;
                    } else {
                        return seenElements < page.totalSize() && !page.isEmpty();
                    }
                }

                @Override
                public Page<T> next() {
                    int pageToFetch = (page == null) ? 0 : page.page() + 1;

                    try {
                        page = fetchAsync(pageToFetch, null).get();
                    } catch (InterruptedException e) {
                        // Interrupt the thread to let upstream code know.
                        Thread.currentThread().interrupt();
                    } catch (ExecutionException e) {
                        ApiException cause;

                        try {
                            cause = Utils.unwrapExecutionException(e);
                        } catch (ApiException einner) {
                            cause = einner;
                        }

                        throw new RuntimeApiException(cause);
                    }

                    seenElements += page.size();

                    return page;
                }

            };

        }

    };
}

From source file:oculus.aperture.common.JSONProperties.java

@Override
public Iterable<Object> getObjects(String key) {
    try {/*from  w  w w  .  j a v a2s  .  com*/
        final JSONArray array = obj.getJSONArray(key);

        return new Iterable<Object>() {
            @Override
            public Iterator<Object> iterator() {
                return new Iterator<Object>() {
                    private final int n = array.length();
                    private int i = 0;

                    @Override
                    public boolean hasNext() {
                        return n > i;
                    }

                    @Override
                    public Object next() {
                        try {
                            return (n > i) ? array.get(i++) : null;
                        } catch (JSONException e) {
                            return null;
                        }
                    }

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

    } catch (JSONException e) {
        return EmptyIterable.instance();
    }
}

From source file:org.echocat.jomon.resources.FileResource.java

@Nonnull
@Override//from   w  w  w.j  av a 2s .c o m
public Iterable<Entry<String, String>> getProperties() throws IOException {
    final Map<String, String> copy;
    synchronized (this) {
        // noinspection unchecked, RedundantCast
        copy = new HashMap<>((Map<String, String>) (Object) getPropertiesInternal());
    }
    return new Iterable<Entry<String, String>>() {
        @Override
        public Iterator<Entry<String, String>> iterator() {
            return new ConvertingIterator<Map.Entry<String, String>, Entry<String, String>>(
                    copy.entrySet().iterator()) {
                private final AtomicReference<String> _currentKey = new AtomicReference<>();

                @Override
                protected Entry<String, String> convert(Map.Entry<String, String> input) {
                    _currentKey.set(input.getKey());
                    return new Impl<>(input.getKey(), input.getValue());
                }

                @Override
                public void remove() {
                    final String key = _currentKey.get();
                    if (key != null) {
                        try {
                            removeProperty(key);
                        } catch (final IOException e) {
                            throw new RuntimeException("Could not remove " + key + ".", e);
                        }
                    }
                }
            };
        }
    };
}

From source file:org.apache.hadoop.metrics2.impl.MetricsConfig.java

Iterable<String> keys() {
    return new Iterable<String>() {
        @SuppressWarnings("unchecked")
        public Iterator<String> iterator() {
            return (Iterator<String>) getKeys();
        }//from   ww w  .  j a v a  2  s. c o  m
    };
}

From source file:com.zavakid.mushroom.impl.MetricsConfig.java

Iterable<String> keys() {
    return new Iterable<String>() {

        public Iterator<String> iterator() {
            return (Iterator<String>) getKeys();
        }/*from www  .  ja  v  a  2  s.  c o m*/
    };
}

From source file:de.micromata.genome.gwiki.model.mpt.GWikiCombinedPageCache.java

public Iterable<GWikiElementInfo> getPageInfos() {
    return new Iterable<GWikiElementInfo>() {

        public Iterator<GWikiElementInfo> iterator() {
            return new CombinedElementInfoIterator();
        }/* ww w. ja v a2s  . com*/
    };
}

From source file:com.freiheit.fuava.simplebatch.http.ConvertingHttpPagingFetcher.java

@Override
public Iterable<Result<FetchedItem<T>, T>> fetchAll() {
    return new Iterable<Result<FetchedItem<T>, T>>() {

        @Override//from ww  w  .  j av a 2 s  .  c om
        public Iterator<Result<FetchedItem<T>, T>> iterator() {
            final Iterator<Result<PagingInput, Iterable<Raw>>> iterator = new LazyPageFetchingIterable<Iterable<Raw>>(
                    new HttpPageFetcher<Iterable<Raw>>(fetcher, settings, converter), initialFrom, pageSize,
                    settings);
            return Iterators.concat(Iterators.transform(iterator, resultTransformer));
        }

    };

}

From source file:org.polymap.core.runtime.cache.Soft2Cache.java

public Iterable<V> values() {
    assert entries != null : "Cache is closed.";

    // the returned iterator should be aware off reclaimed entries
    // and needs to support remove()
    return new Iterable<V>() {
        public Iterator<V> iterator() {
            return new Iterator<V>() {

                private Iterator<CacheEntry<K, V>> it = entries.values().iterator();

                private CacheEntry<K, V> next;

                public boolean hasNext() {
                    // find next, un-reclaimed entry
                    while ((next == null || next.value() == null) && it.hasNext()) {
                        next = it.next();
                    }//from www. jav  a2 s.c om
                    return next != null;
                }

                public V next() {
                    assert next != null;
                    try {
                        return next.value();
                    } finally {
                        next = null;
                    }
                }

                public void remove() {
                    it.remove();
                }
            };
        }
    };
}

From source file:org.jiemamy.entity.io.meta.EntityMeta.java

/**
* ???{@link Iterable}??//from   ww  w .  j a va 2s. com
* 
* @return ???{@link Iterable}
*/
public Iterable<PropertyMeta> getAllPropertyMeta() {
    return new Iterable<PropertyMeta>() {

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

                private int i;

                public boolean hasNext() {
                    return i < propertyMetas.size();
                }

                public PropertyMeta next() {
                    return propertyMetas.get(i++);
                }

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

From source file:rita.compiler.CompileString.java

static Iterable<JavaSourceFromString> getJavaSourceFromString(String code) {
    final JavaSourceFromString jsfs;
    jsfs = new JavaSourceFromString(clazzName, code);
    return new Iterable<JavaSourceFromString>() {
        public Iterator<JavaSourceFromString> iterator() {
            return new Iterator<JavaSourceFromString>() {
                boolean isNext = true;

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

                public JavaSourceFromString next() {
                    if (!isNext)
                        throw new NoSuchElementException();
                    isNext = false;
                    return jsfs;
                }

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