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

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

Introduction

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

Prototype

public static int size(Iterable<?> iterable) 

Source Link

Document

Returns the number of elements in iterable .

Usage

From source file:org.apache.beam.sdk.io.gcp.spanner.MutationCellCounter.java

/** Count the number of cells modified by {@link MutationGroup}. */
public static long countOf(SpannerSchema spannerSchema, MutationGroup mutationGroup) {
    long mutatedCells = 0L;
    for (Mutation mutation : mutationGroup) {
        if (mutation.getOperation() == Op.DELETE) {
            // For single key deletes sum up all the columns in the schema.
            // There is no clear way to estimate range deletes, so they are ignored.
            if (isPointDelete(mutation)) {
                final KeySet keySet = mutation.getKeySet();

                final long rows = Iterables.size(keySet.getKeys());
                mutatedCells += rows * spannerSchema.getCellsMutatedPerRow(mutation.getTable());
            }/* w w w  .  j  a va 2 s  .c o  m*/
        } else {
            // sum the cells of the columns included in the mutation
            for (String column : mutation.getColumns()) {
                mutatedCells += spannerSchema.getCellsMutatedPerColumn(mutation.getTable(), column);
            }
        }
    }

    return mutatedCells;
}

From source file:org.killbill.billing.invoice.calculator.InvoiceCalculatorUtils.java

public static boolean isInvoiceAdjustmentItem(final InvoiceItem invoiceItem,
        final Iterable<InvoiceItem> otherInvoiceItems) {
    // Invoice level credit, i.e. credit adj, but NOT on its on own invoice
    return (InvoiceItemType.CREDIT_ADJ.equals(invoiceItem.getInvoiceItemType())
            && !(Iterables.size(otherInvoiceItems) == 1
                    && InvoiceItemType.CBA_ADJ.equals(otherInvoiceItems.iterator().next().getInvoiceItemType())
                    && otherInvoiceItems.iterator().next().getInvoiceId().equals(invoiceItem.getInvoiceId())
                    && otherInvoiceItems.iterator().next().getAmount()
                            .compareTo(invoiceItem.getAmount().negate()) == 0));
}

From source file:net.sourceforge.cilib.util.selection.Selection.java

private static <T> Object[] copyOfInternal(Iterable<T> iterable) {
    checkArgument(Iterables.size(iterable) >= 1,
            "Attempting to create a " + "selection on an empty collection is not valid.");
    List<T> list = Lists.newArrayList(iterable);
    return list.toArray();
}

From source file:com.google.devtools.build.lib.concurrent.MoreFutures.java

/**
 * Waits for the first one of the following to occur:
 * <ul>//w w w. ja  va 2  s  .  c om
 * <li>All of the given futures complete successfully.
 * <li>One of the given futures has an {@link ExecutionException}. This {@link ExecutionException}
 *   is propagated. (N.B. If multiple futures have {@link ExecutionExceptions}s, one will be
 *   selected non-deterministically.)
 * <li>The calling thread is interrupted. The {@link InterruptedException} is propagated.
 * </ul>
 */
public static <V> void waitForAllInterruptiblyFailFast(Iterable<? extends Future<? extends V>> futures)
        throws ExecutionException, InterruptedException {
    int numFutures = Iterables.size(futures);
    while (true) {
        int numCompletedFutures = 0;
        for (Future<? extends V> future : futures) {
            try {
                future.get(1, TimeUnit.MILLISECONDS);
            } catch (TimeoutException te) {
                continue;
            } catch (ExecutionException ee) {
                throw ee;
            }
            numCompletedFutures++;
        }
        if (numCompletedFutures == numFutures) {
            return;
        }
    }
}

From source file:com.ning.billing.invoice.calculator.InvoiceCalculatorUtils.java

public static boolean isInvoiceAdjustmentItem(final InvoiceItem invoiceItem,
        final Iterable<InvoiceItem> otherInvoiceItems) {
    // Either REFUND_ADJ
    return InvoiceItemType.REFUND_ADJ.equals(invoiceItem.getInvoiceItemType()) ||
    // Or invoice level credit, i.e. credit adj, but NOT on its on own invoice
            (InvoiceItemType.CREDIT_ADJ.equals(invoiceItem.getInvoiceItemType())
                    && !(Iterables.size(otherInvoiceItems) == 1
                            && InvoiceItemType.CBA_ADJ
                                    .equals(otherInvoiceItems.iterator().next().getInvoiceItemType())
                            && otherInvoiceItems.iterator().next().getInvoiceId()
                                    .equals(invoiceItem.getInvoiceId())
                            && otherInvoiceItems.iterator().next().getAmount()
                                    .compareTo(invoiceItem.getAmount().negate()) == 0));
}

From source file:org.apache.mahout.math.als.AlternatingLeastSquaresSolver.java

public static Vector solve(Iterable<Vector> featureVectors, Vector ratingVector, double lambda,
        int numFeatures) {

    Preconditions.checkNotNull(featureVectors, "Feature Vectors cannot be null");
    Preconditions.checkArgument(!Iterables.isEmpty(featureVectors));
    Preconditions.checkNotNull(ratingVector, "Rating Vector cannot be null");
    Preconditions.checkArgument(ratingVector.getNumNondefaultElements() > 0, "Rating Vector cannot be empty");
    Preconditions.checkArgument(Iterables.size(featureVectors) == ratingVector.getNumNondefaultElements());

    int nui = ratingVector.getNumNondefaultElements();

    Matrix MiIi = createMiIi(featureVectors, numFeatures);
    Matrix RiIiMaybeTransposed = createRiIiMaybeTransposed(ratingVector);

    /* compute Ai = MiIi * t(MiIi) + lambda * nui * E */
    Matrix Ai = miTimesMiTransposePlusLambdaTimesNuiTimesE(MiIi, lambda, nui);
    /* compute Vi = MiIi * t(R(i,Ii)) */
    Matrix Vi = MiIi.times(RiIiMaybeTransposed);
    /* compute Ai * ui = Vi */
    return solve(Ai, Vi);
}

From source file:com.android.tools.idea.npw.importing.ImportUIUtil.java

/**
 * Formats a message picking the format string depending on number of arguments
 *
 * @param values                       values that will be used as format argument.
 * @param oneElementMessage            message when only one value is in the list. Should accept one string argument.
 * @param twoOrThreeElementsMessage    message format when there's 2 or 3 values. Should accept two string arguments.
 * @param moreThenThreeElementsMessage message format for over 3 values. Should accept one string and one number.
 * @return formatted message string//from   w  ww.ja v a 2 s  .  co  m
 */
public static String formatElementListString(Iterable<String> values, String oneElementMessage,
        String twoOrThreeElementsMessage, String moreThenThreeElementsMessage) {
    int size = Iterables.size(values);
    if (size <= 1) { // If there's 0 elements, some error happened
        return String.format(oneElementMessage, Iterables.getFirst(values, "<validation error>"));
    } else if (size <= 3) {
        return String.format(twoOrThreeElementsMessage, atMostTwo(values, size), Iterables.getLast(values));
    } else {
        return String.format(moreThenThreeElementsMessage, atMostTwo(values, size), size - 2);
    }
}

From source file:org.jclouds.openstack.nova.v1_1.domain.zonescoped.ZoneAndName.java

public static ZoneAndName fromSlashEncoded(String name) {
    Iterable<String> parts = Splitter.on('/').split(checkNotNull(name, "name"));
    checkArgument(Iterables.size(parts) == 2, "name must be in format zoneId/name");
    return new ZoneAndName(Iterables.get(parts, 0), Iterables.get(parts, 1));
}

From source file:org.eclipse.lsp4j.util.SemanticHighlightingTokens.java

/**
 * Encodes the iterable of tokens into a compact, {@code base64} string. Returns
 * with an empty string if the {@code tokens} argument is {@code null} or empty.
 *///from   www  .  j a  v  a  2 s .c o  m
public static String encode(Iterable<? extends Token> tokens) {
    if (IterableExtensions.isNullOrEmpty(tokens)) {
        return "";
    }
    // 2 stands for: number of elements for the output; the "character" and the
    // "length and scope".
    // 4 stands for: 4 byte for a primitive integer.
    ByteBuffer buffer = ByteBuffer.allocate(Iterables.size(tokens) * 2 * 4);
    for (Token token : tokens) {
        int character = token.character;
        int length = token.length;
        int scope = token.scope;

        int lengthAndScope = length;
        lengthAndScope = lengthAndScope << LENGTH_SHIFT;
        lengthAndScope |= scope;

        buffer.putInt(character);
        buffer.putInt(lengthAndScope);
    }
    return Base64.getEncoder().encodeToString(buffer.array());
}

From source file:org.sitemap4j.sitemap.AbstractSiteMap.java

@Override
public boolean equals(final Object object) {
    if (object == this) {
        return true;
    } else if (object == null) {
        return false;
    } else if (!(object instanceof SiteMap)) {
        return false;
    }//from w w w.j  a  va  2s  .co m

    final SiteMap that = (SiteMap) object;

    if (Iterables.size(that) != Iterables.size(this)) {
        return false;
    }

    for (int i = 0; i < Iterables.size(this); i++) {
        if (!Iterables.get(this, i).equals(Iterables.get(that, i))) {
            return false;
        }
    }

    return true;
}