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.openstack.keystone.v2_0.functions.ReturnEmptyPaginatedCollectionOnNotFoundOr404.java

public Object apply(Exception from) {
    Iterable<ResourceNotFoundException> throwables = Iterables.filter(Throwables.getCausalChain(from),
            ResourceNotFoundException.class);
    if (Iterables.size(throwables) >= 1) {
        return PaginatedCollection.EMPTY;
    } else if (rto404.apply(from)) {
        return PaginatedCollection.EMPTY;
    }//from   ww  w  .j a  va2 s.c om
    throw Throwables.propagate(from);
}

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

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

From source file:uk.gov.gchq.koryphe.impl.function.Length.java

@Override
public Integer apply(final Object value) {
    final int length;

    if (null == value) {
        length = 0;//from  w w  w.j  av  a 2s .co m
    } else if (value instanceof String) {
        length = ((String) value).length();
    } else if (value instanceof Object[]) {
        length = ((Object[]) value).length;
    } else if (value instanceof Iterable) {
        if (null != maxLength) {
            length = Iterables.size(IterableUtil.limit((Iterable) value, 0, maxLength, true));
        } else {
            length = Iterables.size((Iterable) value);
        }
    } else if (value instanceof Map) {
        length = ((Map) value).size();
    } else {
        throw new IllegalArgumentException("Could not determine the size of the provided value");
    }

    if (null == maxLength) {
        return length;
    }

    return Math.min(length, maxLength);
}

From source file:org.jclouds.virtualbox.util.MachineNameOrIdAndNicSlot.java

public static MachineNameOrIdAndNicSlot fromString(String machineNameOrIdAndNicSlotString) {
    Iterable<String> splittedString = Splitter.on(SEPARATOR).split(machineNameOrIdAndNicSlotString);
    checkState(Iterables.size(splittedString) == 2);
    String machineNameOrId = Strings.nullToEmpty(Iterables.get(splittedString, 0));
    String nicSlotString = Strings.nullToEmpty(Iterables.get(splittedString, 1));
    checkArgument(!nicSlotString.startsWith("+"), "Unparseable slot number: %s", nicSlotString);
    try {//from ww w. j a v  a 2 s.co m
        long slot = Long.parseLong(nicSlotString);
        checkArgument(isValidSlot(slot), "Slot number out of range: %s", nicSlotString);
        return new MachineNameOrIdAndNicSlot(machineNameOrId, slot);
    } catch (NumberFormatException e) {
        throw new IllegalArgumentException("Unparseable slot number: " + nicSlotString);
    }
}

From source file:org.jclouds.functions.JoinOnComma.java

public String apply(Object o) {
    checkNotNull(o, "input cannot be null");
    if (o.getClass().isArray()) {
        Builder<Object> builder = ImmutableList.builder();
        for (int i = 0; i < Array.getLength(o); i++)
            builder.add(Array.get(o, i));
        o = builder.build();//from   ww  w .  j  a v a2  s .co m
    }
    checkArgument(o instanceof Iterable<?>, "you must pass an iterable or array");
    Iterable<?> toJoin = (Iterable<?>) o;
    checkArgument(Iterables.size(toJoin) > 0, "you must pass an iterable or array with elements");
    return Joiner.on(',').join(toJoin);
}

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

public Object apply(Exception from) {
    Iterable<ResourceNotFoundException> throwables = Iterables.filter(Throwables.getCausalChain(from),
            ResourceNotFoundException.class);
    if (Iterables.size(throwables) >= 1) {
        return ImmutableMultimap.of();
    } else if (rto404.apply(from)) {
        return ImmutableMultimap.of();
    }//from   w w  w .j  ava 2s  .c om
    return Multimap.class.cast(propagateOrNull(from));
}

From source file:org.jclouds.tmrk.enterprisecloud.functions.ReturnEmptyVirtualMachinesOnNotFoundOr404.java

public Object apply(Exception from) {
    Iterable<ResourceNotFoundException> throwables = Iterables.filter(Throwables.getCausalChain(from),
            ResourceNotFoundException.class);
    if (Iterables.size(throwables) >= 1) {
        return VirtualMachines.builder().build();
    } else if (rto404.apply(from)) {
        return VirtualMachines.builder().build();
    }/*from   w  w  w.  ja v  a 2  s  . com*/
    return VirtualMachines.class.cast(propagateOrNull(from));
}

From source file:org.kududb.util.NetUtil.java

/**
 * Parse a comma separated list of "host:port" pairs into a list of
 * {@link HostAndPort} objects. If no port is specified for an entry in
 * the comma separated list, then a default port is used.
 * The inverse of {@link #hostsAndPortsToString(List)}.
 *
 * @param commaSepAddrs The comma separated list of "host:port" pairs.
 * @param defaultPort   The default port to use if no port is specified.
 * @return A list of HostAndPort objects constructed from commaSepAddrs.
 *//*w ww .  j ava2  s. c o  m*/
public static List<HostAndPort> parseStrings(final String commaSepAddrs, int defaultPort) {
    Iterable<String> addrStrings = Splitter.on(',').trimResults().split(commaSepAddrs);
    List<HostAndPort> hostsAndPorts = Lists.newArrayListWithCapacity(Iterables.size(addrStrings));
    for (String addrString : addrStrings) {
        HostAndPort hostAndPort = parseString(addrString, defaultPort);
        hostsAndPorts.add(hostAndPort);
    }
    return hostsAndPorts;
}

From source file:org.opendaylight.yangtools.yang.data.impl.schema.transform.base.parser.LeafSetEntryNodeBaseParser.java

@SuppressWarnings("unchecked")
@Override/*from w w  w .ja  v a 2  s.  c  om*/
public final LeafSetEntryNode<?> parse(final Iterable<E> elements, final LeafListSchemaNode schema) {
    final int size = Iterables.size(elements);
    Preconditions.checkArgument(size == 1, "Xml elements mapped to leaf node illegal count: %s", size);

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

    NormalizedNodeAttrBuilder<NodeWithValue, Object, LeafSetEntryNode<Object>> leafEntryBuilder = Builders
            .leafSetEntryBuilder(schema);
    leafEntryBuilder.withAttributes(getAttributes(e));
    leafEntryBuilder.withValue(value);

    final BuildingStrategy rawBuildingStrat = buildingStrategy;
    return (LeafSetEntryNode<?>) rawBuildingStrat.build(leafEntryBuilder);
}

From source file:com.facebook.buck.core.util.graph.TopologicalSort.java

public static <T extends Comparable<?>> ImmutableList<T> sort(TraversableGraph<T> graph) {

    // AtomicInteger is used to decrement the integer value in-place.
    Map<T, AtomicInteger> effectiveOutDegreesOfExplorableNodes = new HashMap<>();
    Queue<T> nextLevel = Queues.newArrayDeque(graph.getNodesWithNoOutgoingEdges());
    Set<T> visitedNodes = new HashSet<>();
    ImmutableList.Builder<T> toReturn = ImmutableList.builder();

    while (!nextLevel.isEmpty()) {
        Queue<T> toExplore = nextLevel;
        nextLevel = Queues.newArrayDeque();

        Set<T> level = new TreeSet<>();

        while (!toExplore.isEmpty()) {
            T node = toExplore.remove();
            Preconditions.checkState(!visitedNodes.contains(node),
                    "The queue of nodes to explore should not contain a node that has already been"
                            + " visited.");

            level.add(node);//  w  w w. j  a v a  2  s  .c  om
            visitedNodes.add(node);

            // Only add a node to the set of nodes to be explored if all the nodes it depends on have
            // been visited already. We achieve the same by keeping track of the out degrees of
            // explorable nodes. After visiting a node, decrement the out degree of each of its parent
            // node. When the out degree reaches zero, it is safe to add that node to the list of nodes
            // to explore next.
            for (T exploreCandidate : graph.getIncomingNodesFor(node)) {
                if (!effectiveOutDegreesOfExplorableNodes.containsKey(exploreCandidate)) {
                    effectiveOutDegreesOfExplorableNodes.put(exploreCandidate,
                            new AtomicInteger(Iterables.size(graph.getOutgoingNodesFor(exploreCandidate))));
                }
                if (effectiveOutDegreesOfExplorableNodes.get(exploreCandidate).decrementAndGet() == 0) {
                    nextLevel.add(exploreCandidate);
                }
            }
        }
        toReturn.addAll(level);
    }

    return toReturn.build();
}