List of usage examples for com.google.common.collect Iterables limit
public static <T> Iterable<T> limit(final Iterable<T> iterable, final int limitSize)
From source file:com.facebook.presto.tests.QueryAssertions.java
public static void assertEqualsIgnoreOrder(Iterable<?> actual, Iterable<?> expected) { assertNotNull(actual, "actual is null"); assertNotNull(expected, "expected is null"); ImmutableMultiset<?> actualSet = ImmutableMultiset.copyOf(actual); ImmutableMultiset<?> expectedSet = ImmutableMultiset.copyOf(expected); if (!actualSet.equals(expectedSet)) { fail(format("not equal\nActual %s rows:\n %s\nExpected %s rows:\n %s\n", actualSet.size(), Joiner.on("\n ").join(Iterables.limit(actualSet, 100)), expectedSet.size(), Joiner.on("\n ").join(Iterables.limit(expectedSet, 100)))); }/* w w w . j a v a2s. co m*/ }
From source file:org.apache.mahout.knn.means.ThreadedKmeansScaling.java
public static void testScaling() { DistanceMeasure m = new EuclideanDistanceMeasure(); for (int d : new int[] { 5, 10, 20, 50, 100, 200, 500 }) { MultiNormal gen = new MultiNormal(d); final DenseVector projection = new DenseVector(d); projection.assign(gen.sample()); projection.normalize();//ww w .ja v a2 s . c o m Matrix data = new DenseMatrix(10000, d); TreeSet<WeightedVector> tmp = Sets.newTreeSet(); for (MatrixSlice row : data) { row.vector().assign(gen.sample()); tmp.add(WeightedVector.project(row.vector(), projection, row.index())); } Searcher ref = new Brute(data); double correct = 0; double depthSum = 0; double[] cnt = new double[MAX_DEPTH]; OnlineSummarizer distanceRatio = new OnlineSummarizer(); for (int i = 0; i < QUERIES; i++) { Vector query = new DenseVector(d); query.assign(gen.sample()); List<WeightedVector> nearest = ref.search(query, MAX_DEPTH); WeightedVector qp = WeightedVector.project(query, projection); List<WeightedVector> r = Lists.newArrayList(); for (WeightedVector v : Iterables.limit(tmp.tailSet(qp, false), SEARCH_SIZE)) { final WeightedVector projectedVector = new WeightedVector(v.getVector(), m.distance(query, v), v.getIndex()); r.add(projectedVector); } for (WeightedVector v : Iterables.limit(tmp.headSet(qp, false).descendingSet(), SEARCH_SIZE)) { r.add(new WeightedVector(v.getVector(), m.distance(query, v), v.getIndex())); } Collections.sort(r); distanceRatio.add(r.get(0).getWeight() / nearest.get(0).getWeight()); if (nearest.get(0).getIndex() == r.get(0).getIndex()) { correct++; } int depth = 0; for (WeightedVector vector : nearest) { if (vector.getIndex() == r.get(0).getIndex()) { depthSum += depth; cnt[depth]++; break; } depth++; } } System.out.printf("%d\t%.2f\t%.2f", d, correct / QUERIES, depthSum / QUERIES); System.out.printf("\t%.2f\t%.2f\t%.2f", distanceRatio.getQuartile(1), distanceRatio.getQuartile(2), distanceRatio.getQuartile(3)); for (int i = 0; i < 10; i++) { System.out.printf("\t%.2f", cnt[i] / QUERIES); } System.out.printf("\n"); } }
From source file:org.onos.yangtools.yang.data.api.schema.tree.StoreTreeNodes.java
public static <T extends StoreTreeNode<T>> Map.Entry<YangInstanceIdentifier, T> findClosestsOrFirstMatch( final T tree, final YangInstanceIdentifier path, final Predicate<T> predicate) { Optional<T> parent = Optional.<T>of(tree); Optional<T> current = Optional.<T>of(tree); int nesting = 0; Iterator<PathArgument> pathIter = path.getPathArguments().iterator(); while (current.isPresent() && pathIter.hasNext() && !predicate.apply(current.get())) { parent = current;//from w w w . jav a 2 s. c o m current = current.get().getChild(pathIter.next()); nesting++; } if (current.isPresent()) { final YangInstanceIdentifier currentPath = YangInstanceIdentifier .create(Iterables.limit(path.getPathArguments(), nesting)); return new SimpleEntry<YangInstanceIdentifier, T>(currentPath, current.get()); } /* * Subtracting 1 from nesting level at this point is safe, because we * cannot reach here with nesting == 0: that would mean the above check * for current.isPresent() failed, which it cannot, as current is always * present. At any rate we check state just to be on the safe side. */ Preconditions.checkState(nesting > 0); final YangInstanceIdentifier parentPath = YangInstanceIdentifier .create(Iterables.limit(path.getPathArguments(), nesting - 1)); return new SimpleEntry<YangInstanceIdentifier, T>(parentPath, parent.get()); }
From source file:org.apache.cassandra.service.BatchlogEndpointSelector.java
/** * @param endpoints nodes in the local datacenter, grouped by rack name * @return list of candidates for batchlog hosting. if possible these will be two nodes from different racks. *///from w w w. j a v a 2 s. co m public Collection<InetAddress> chooseEndpoints(Multimap<String, InetAddress> endpoints) { // strip out dead endpoints and localhost ListMultimap<String, InetAddress> validated = ArrayListMultimap.create(); for (Map.Entry<String, InetAddress> entry : endpoints.entries()) { if (isValid(entry.getValue())) validated.put(entry.getKey(), entry.getValue()); } if (validated.size() <= 2) return validated.values(); if ((validated.size() - validated.get(localRack).size()) >= 2) { // we have enough endpoints in other racks validated.removeAll(localRack); } if (validated.keySet().size() == 1) { // we have only 1 `other` rack Collection<InetAddress> otherRack = Iterables.getOnlyElement(validated.asMap().values()); return Lists.newArrayList(Iterables.limit(otherRack, 2)); } // randomize which racks we pick from if more than 2 remaining Collection<String> racks; if (validated.keySet().size() == 2) { racks = validated.keySet(); } else { racks = Lists.newArrayList(validated.keySet()); Collections.shuffle((List) racks); } // grab a random member of up to two racks List<InetAddress> result = new ArrayList<>(2); for (String rack : Iterables.limit(racks, 2)) { List<InetAddress> rackMembers = validated.get(rack); result.add(rackMembers.get(getRandomInt(rackMembers.size()))); } return result; }
From source file:com.metamx.common.guava.FunctionalIterable.java
public FunctionalIterable<T> limit(int limit) { return new FunctionalIterable<>(Iterables.limit(delegate, limit)); }
From source file:dropship.agent.statsd.SnitchService.java
SnitchService(Properties properties, String groupArtifactId, Class mainClass) { checkNotNull(properties, "properties"); this.gavKeys = ImmutableList.copyOf( Iterables.limit(Splitter.on(':').split(CharMatcher.is('.').replaceFrom(groupArtifactId, '-')), 2)); this.hostKeys = ImmutableList.of(getHostname()); String simplifiedMainClassName = Iterables.getLast(Splitter.on('.').split(mainClass.getName())); this.methodKeys = ImmutableList.of(simplifiedMainClassName); }
From source file:sg.atom.utils._beta.functional.FunctionalIterable.java
public FunctionalIterable<T> limit(int limit) { return new FunctionalIterable<T>(Iterables.limit(delegate, limit)); }
From source file:com.facebook.presto.plugin.blackhole.BlackHolePageSourceProvider.java
@Override public ConnectorPageSource createPageSource(ConnectorSession session, ConnectorSplit split, List<ColumnHandle> columns) { BlackHoleSplit blackHoleSplit = checkType(split, BlackHoleSplit.class, "BlackHoleSplit"); ImmutableList.Builder<Type> builder = ImmutableList.builder(); for (ColumnHandle column : columns) { builder.add((checkType(column, BlackHoleColumnHandle.class, "BlackHoleColumnHandle")).getColumnType()); }/*ww w . j a v a 2s. c o m*/ List<Type> types = builder.build(); return new FixedPageSource(Iterables.limit( Iterables.cycle( generateZeroPage(types, blackHoleSplit.getRowsPerPage(), blackHoleSplit.getFieldsLength())), blackHoleSplit.getPagesCount())); }
From source file:com.google.devtools.build.lib.util.StringUtil.java
/** * Lists items up to a given limit, then prints how many were omitted. *//*from www . j av a2s .c o m*/ public static StringBuilder listItemsWithLimit(StringBuilder appendTo, int limit, Collection<?> items) { Preconditions.checkState(limit > 0); Joiner.on(", ").appendTo(appendTo, Iterables.limit(items, limit)); if (items.size() > limit) { appendTo.append(" ...(omitting ").append(items.size() - limit).append(" more item(s))"); } return appendTo; }
From source file:it.anyplace.sync.core.cache.FileBlockCache.java
private void runCleanup() { if (size > MAX_SIZE) { logger.info("starting cleanup of cache directory, initial size = {}", FileUtils.byteCountToDisplaySize(size)); List<File> files = Lists.newArrayList(dir.listFiles()); Collections.sort(files, Ordering.natural().onResultOf(new Function<File, Long>() { @Override//from ww w . j ava2 s. c o m public Long apply(File input) { return input.lastModified(); } })); for (File file : Iterables.limit(files, (int) (files.size() * PERC_TO_DELETE))) { logger.debug("delete file {}", file); FileUtils.deleteQuietly(file); } size = FileUtils.sizeOfDirectory(dir); logger.info("cleanup of cache directory completed, final size = {}", FileUtils.byteCountToDisplaySize(size)); } }