Example usage for com.google.common.collect Collections2 transform

List of usage examples for com.google.common.collect Collections2 transform

Introduction

In this page you can find the example usage for com.google.common.collect Collections2 transform.

Prototype

public static <F, T> Collection<T> transform(Collection<F> fromCollection, Function<? super F, T> function) 

Source Link

Document

Returns a collection that applies function to each element of fromCollection .

Usage

From source file:com.google.security.zynamics.zylib.yfileswrap.gui.zygraph.editmode.helpers.CGraphSelector.java

/**
 * Function which handles the selection of nodes in a path finding scenario. The function performs
 * four BFS runs: BFS 1 searches for all successors of the start nodes. BFS 2 searches for all
 * predecessors of the end nodes. BFS 2 searches for all predecessors of the start nodes. BFS 4
 * searches for all successors of the end nodes.
 * /*  w ww .  j a va 2  s  .  c  om*/
 * These four BFS runs are used in two sets: Set 1 is intersect of nodes reached through (BFS 1,
 * BFS 2). Set 2 is intersect of nodes reached through (BFS 3, BFS 4).
 * 
 * Therefore Set 1 represents all nodes on paths if the set of start nodes contains parents of the
 * newly selected node and Set 2 represents all nodes on paths if the set of start nodes contains
 * child nodes of the newly selected node.
 * 
 * 
 * @param graph The graph in which the selection takes place.
 * @param alreadySelectedNodes The List of nodes already selected.
 * @param newlySelectedNode The node which is newly selected.
 */
@SuppressWarnings("unchecked")
public static <NodeType extends ZyGraphNode<?>> void selectPath(final AbstractZyGraph<NodeType, ?> graph,
        final ArrayList<NodeType> alreadySelectedNodes, final NodeType newlySelectedNode) {
    final Function<NodeType, Node> function = new Function<NodeType, Node>() {
        @Override
        public Node apply(final NodeType input) {
            return input.getNode();
        }
    };

    final Collection<Node> foo = Collections2.transform(alreadySelectedNodes, function);

    final NodeList startNodes = new NodeList(foo.iterator());
    final NodeList endNodes = new NodeList(newlySelectedNode.getNode());

    final Set<Node> startSuccSet = new HashSet<Node>();
    final NodeList[] nodeListsStartSucc = Bfs.getLayers(graph.getGraph(), startNodes, Bfs.DIRECTION_SUCCESSOR,
            graph.getGraph().createNodeMap(), 0);
    for (final NodeList nodeList : nodeListsStartSucc) {
        startSuccSet.addAll(nodeList);
    }
    final Set<Node> endPredSet = new HashSet<Node>();
    final NodeList[] nodeListsEndPred = Bfs.getLayers(graph.getGraph(), endNodes, Bfs.DIRECTION_PREDECESSOR,
            graph.getGraph().createNodeMap(), 0);
    for (final NodeList nodeList : nodeListsEndPred) {
        endPredSet.addAll(nodeList);
    }

    final SetView<Node> startBeforeEndSetView = Sets.intersection(startSuccSet, endPredSet);
    if (!startBeforeEndSetView.isEmpty()) {
        for (final Node node : startBeforeEndSetView) {
            graph.getGraph().setSelected(node, true);
        }
    }

    final Set<Node> startPredSet = new HashSet<Node>();
    final NodeList[] nodeListsStartPred = Bfs.getLayers(graph.getGraph(), startNodes, Bfs.DIRECTION_PREDECESSOR,
            graph.getGraph().createNodeMap(), 0);
    for (final NodeList nodeList : nodeListsStartPred) {
        startPredSet.addAll(nodeList);
    }

    final Set<Node> endSuccSet = new HashSet<Node>();
    final NodeList[] nodeListsEndSucc = Bfs.getLayers(graph.getGraph(), endNodes, Bfs.DIRECTION_SUCCESSOR,
            graph.getGraph().createNodeMap(), 0);
    for (final NodeList nodeList : nodeListsEndSucc) {
        endSuccSet.addAll(nodeList);
    }

    final SetView<Node> endBeforeStartSetView = Sets.intersection(startPredSet, endSuccSet);
    if (!endBeforeStartSetView.isEmpty()) {
        for (final Node node : endBeforeStartSetView) {
            graph.getGraph().setSelected(node, true);
        }
    }
}

From source file:com.google.cloud.dataflow.sdk.options.PipelineOptionsValidator.java

/**
 * Validates that the passed {@link PipelineOptions} conforms to all the validation criteria from
 * the passed in interface./*from  w w  w  . ja v a 2s  .  c  o m*/
 *
 * <p>Note that the interface requested must conform to the validation criteria specified on
 * {@link PipelineOptions#as(Class)}.
 *
 * @param klass The interface to fetch validation criteria from.
 * @param options The {@link PipelineOptions} to validate.
 * @return The type
 */
public static <T extends PipelineOptions> T validate(Class<T> klass, PipelineOptions options) {
    checkNotNull(klass);
    checkNotNull(options);
    checkArgument(Proxy.isProxyClass(options.getClass()));
    checkArgument(Proxy.getInvocationHandler(options) instanceof ProxyInvocationHandler);

    // Ensure the methods for T are registered on the ProxyInvocationHandler
    T asClassOptions = options.as(klass);

    ProxyInvocationHandler handler = (ProxyInvocationHandler) Proxy.getInvocationHandler(asClassOptions);

    SortedSetMultimap<String, Method> requiredGroups = TreeMultimap.create(Ordering.natural(),
            PipelineOptionsFactory.MethodNameComparator.INSTANCE);
    for (Method method : ReflectHelpers.getClosureOfMethodsOnInterface(klass)) {
        Required requiredAnnotation = method.getAnnotation(Validation.Required.class);
        if (requiredAnnotation != null) {
            if (requiredAnnotation.groups().length > 0) {
                for (String requiredGroup : requiredAnnotation.groups()) {
                    requiredGroups.put(requiredGroup, method);
                }
            } else {
                checkArgument(handler.invoke(asClassOptions, method, null) != null,
                        "Missing required value for [" + method + ", \"" + getDescription(method) + "\"]. ");
            }
        }
    }

    for (String requiredGroup : requiredGroups.keySet()) {
        if (!verifyGroup(handler, asClassOptions, requiredGroups.get(requiredGroup))) {
            throw new IllegalArgumentException("Missing required value for group [" + requiredGroup
                    + "]. At least one of the following properties "
                    + Collections2.transform(requiredGroups.get(requiredGroup), ReflectHelpers.METHOD_FORMATTER)
                    + " required. Run with --help=" + klass.getSimpleName() + " for more information.");
        }
    }

    return asClassOptions;
}

From source file:com.dangdang.ddframe.job.cloud.state.misfired.MisfiredService.java

/**
 * ?./*from  w  ww  .  j  a v  a  2 s .co m*/
 *
 * @param ineligibleJobContexts 
 * @return ?
 */
public Collection<JobContext> getAllEligibleJobContexts(final Collection<JobContext> ineligibleJobContexts) {
    if (!regCenter.isExisted(MisfiredNode.ROOT)) {
        return Collections.emptyList();
    }
    Collection<String> ineligibleJobNames = Collections2.transform(ineligibleJobContexts,
            new Function<JobContext, String>() {

                @Override
                public String apply(final JobContext input) {
                    return input.getJobConfig().getJobName();
                }
            });
    List<String> jobNames = regCenter.getChildrenKeys(MisfiredNode.ROOT);
    Collection<JobContext> result = new ArrayList<>(jobNames.size());
    for (String each : jobNames) {
        Optional<CloudJobConfiguration> jobConfig = configService.load(each);
        if (!jobConfig.isPresent()) {
            regCenter.remove(MisfiredNode.getMisfiredJobNodePath(each));
            continue;
        }
        if (!ineligibleJobNames.contains(each) && !runningService.isJobRunning(each)) {
            result.add(JobContext.from(jobConfig.get(), ExecutionType.MISFIRED));
        }
    }
    return result;
}

From source file:com.codemacro.jcm.storage.ServerStorage.java

public Collection<String> getMembers() {
    List<String> nodes = getChildren();
    return Collections2.transform(nodes, new Function<String, String>() {
        public String apply(String ns) {
            String path = fullPath + "/" + ns;
            return new String(getData(path));
        }//w w w .  j a v  a 2s  .c  o m
    });
}

From source file:org.caleydo.view.domino.api.model.typed.MultiTypedSet.java

public TypedSet slice(IDType idType) {
    int index = index(idType);
    if (index < 0)
        return new TypedSet(ImmutableSet.<Integer>of(), idType);
    if (depth() == 1 && ids instanceof Single)
        return ((Single) ids).set;
    return new TypedSet(ImmutableSet.copyOf(Collections2.transform(ids, slice(index))), idType);
}

From source file:org.ow2.play.governance.platform.user.service.rest.StreamService.java

public Response streams() {
    // streams are topics... Get the topics then change them to streams

    List<Topic> topics = null;
    try {//  w  w  w . jav a  2s . co m
        topics = userResourceAccess.getTopicsForUser(getUser());
    } catch (GovernanceExeption e) {
        e.printStackTrace();
        return error(Status.INTERNAL_SERVER_ERROR, "Can not get streams");
    }

    Collection<org.ow2.play.governance.platform.user.api.rest.bean.Stream> out = Collections2.transform(topics,
            new Function<Topic, org.ow2.play.governance.platform.user.api.rest.bean.Stream>() {
                public org.ow2.play.governance.platform.user.api.rest.bean.Stream apply(Topic input) {

                    org.ow2.play.governance.platform.user.api.rest.bean.Stream stream = new org.ow2.play.governance.platform.user.api.rest.bean.Stream();
                    stream.id = StreamHelper.getStreamID(input);
                    stream.resourceUrl = getResourceURI(input);
                    return stream;
                }
            });
    return ok(out.toArray(new org.ow2.play.governance.platform.user.api.rest.bean.Stream[out.size()]));
}

From source file:blue.lapis.pore.impl.entity.PoreThrownPotion.java

@Override
public Collection<PotionEffect> getEffects() {
    Optional<PotionEffectData> data = getHandle().get(CatalogEntityData.POTION_EFFECT_DATA);
    if (data.isPresent()) {
        return Collections2.transform(data.get().effects().get(),
                new Function<org.spongepowered.api.potion.PotionEffect, PotionEffect>() {
                    @Override/* w  w  w . j a v a 2  s.c o m*/
                    public PotionEffect apply(org.spongepowered.api.potion.PotionEffect input) {
                        return PotionEffectConverter.of(input);
                    }
                });
    }
    return Collections.emptyList();
}

From source file:org.semanticweb.elk.proofs.adapters.BinarizedInferenceSetAdapter.java

@Override
public Collection<? extends JustifiedInference<List<C>, A>> getInferences(final List<C> conclusion) {
    switch (conclusion.size()) {
    case 0:/*from w  w  w.  j  av  a 2 s.c  o m*/
        return Collections.emptyList();
    case 1:
        C member = conclusion.get(0);
        return Collections2.transform(original_.getInferences(member), ToBinaryInference.<C, A>get());
    default:
        JustifiedInference<List<C>, A> inf = new BinaryListInference<C, A>(conclusion);
        return Collections.singleton(inf);
    }
}

From source file:info.magnolia.configuration.app.overview.data.DefinitionProviderGroupId.java

@Override
public Collection<? extends Id> getChildren() {
    if (children == null) {
        children = Collections2.transform(getValue(), new Function<DefinitionProvider, Id>() {
            @Nullable/*from   ww  w .j  ava2s.co m*/
            @Override
            public Id apply(DefinitionProvider input) {
                return new DefinitionProviderId(DefinitionProviderGroupId.this, input,
                        resolveOriginName(input));
            }
        });
    }
    return children;
}

From source file:com.hortonworks.registries.storage.impl.jdbc.provider.sql.query.AbstractSqlQuery.java

/**
 * if formatter != null applies the formatter to the column names. Examples of output are:
 * <p/>/*from  www . ja v  a 2 s. c  o m*/
 * formatter == null ==> [colName1, colName2]
 * <p/>
 * formatter == "%s = ?" ==> [colName1 = ?, colName2 = ?]
 */
protected Collection<String> getColumnNames(Collection<Schema.Field> columns, final String formatter) {
    return Collections2.transform(columns, new Function<Schema.Field, String>() {
        @Override
        public String apply(Schema.Field field) {
            return formatter == null ? field.getName() : String.format(formatter, field.getName());
        }
    });
}