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.hortonworks.streamline.common.util.ProxyUtil.java

public static Collection<String> canonicalNames(Collection<Class<?>> classes) {
    return Collections2.transform(classes, new Function<Class<?>, String>() {
        @Nullable//  w w w  . j a va 2  s. c o  m
        @Override
        public String apply(@Nullable Class<?> input) {
            return input.getCanonicalName();
        }
    });
}

From source file:org.openqa.selenium.remote.internal.WebElementToJsonConverter.java

public Object apply(Object arg) {
    if (arg == null || arg instanceof String || arg instanceof Boolean || arg instanceof Number) {
        return arg;
    }/*from w  w w.j  a v a 2 s .  com*/

    while (arg instanceof WrapsElement) {
        arg = ((WrapsElement) arg).getWrappedElement();
    }

    if (arg instanceof RemoteWebElement) {
        return ImmutableMap.of("ELEMENT", ((RemoteWebElement) arg).getId(),
                "element-6066-11e4-a52e-4f735466cecf", ((RemoteWebElement) arg).getId());
    }

    if (arg.getClass().isArray()) {
        arg = Lists.newArrayList((Object[]) arg);
    }

    if (arg instanceof Collection<?>) {
        Collection<?> args = (Collection<?>) arg;
        return Collections2.transform(args, this);
    }

    if (arg instanceof Map<?, ?>) {
        Map<?, ?> args = (Map<?, ?>) arg;
        Map<String, Object> converted = Maps.newHashMapWithExpectedSize(args.size());
        for (Map.Entry<?, ?> entry : args.entrySet()) {
            Object key = entry.getKey();
            if (!(key instanceof String)) {
                throw new IllegalArgumentException(
                        "All keys in Map script arguments must be strings: " + key.getClass().getName());
            }
            converted.put((String) key, apply(entry.getValue()));
        }
        return converted;
    }

    throw new IllegalArgumentException("Argument is of an illegal type: " + arg.getClass().getName());
}

From source file:org.mandarax.dsl.RelationshipDefinition.java

/**
 * Constructor./*from   www  .  j  av a  2s  . c om*/
 * @throws InternalScriptException thrown if method parameter names do not occur in slot definitions
 */
public RelationshipDefinition(Position position, Context context, String name,
        List<VariableDeclaration> slotDeclarations, List<String> superTypes, List<FunctionDeclaration> queries)
        throws InternalScriptException {
    super(position, context);
    this.name = name;
    this.slotDeclarations = slotDeclarations;
    this.superTypes = superTypes;
    this.queries = queries;

    // set backreference for queries
    for (FunctionDeclaration query : queries) {
        query.setRelationship(this);
    }

    // consistency check: all methodParamNames must occur in the slot declarations
    Collection<String> definedNames = Collections2.transform(slotDeclarations,
            new Function<VariableDeclaration, String>() {
                @Override
                public String apply(VariableDeclaration v) {
                    return v.getName();
                }
            });

    for (FunctionDeclaration query : queries) {
        for (String refedName : query.getParameterNames()) {
            if (!definedNames.contains(refedName))
                throw new InternalScriptException("Exception in query definition at " + query.getPosition()
                        + " the following method parameter is not defined as predicate slot: " + name);
        }
    }
}

From source file:ca.travelagency.config.ParameterRepositoryImpl.java

private List<String> getDefaultParameters(Condition condition) {
    Criteria criteria = Criteria.make(Parameter.class).addAndCondition(condition);
    List<Parameter> parameters = daoSupport.find(criteria);
    return Lists.newArrayList(Collections2.transform(parameters, new Transform()));
}

From source file:org.eclipse.viatra.emf.runtime.rules.EventDrivenTransformationRuleGroup.java

@SuppressWarnings("unchecked")
public Set<RuleSpecification<?>> getRuleSpecifications() {
    return Sets.newHashSet(Collections2.transform(this, new RuleTransformerFunction()));
}

From source file:uk.ac.ebi.atlas.solr.query.conditions.BaselineConditionsSearchService.java

Collection<IndexedAssayGroup> findAssayGroups(String queryString) {

    try {/* w  w w .  ja  v a2 s . c  o m*/
        QueryResponse queryResponse = baselineConditionsSolrServer.query(queryBuilder.build(queryString));
        List<Condition> beans = queryResponse.getBeans(Condition.class);

        Collection<IndexedAssayGroup> result = Collections2.transform(beans,
                new Function<Condition, IndexedAssayGroup>() {
                    @Override
                    public IndexedAssayGroup apply(Condition conditionProperty) {
                        return new IndexedAssayGroup(conditionProperty.getExperimentAccession(),
                                conditionProperty.getAssayGroupId());
                    }
                });

        return Sets.newHashSet(result);
    } catch (SolrServerException e) {
        throw new IllegalStateException("Conditions index query failed!", e);
    }
}

From source file:org.opennms.newts.rest.Transform.java

/**
 * Convert {@link SampleDTO}s to {@link Sample}s.
 *
 * @param samples//w w w.j  av a  2  s  .  c  o m
 *            samples to convert
 * @return converted samples
 */
static Collection<Sample> samples(Collection<SampleDTO> samples) {
    return Collections2.transform(samples, DTO_TO_SAMPLE);
}

From source file:org.raml.jaxrs.types.RamlType.java

public void write(AnnotationInstanceEmitter emitter, IndentedAppendable writer) throws IOException {

    Class c = (Class) type.getType();
    writer.appendLine(c.getSimpleName() + ":");
    writer.indent();/* w w  w .  ja  v a2s. c  o  m*/

    if (superTypes != null && superTypes.size() > 0) {
        writer.appendList("type", Collections2.transform(superTypes, new Function<RamlType, String>() {

            @Override
            public String apply(RamlType input) {
                return input.getTypeName();
            }
        }).toArray(new String[] {}));
    }

    emitter.emit(this);

    writeExample(writer);

    if (type.getDescription().isPresent()) {
        writer.appendLine("description: " + type.getDescription().get());
    }

    writer.appendLine("properties:");
    writer.indent();
    for (RamlProperty ramlProperty : properties.values()) {

        ramlProperty.write(emitter, writer);
    }
    writer.outdent();
    writer.outdent();
}

From source file:com.dangdang.ddframe.job.cloud.scheduler.mesos.ReconcileService.java

/**
 * ???.//from  ww w.j av  a  2s . c  o m
 */
public void explicitReconcile() {
    lock.lock();
    try {
        Set<TaskContext> runningTask = new HashSet<>();
        for (Set<TaskContext> each : facadeService.getAllRunningTasks().values()) {
            runningTask.addAll(each);
        }
        if (runningTask.isEmpty()) {
            return;
        }
        log.info("Requesting {} tasks reconciliation with the Mesos master", runningTask.size());
        schedulerDriver.reconcileTasks(
                Collections2.transform(runningTask, new Function<TaskContext, Protos.TaskStatus>() {
                    @Override
                    public Protos.TaskStatus apply(final TaskContext input) {
                        return Protos.TaskStatus.newBuilder()
                                .setTaskId(Protos.TaskID.newBuilder().setValue(input.getId()).build())
                                .setSlaveId(Protos.SlaveID.newBuilder().setValue(input.getSlaveId()).build())
                                .setState(Protos.TaskState.TASK_RUNNING).build();
                    }
                }));
    } finally {
        lock.unlock();
    }
}

From source file:org.polarsys.reqcycle.repository.ui.providers.DummyInputContentProvider.java

@Override
public Object[] getChildren(final Object object) {
    if (object instanceof DummyInput) {
        Object[] children = Collections2
                .transform(((DummyInput) object).getInput(), new Function<RequirementSource, DummyObject>() {

                    @Override/*w  w  w .j  a va2 s.  com*/
                    public DummyObject apply(RequirementSource reqSource) {
                        return new DummyObject(((DummyInput) object).getPredicate(), reqSource);
                    };
                }).toArray();
        return children;
    }
    if (object instanceof DummyObject) {
        final DummyObject dummyObject = (DummyObject) object;
        EObject obj = dummyObject.getEobj();
        Collection<AbstractElement> elements = Collections.emptyList();
        if (obj instanceof RequirementSource) {
            elements = ((RequirementSource) obj).getRequirements();
        }
        if (obj instanceof Section) {
            elements = ((Section) obj).getChildren();
        }

        Collection<DummyObject> transform = Collections2.transform(elements,
                new Function<EObject, DummyObject>() {

                    @Override
                    public DummyObject apply(EObject eObj) {
                        IPredicate predicate = dummyObject.getPredicate();
                        DummyObject dObj = new DummyObject(predicate, eObj);
                        if (dObj.getEobj() instanceof Section
                                && !(dObj.getEobj() instanceof SimpleRequirement)) {
                            return dObj; // do not use predicate filter for
                                         // sections which are not
                                         // requirements
                        }
                        if (predicate != null) {
                            return predicate.match(eObj) ? dObj : null;
                        } else {
                            return dObj;
                        }
                    }
                });

        Iterable<DummyObject> result = Iterables.filter(transform, Predicates.notNull());
        return Iterables.toArray(result, DummyObject.class);
    }
    return super.getChildren(object);
}