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

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

Introduction

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

Prototype

public static <T> T get(Iterable<T> iterable, int position) 

Source Link

Document

Returns the element at the specified position in an iterable.

Usage

From source file:org.apache.ranger.policyengine.perftest.v2.RangerPolicyFactory.java

private static String pickOneRandomly(Collection<String> list) {
    return Iterables.get(list, RANDOM.nextInt(list.size()));
}

From source file:org.hbs.neo4j.importers.CsvImporter.java

/**
 * Insert a relationship from a CSV line.
 * //from  w ww  .j a v a 2 s .  c  o m
 * @param header CSV file Header.
 * @param line CSV line to insert.
 */
protected void insertRel(Iterable<String> header, String line) {
    Iterable<String> values = Splitter.on(SEPARATOR).trimResults().split(line);
    String fromNodeKey = Iterables.get(values, RELS_FILE_LINE_FROM_KEY_IDX);
    Node startNode = index.get(NodeEntity.Keys.KEY, fromNodeKey).getSingle();
    if (startNode == null) {
        throw new IllegalArgumentException(
                "Start node with key " + fromNodeKey + " does not exist. Rel won't be created.");
    }
    String toNodeKey = Iterables.get(values, RELS_FILE_LINE_TO_KEY_IDX);
    Node endNode = index.get(NodeEntity.Keys.KEY, toNodeKey).getSingle();
    if (endNode == null) {
        throw new IllegalArgumentException(
                "End node with key " + toNodeKey + " does not exist. Rel won't be created.");
    }
    Relationship relationship = startNode.createRelationshipTo(endNode,
            DynamicRelationshipType.withName(Iterables.get(values, RELS_FILE_LINE_REL_TYPE_KEY_IDX)));
    for (int idx = RELS_FILE_HEADER_MIN_SIZE; idx < Iterables.size(header); idx++) {
        relationship.setProperty(Iterables.get(header, idx), Iterables.get(values, idx));
    }
}

From source file:edu.udo.scaffoldhunter.model.dataimport.ImportJob.java

/**
 * @param index/*from ww w.j  a  va2 s . c om*/
 *            the index or row of the PropertyMapping
 * @return SourcePropertyMapping for the given Index. The
 *         sourceMergeByProperty is not filtered out. Therefore this Mapping
 *         might not be used by the actual import process
 */
public SourcePropertyMapping getPropertyMapping(int index) {
    return Iterables.get(propertyMappings.values(), index);
}

From source file:com.cinchapi.common.collect.AnyMaps.java

/**
 * Return a possibly nested value in a {@link Map} that contains other maps
 * and collections as values./*from ww  w.j  ava2 s  . c o m*/
 * 
 * @param path a navigable path key (e.g. foo.bar.1.baz)
 * @return the value
 * @deprecated in favor if the {@link Association} framework. Use
 *             {@link Association#of(Map)} to create an {@link Association}
 *             followed by {@link Association#fetch(String)} to query the
 *             {@code path}
 */
@SuppressWarnings("unchecked")
@Deprecated
public static <T> T navigate(String path, Map<String, ? extends Object> map) {
    T value = (T) map.get(path); // First, see if the path has been directly
                                 // added to the map
    if (value == null) {
        String[] components = path.split("\\.");
        Object lookup = map;
        for (String component : components) {
            Integer index;
            if (lookup == null) {
                break;
            } else if ((index = Ints.tryParse(component)) != null) {
                lookup = lookup instanceof Iterable ? Iterables.get((Iterable<T>) lookup, index) : null;
            } else {
                lookup = lookup instanceof Map ? ((Map<String, Object>) lookup).get(component) : null;
            }
        }
        if (lookup != null) {
            value = (T) lookup;
        }
    }
    return value;
}

From source file:org.polarsys.reqcycle.traceability.table.view.TraceabilityTableView.java

private void createModel() {
    TableViewerColumn viewerColumn = new TableViewerColumn(viewer, SWT.NONE);
    addDoubleClickListener(viewerColumn);
    createTableViewerColumn(viewerColumn, "Link type", 50, 0)
            .setLabelProvider(new LinkLabelProvider(styleProvider) {

                @Override/*  ww  w. j  a  v a2  s  .  c o m*/
                public String getText(Object element) {
                    if (element instanceof Link) {
                        TType kind = ((Link) element).getKind();
                        StringBuilder builder = new StringBuilder(kind.getLabel());
                        TType superKind = kind.getSuperType();
                        if (superKind != null) {
                            builder.append(String.format(" [Transverse : %s]", superKind.getLabel()));
                        }
                        return builder.toString();
                    }
                    return super.getText(element);
                }
            });
    viewerColumn = new TableViewerColumn(viewer, SWT.NONE);
    createTableViewerColumn(viewerColumn, "Upstream", 200, 1)
            .setLabelProvider(new LinkLabelProvider(styleProvider) {

                @Override
                public String getText(Object element) {
                    if (element instanceof Link) {
                        Set<Reachable> set = ((Link) element).getSources();
                        if (set != null && set.size() == 1) {
                            Reachable reachable = Iterables.get(set, 0);
                            return TraceabilityUtils.getText(reachable);
                        }
                    }
                    return super.getText(element);
                }
            });
    viewerColumn = new TableViewerColumn(viewer, SWT.NONE);
    createTableViewerColumn(viewerColumn, "Downstream", 200, 2)
            .setLabelProvider(new LinkLabelProvider(styleProvider) {

                @Override
                public String getText(Object element) {
                    if (element instanceof Link) {
                        Set<Reachable> set = ((Link) element).getTargets();
                        if (set != null && set.size() == 1) {
                            Reachable reachable = Iterables.get(set, 0);
                            return TraceabilityUtils.getText(reachable);
                        }
                    }
                    return super.getText(element);
                }
            });

}

From source file:edu.udo.scaffoldhunter.model.util.Subsets.java

/**
 * Calculated the lowest common ancestor of all given subsets.
 * /*from   w w  w .  jav  a2 s .  c o m*/
 * @param subsets
 *            the given subsets.
 * 
 * @return the lowest common ancestor of all the given subsets.
 */
public static Subset getLowestCommonAncestor(Iterable<Subset> subsets) {
    List<Subset> ancestors = Lists.newArrayList(getAncestors(Iterables.get(subsets, 0)));

    for (Subset s : Iterables.skip(subsets, 1)) {
        ancestors.retainAll(getAncestors(s));
    }

    if (ancestors.size() == 0) {
        throw new IllegalArgumentException(
                "The subsets do not have a common ancestor. " + "They are not in a common subset tree.");
    }

    return ancestors.get(0);
}

From source file:org.aksw.mex.log4mex.ExperimentConfigurationVO.java

public Execution Execution(String id) {
    Execution ret = null;/*w  w w.  java 2s  . com*/
    try {
        Collection<Execution> t = Collections2.filter(this._executions, p -> p._id.equals(id));
        if (t != null && t.size() > 0) {
            ret = Iterables.get(t, 0);
        }
    } catch (Exception e) {
        System.out.println(e.toString());
    }
    return ret;
}

From source file:io.motown.chargingstationconfiguration.viewmodel.domain.DomainService.java

/**
 * Creates a connector in a charging station type evse.
 *
 * @param chargingStationTypeId    charging station identifier.
 * @param evseId                   evse id.
 * @param connector                connector to be created.
 * @return created connector.//from   w  ww. jav a2  s.  c  om
 */
public Connector createConnector(Long chargingStationTypeId, Long evseId, Connector connector) {
    ChargingStationType chargingStationType = chargingStationTypeRepository.findOne(chargingStationTypeId);
    Evse evse = getEvseById(chargingStationType, evseId);
    Set<Connector> originalConnectors = ImmutableSet.copyOf(evse.getConnectors());

    evse.getConnectors().add(connector);

    chargingStationType = chargingStationTypeRepository.createOrUpdate(chargingStationType);

    Set<Connector> newConnectors = getEvseById(chargingStationType, evseId).getConnectors();
    Set<Connector> diffConnectors = Sets.difference(newConnectors, originalConnectors);

    if (diffConnectors.size() == 1) {
        return Iterables.get(diffConnectors, 0);
    } else {
        return null;
    }
}

From source file:org.apache.brooklyn.policy.followthesun.FollowTheSunStrategy.java

private ContainerType findOptimal(Collection<ContainerType> contenders) {
    /*/*from   w  ww .j  a v  a  2  s  .c  om*/
     * TODO should choose the least loaded mediator. Currently chooses first available, and relies 
     * on a load-balancer to move it again; would be good if these could share decision code so move 
     * it to the right place immediately. e.g.
     *   policyUtil.findLeastLoadedMediator(nodesInLocation);
     */
    return (contenders.isEmpty() ? null : Iterables.get(contenders, 0));
}

From source file:com.google.security.zynamics.binnavi.disassembly.algorithms.CViewInserter.java

/**
 * Clones a source node and inserts the cloned node into the target view.
 *
 * @param target The target view where the node is inserted.
 * @param node Node from the source view that is cloned.
 *
 * @return The cloned code node.//from w  ww .  j a va2 s  .co m
 */
private static INaviCodeNode insertCodeNode(final INaviView target, final INaviCodeNode node) {
    // TODO: cloning the node is a bad solution since this just fixes the symptoms: instructions are
    // closed two times
    final INaviCodeNode sourceNode = (INaviCodeNode) node.cloneNode();
    final Iterable<INaviInstruction> instructions = sourceNode.getInstructions();
    final ArrayList<INaviInstruction> instructionList = Lists.newArrayList(instructions);

    CCodeNode codeNode;

    try {
        codeNode = target.getContent().createCodeNode(sourceNode.getParentFunction(), instructionList);
    } catch (final MaybeNullException e) {
        codeNode = target.getContent().createCodeNode(null, instructionList);
    }

    if (sourceNode.getComments().getGlobalCodeNodeComment() != null) {
        codeNode.getComments()
                .initializeGlobalCodeNodeComment(sourceNode.getComments().getGlobalCodeNodeComment());
    }
    if (sourceNode.getComments().getLocalCodeNodeComment() != null) {
        codeNode.getComments()
                .initializeLocalCodeNodeComment(sourceNode.getComments().getLocalCodeNodeComment());
    }

    final Iterable<INaviInstruction> newInstructions = codeNode.getInstructions();
    for (int i = 0; i < Iterables.size(instructions); i++) {
        codeNode.getComments().initializeLocalInstructionComment(Iterables.get(newInstructions, i),
                sourceNode.getComments().getLocalInstructionComment(Iterables.get(instructions, i)));
    }

    return codeNode;
}