Example usage for com.google.common.collect Iterables limit

List of usage examples for com.google.common.collect Iterables limit

Introduction

In this page you can find the example usage for com.google.common.collect Iterables limit.

Prototype

public static <T> Iterable<T> limit(final Iterable<T> iterable, final int limitSize) 

Source Link

Document

Creates an iterable with the first limitSize elements of the given iterable.

Usage

From source file:forge.game.zone.PlayerZone.java

public CardCollectionView getCardsPlayerCanActivate(Player who) {
    CardCollectionView cl = getCards(false);
    boolean checkingForOwner = who == player;

    if (checkingForOwner && (is(ZoneType.Battlefield) || is(ZoneType.Hand))) {
        return cl;
    }/*w ww .  ja v a 2s  .c o  m*/

    // Only check the top card of the library
    Iterable<Card> cards = cl;
    if (is(ZoneType.Library)) {
        cards = Iterables.limit(cards, 1);
    }

    final Predicate<Card> filterPredicate = checkingForOwner ? new OwnCardsActivationFilter()
            : new AlienCardsActivationFilter();
    return CardLists.filter(cl, filterPredicate);
}

From source file:org.calrissian.mango.collect.CloseableIterables.java

/**
 * Creates a closeable iterable with the first {@code limitSize} elements of the given
 * closeable iterable. If the original closeable iterable does not contain that many
 * elements, the returned iterator will have the same behavior as the original
 * closeable iterable. The returned closeable iterable's iterator supports {@code remove()}
 * if the original iterator does.//from   ww w.  j  a  va  2s .  c  o m
 */
public static <T> CloseableIterable<T> limit(final CloseableIterable<T> iterable, final int limitSize) {
    return wrap(Iterables.limit(iterable, limitSize), iterable);
}

From source file:com.opengamma.integration.viewer.status.impl.ViewStatusResultAggregatorImpl.java

private ViewStatusModel aggregate(final List<ViewColumnType> columnTypes) {
    ArgumentChecker.notNull(columnTypes, "aggregations");

    if (columnTypes.isEmpty()) {
        return defaultModel();
    }/*from   www.  j  a  va2 s.c o  m*/

    Iterable<ViewColumnType> fixedColumnTypes = Iterables.limit(columnTypes, columnTypes.size() - 1);
    final Map<List<String>, Set<String>> fixedRow2Columns = Maps.newHashMap();

    for (ViewStatusKey viewStatusKey : _viewStatusResult.keySet()) {
        ViewStatusKeyBean viewStatusKeyBean = new ViewStatusKeyBean(viewStatusKey.getSecurityType(),
                viewStatusKey.getValueRequirementName(), viewStatusKey.getCurrency(),
                viewStatusKey.getTargetType());
        List<String> key = viewStatusKey(viewStatusKeyBean, fixedColumnTypes);

        Set<String> columnValues = fixedRow2Columns.get(key);
        if (columnValues == null) {
            columnValues = Sets.newHashSet();
            fixedRow2Columns.put(key, columnValues);
        }
        columnValues.addAll(
                viewStatusKey(viewStatusKeyBean, Collections.singletonList(Iterables.getLast(columnTypes))));
    }

    Set<String> extraColumns = getExtraColumns(fixedRow2Columns);

    List<List<String>> columnHeaders = makeHeaders(columnTypes, extraColumns);
    List<List<Object>> rowData = createRowData(fixedRow2Columns, extraColumns, columnTypes);

    return new SimpleViewStatusModel(columnHeaders, rowData, _viewStatusResult);
}

From source file:uk.ac.ebi.fg.annotare2.web.server.rpc.ApplicationDataServiceImpl.java

@Override
public ArrayList<ArrayDesignRef> getArrayDesignList(String query, int limit) {
    return newArrayList(Iterables.transform(Iterables.limit(arrayDesignList.getArrayDesigns(query), limit),
            new Function<ArrayExpress.ArrayDesign, ArrayDesignRef>() {
                @Nullable//from   w w  w . java 2  s . c  o m
                @Override
                public ArrayDesignRef apply(@Nullable ArrayExpress.ArrayDesign ad) {
                    return new ArrayDesignRef(ad.getAccession(), ad.getName());
                }
            }));
}

From source file:org.apache.mahout.knn.search.ProjectionSearch.java

public List<Vector> search(final Vector query, int n, int searchSize) {
    List<Vector> top = Lists.newArrayList();
    for (TreeSet<Vector> v : vectors) {
        Iterables.addAll(top, Iterables.limit(v.tailSet(query, true), searchSize));
        Iterables.addAll(top, Iterables.limit(v.headSet(query, false).descendingSet(), searchSize));
    }/*from  www  . j  av a 2  s . co  m*/
    System.out.print(top.size());
    removeDuplicate(top);
    System.out.print(" ");
    System.out.println(top.size());

    // if searchSize * vectors.size() is small enough not to cause much memory pressure, this is probably
    // just as fast as a priority queue here.
    Collections.sort(top, byQueryDistance(query));
    return top.subList(0, n);
}

From source file:com.github.mike10004.demo.infinitescroll.ItemServlet.java

List<Item> trim(Function<String, String> paramMap) throws ServletException, ExceptionWithStatus {
    int pageStart;
    int pageSize;
    try {//from  w  w  w .  j a  v a 2s  . com
        long delay = getParameter(paramMap, "delay", 0, 0, 10 * 1000);
        try {
            Thread.sleep(delay);
        } catch (InterruptedException ex) {
            throw new ServletException(ex);
        }
        int error = getParameter(paramMap, "error", 0, 0, 600);
        if (error != 0) {
            throw new ExceptionWithStatus(error);
        }
        pageStart = getParameter(paramMap, "pageStart", 0, 0, Integer.MAX_VALUE);
        pageSize = getParameter(paramMap, "pageSize", 20, 1, Integer.MAX_VALUE);
    } catch (NumberFormatException e) {
        throw new ExceptionWithStatus(HttpServletResponse.SC_BAD_REQUEST, e);
    }
    int cap = getParameter(paramMap, "cap", Integer.MAX_VALUE, 0, Integer.MAX_VALUE);
    List<Item> items_ = Lists.newArrayList(Iterables.limit(items, cap));
    int start = Math.max(0, pageStart);
    start = Math.min(start, items_.size());
    int end = start + pageSize;
    end = Math.min(end, items_.size());
    List<Item> page = Lists.newArrayList(items_.subList(start, end));
    return page;
}

From source file:org.apache.mahout.knn.cluster.DataUtils.java

/**
 * Estimates the distance cutoff. In StreamingKMeans, the distance between two vectors divided
 * by this value is used as a probability threshold when deciding whether to form a new cluster
 * or not./*from ww w  . j  ava 2 s.  c  o  m*/
 * Small values (comparable to the minimum distance between two points) are preferred as they
 * guarantee with high likelihood that all but very close points are put in separate clusters
 * initially. The clusters themselves are actually collapsed periodically when their number goes
 * over the maximum number of clusters and the distanceCutoff is increased.
 * So, the returned value is only an initial estimate.
 * @param data
 * @param distanceMeasure
 * @param sampleLimit
 * @return the minimum distance between the first sampleLimit points
 * @see StreamingKMeans#clusterInternal(Iterable, boolean)
 */
public static double estimateDistanceCutoff(Iterable<? extends Vector> data, DistanceMeasure distanceMeasure,
        int sampleLimit) {
    Iterable<? extends Vector> limitedData = Iterables.limit(data, sampleLimit);
    double minDistance = Double.POSITIVE_INFINITY;
    int i = 1;
    for (Vector u : limitedData) {
        for (Vector v : Iterables.skip(limitedData, i)) {
            double distance = distanceMeasure.distance(u, v);
            if (minDistance > distance) {
                minDistance = distance;
            }
        }
        ++i;
    }
    return minDistance;
}

From source file:org.apache.metron.utils.LatencySummarizer.java

public static String getBaseMetric(String s) {
    Iterable<String> tokenIt = Splitter.on('.').split(s);
    int num = Iterables.size(tokenIt);
    return Joiner.on('.').join(Iterables.limit(tokenIt, num - 1));
}

From source file:r.lang.ListExp.java

public static SEXP ofLength(int length) {
    return ListExp.fromIterable(Iterables.limit(Iterables.cycle(NilExp.INSTANCE), length));
}

From source file:com.sam.moca.cache.infinispan.loaders.MocaCacheStore.java

@Override
public Set<InternalCacheEntry> load(int numEntries) throws CacheLoaderException {
    Set<InternalCacheEntry> ices = loadAll();
    Set<InternalCacheEntry> returnIces = ImmutableSet.copyOf(Iterables.limit(ices, numEntries));
    return returnIces;
}