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.jclouds.functions.ExceptionToValueOrPropagate.java

@SuppressWarnings("unchecked")
@Override// w  w w . j a v  a 2 s  .  c om
public T apply(Exception from) {
    checkNotNull(from, "exception");
    List<Throwable> throwables = Throwables.getCausalChain(from);
    Iterable<E> matchingThrowables = Iterables.filter(throwables, matchingClass);
    if (Iterables.size(matchingThrowables) >= 1)
        return value;
    return (T) Throwables2.propagateOrNull(from);
}

From source file:eu.esdihumboldt.util.StructuredEquals.java

/**
 * Determines if the given objects are equal, in turn descending into
 * {@link Iterable}s and arrays and checking if the elements are equal (in
 * order)./* w  w w  . j a v a  2 s .  c  o m*/
 * 
 * @param o1 the first object
 * @param o2 the second object
 * @return if both objects are equal
 * @see #deepIterableHashCode(Object)
 */
public boolean deepIterableEquals(Object o1, Object o2) {
    if (o1 == o2) {
        return true;
    }

    Iterable<?> iterable1 = asIterable(o1);
    Iterable<?> iterable2 = asIterable(o2);
    if (iterable1 != null && iterable2 != null) {
        if (Iterables.size(iterable1) == Iterables.size(iterable2)) { // size
            // check
            Iterator<?> it1 = iterable1.iterator();
            Iterator<?> it2 = iterable2.iterator();
            while (it1.hasNext() || it2.hasNext()) {
                try {
                    if (!deepIterableEquals(it1.next(), it2.next())) {
                        return false;
                    }
                } catch (NoSuchElementException e) {
                    return false;
                }
            }

            return true;
        }

        return false;
    } else {
        return Objects.equal(o1, o2);
    }
}

From source file:org.onos.yangtools.yang.data.impl.schema.transform.base.parser.LeafNodeBaseParser.java

@SuppressWarnings("unchecked")
@Override//from w w  w  . j  a va  2 s.  c o m
public final LeafNode<?> parse(Iterable<E> elements, LeafSchemaNode schema) {
    final int size = Iterables.size(elements);
    Preconditions.checkArgument(size == 1, "Elements mapped to leaf node illegal count: %s", size);

    final E e = elements.iterator().next();
    Object value = parseLeaf(e, schema);

    NormalizedNodeAttrBuilder<YangInstanceIdentifier.NodeIdentifier, Object, LeafNode<Object>> leafBuilder = Builders
            .leafBuilder(schema);

    leafBuilder.withAttributes(getAttributes(e));

    final BuildingStrategy rawBuildingStrat = buildingStrategy;
    return (LeafNode<?>) rawBuildingStrat.build(leafBuilder.withValue(value));
}

From source file:org.jclouds.aws.ec2.binders.BindUserGroupsToIndexedFormParams.java

private void checkValidUserGroup(Object input) {
    Iterable<?> values = (Iterable<?>) input;
    long size = Iterables.size(values);
    checkArgument(size == 0 || (size == 1 && Iterables.getOnlyElement(values).equals("all")),
            "only supported UserGroup is 'all'");
}

From source file:org.eclipseday.xtext.example.entitydsl.validation.EntityDslJavaValidator.java

@Check
public void checkDuplicateAttributes(final Attribute attribute) {
    final Entity entity = EntityDslUtil.getEntity(attribute);

    // Find all the attributes with the given name
    final ImmutableSet<Attribute> attributes = EntityDslUtil.getAttributes(attribute.getName(), entity);

    // Ignore the currently edited attribute
    if (Iterables.size(attributes) > 1) {
        final String message = "Attribute with name '" + attribute.getName() + "' already exists";
        error(message, EntityDslPackage.Literals.ATTRIBUTE__NAME);
    }/*from w  ww.  j a  va 2s .  c  om*/
}

From source file:org.onos.yangtools.yang.data.impl.schema.transform.base.serializer.ListNodeBaseSerializer.java

@Override
public final Iterable<E> serialize(final ListSchemaNode schema, final N node) {
    return Iterables.concat(Iterables.transform(node.getValue(), new Function<O, Iterable<E>>() {
        @Override/*from w  w w .j  av  a 2s .  co  m*/
        public Iterable<E> apply(final O input) {
            final Iterable<E> serializedChild = getListEntryNodeSerializer().serialize(schema, input);
            final int size = Iterables.size(serializedChild);

            Preconditions.checkState(size == 1,
                    "Unexpected count of entries  for list serialized from: %s, should be 1, was: %s", input,
                    size);
            return serializedChild;
        }
    }));
}

From source file:org.jclouds.rest.functions.ReturnEmptySetOnNotFoundOr404.java

public Object apply(Exception from) {
    Iterable<ResourceNotFoundException> throwables = Iterables.filter(Throwables.getCausalChain(from),
            ResourceNotFoundException.class);
    if (Iterables.size(throwables) >= 1) {
        return ImmutableSet.of();
    } else if (rto404.apply(from)) {
        return ImmutableSet.of();
    }/*from   w  w w  .j a  v  a2  s.c  o  m*/
    return Set.class.cast(propagateOrNull(from));
}

From source file:org.jclouds.rest.functions.ReturnEmptyMapOnNotFoundOr404.java

public Object apply(Exception from) {
    Iterable<ResourceNotFoundException> throwables = Iterables.filter(Throwables.getCausalChain(from),
            ResourceNotFoundException.class);
    if (Iterables.size(throwables) >= 1) {
        return ImmutableMap.of();
    } else if (rto404.apply(from)) {
        return ImmutableMap.of();
    }/*from ww  w.  j a  v a  2  s .co m*/
    return Map.class.cast(propagateOrNull(from));
}

From source file:org.jclouds.rest.functions.ReturnEmptyFluentIterableOnNotFoundOr404.java

public Object apply(Exception from) {
    Iterable<ResourceNotFoundException> throwables = Iterables.filter(Throwables.getCausalChain(from),
            ResourceNotFoundException.class);
    if (Iterables.size(throwables) >= 1) {
        return FluentIterable.from(ImmutableSet.of());
    } else if (rto404.apply(from)) {
        return FluentIterable.from(ImmutableSet.of());
    }//w ww .j a  va 2  s .c  o m
    throw Throwables.propagate(from);
}

From source file:co.cask.cdap.data2.metadata.dataset.SortInfo.java

/**
 * Parses a {@link SortInfo} object from the specified string. The supported format is
 * <pre>[sortBy][whitespace][sortOrder]</pre>.
 *
 * @param sort the string to parse into a {@link SortInfo}. If {@code null}, {@link #DEFAULT} is returned
 * @return the parsed {@link SortInfo}//  w  w  w . j  a va  2s.c  o  m
 * @throws BadRequestException if the string does not conform to the expected format
 */
public static SortInfo of(@Nullable String sort) throws BadRequestException {
    if (Strings.isNullOrEmpty(sort)) {
        return SortInfo.DEFAULT;
    }
    Iterable<String> sortSplit = Splitter.on(SPACE_SPLIT_PATTERN).trimResults().omitEmptyStrings().split(sort);
    if (Iterables.size(sortSplit) != 2) {
        throw new BadRequestException(String.format(
                "'sort' parameter should be a space separated string containing the field ('%s' or '%s') and "
                        + "the sort order ('%s' or '%s'). Found %s.",
                AbstractSystemMetadataWriter.ENTITY_NAME_KEY, AbstractSystemMetadataWriter.CREATION_TIME_KEY,
                SortOrder.ASC, SortOrder.DESC, sort));
    }
    Iterator<String> iterator = sortSplit.iterator();
    String sortBy = iterator.next();
    String sortOrder = iterator.next();
    if (!AbstractSystemMetadataWriter.ENTITY_NAME_KEY.equalsIgnoreCase(sortBy)
            && !AbstractSystemMetadataWriter.CREATION_TIME_KEY.equalsIgnoreCase(sortBy)) {
        throw new BadRequestException(String.format("Sort field must be '%s' or '%s'. Found %s.",
                AbstractSystemMetadataWriter.ENTITY_NAME_KEY, AbstractSystemMetadataWriter.CREATION_TIME_KEY,
                sortBy));
    }
    if (!"asc".equalsIgnoreCase(sortOrder) && !"desc".equalsIgnoreCase(sortOrder)) {
        throw new BadRequestException(String.format("Sort order must be one of '%s' or '%s'. Found %s.",
                SortOrder.ASC, SortOrder.DESC, sortOrder));
    }

    return new SortInfo(sortBy, SortInfo.SortOrder.valueOf(sortOrder.toUpperCase()));
}