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

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

Introduction

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

Prototype

@CheckReturnValue
public static <F, T> Iterable<T> transform(final Iterable<F> fromIterable,
        final Function<? super F, ? extends T> function) 

Source Link

Document

Returns an iterable that applies function to each element of fromIterable .

Usage

From source file:org.eclipse.emf.mwe2.language.factory.SettingProviderImpl.java

public Map<QualifiedName, ISetting> getSettings(final Object obj, JvmType type) {
    Map<QualifiedName, JvmFeature> features = injectableFeatureLookup.getInjectableFeatures(type);

    Iterable<ISetting> settings = Iterables.transform(features.entrySet(),
            new Function<Map.Entry<QualifiedName, JvmFeature>, ISetting>() {
                public ISetting apply(final Map.Entry<QualifiedName, JvmFeature> from) {
                    if (from.getValue() instanceof JvmOperation) {
                        return new ISetting() {
                            public void setValue(Object value) {
                                Method method = reflectAccess.getMethod((JvmOperation) from.getValue());
                                try {
                                    method.invoke(obj, value);
                                } catch (Exception e) {
                                    throw new WrappedException(e);
                                }//from  w w  w.  jav  a2s.c  o  m
                            }

                            public QualifiedName getName() {
                                return from.getKey();
                            }
                        };
                    } else if (from.getValue() instanceof JvmField) {
                        return new ISetting() {
                            public void setValue(Object value) {
                                Field field = reflectAccess.getField((JvmField) from.getValue());
                                try {
                                    field.set(obj, value);
                                } catch (Exception e) {
                                    throw new WrappedException(e);
                                }
                            }

                            public QualifiedName getName() {
                                return from.getKey();
                            }
                        };
                    }
                    throw new IllegalArgumentException(
                            from.getValue().getIdentifier() + " can not be handled.");
                }
            });
    return Maps.uniqueIndex(settings, new Function<ISetting, QualifiedName>() {
        public QualifiedName apply(ISetting from) {
            return from.getName();
        }
    });
}

From source file:org.apache.hadoop.hive.ql.exec.tez.ScaledHeadroomCalculator.java

private List<Double> getThresholds(String distributionString) {
    try {//  w w w.j av  a 2  s  .  com
        List<String> thresholdStrings = Lists.newArrayList(distributionString.split(","));
        List<Double> thresholds = Lists
                .newArrayList(Iterables.transform(thresholdStrings, new Function<String, Double>() {
                    @Override
                    public Double apply(String input) {
                        return Double.parseDouble(input);
                    }
                }));
        Collections.sort(thresholds);
        return thresholds;
    } catch (Exception exception) {
        LOG.error(
                "Could not extract thresholds from string: " + distributionString + ". Switching to defaults: "
                        + HiveConf.ConfVars.HIVE_AM_SCALED_HEADROOM_CALCULATOR_DISTRIBUTION.getDefaultValue(),
                exception);
        return getThresholds(
                HiveConf.ConfVars.HIVE_AM_SCALED_HEADROOM_CALCULATOR_DISTRIBUTION.getDefaultValue());
    }

}

From source file:io.fabric8.service.jclouds.commands.CloudServiceList.java

@Override
protected Object doExecute() throws Exception {
    Iterable<ProviderMetadata> providers = Providers.viewableAs(TypeToken.of(ComputeServiceContext.class));
    Iterable<ApiMetadata> apis = Apis.viewableAs(TypeToken.of(ComputeServiceContext.class));

    Iterable<String> providerIds = Iterables.transform(providers, new Function<ProviderMetadata, String>() {
        @Override//from   w ww. ja va2 s  .  co m
        public String apply(@Nullable ProviderMetadata input) {
            return input.getId();
        }
    });

    Iterable<String> apiIds = Iterables.transform(apis, new Function<ApiMetadata, String>() {
        @Override
        public String apply(@Nullable ApiMetadata input) {
            return input.getId();
        }
    });

    boolean providerOrApiFound = false;

    if (apiIds != null) {
        providerOrApiFound = true;
        System.out.println("Compute APIs:");
        System.out.println("-------------");
        printComputeProvidersOrApis(apiIds, computeRegistry.list(), "", System.out);
    }

    if (providers != null) {
        providerOrApiFound = true;
        System.out.println("Compute Providers:");
        System.out.println("-------------");
        printComputeProvidersOrApis(providerIds, computeRegistry.list(), "", System.out);
    }

    if (!providerOrApiFound) {
        System.out.println("No providers or apis have been found.");
    }
    return null;
}

From source file:com.proofpoint.event.monitor.MonitorsResource.java

@GET
@Produces(MediaType.APPLICATION_JSON)/*from w  w  w  .j a v a2s . c o m*/
public Iterable<MonitorRepresentation> getAll(@Context final UriInfo uriInfo) {
    return Iterables.transform(monitors.values(), new Function<Monitor, MonitorRepresentation>() {
        @Override
        public MonitorRepresentation apply(@Nullable Monitor monitor) {
            return MonitorRepresentation.of(monitor, uriInfo);
        }
    });
}

From source file:com.anhth12.lambda.app.serving.als.Because.java

@GET
@Path("{userID}/{itemID}")
@Produces({ MediaType.TEXT_PLAIN, CSVMessageBodyWriter.TEXT_CSV, MediaType.APPLICATION_JSON })
public List<IDValue> get(@PathParam("userID") String userID, @PathParam("itemID") String itemID,
        @DefaultValue("10") @QueryParam("howMany") int howMany,
        @DefaultValue("0") @QueryParam("offset") int offset) throws LambdaServingException {
    check(howMany > 0, "howMany must be positive");
    check(offset >= 0, "offset must be non-negative");

    ALSServingModel model = getALSServingModel();
    float[] itemVector = model.getItemVector(itemID);
    checkExists(itemVector != null, itemID);
    List<Pair<String, float[]>> knownItemVectors = model.getKnowItemVectorsForUser(userID);

    checkExists(knownItemVectors != null, itemID);

    Iterable<Pair<String, Double>> idSimilarities = Iterables.transform(knownItemVectors,
            new CosineSimilarityFunction(itemVector));

    Ordering<Pair<?, Double>> ordering = Ordering.from(PairComparators.<Double>bySecond());

    return toIDValueResponse(ordering.greatestOf(idSimilarities, howMany + offset), howMany, offset);
}

From source file:com.slimgears.slimrepo.apt.MetaFields.java

public static <P extends PropertyInfo> TypeSpec createMetaType(TypeName entityType, TypeName keyType,
        Iterable<P> props) {
    return TypeSpec.classBuilder("MetaType")
            .superclass(ParameterizedTypeName.get(ClassName.get(AbstractEntityType.class), keyType, entityType))
            .addModifiers(Modifier.PRIVATE, Modifier.STATIC)
            .addMethod(MethodSpec.constructorBuilder().addCode("super($T.class, ", entityType)
                    .addCode(Joiner.on(", ")
                            .join(Iterables.transform(props, prop -> getMetaFieldName(prop.getName()))))
                    .addCode(");\n").build())
            .addMethod(MethodSpec.methodBuilder("newInstance").addAnnotation(Override.class)
                    .addModifiers(Modifier.PUBLIC).returns(entityType).addCode("return new $T();\n", entityType)
                    .build())// w w w. j a v a  2s . c  om
            .build();
}

From source file:com.facebook.buck.cxx.toolchain.WindowsPreprocessor.java

@Override
public Iterable<String> quoteIncludeArgs(Iterable<String> includeRoots) {
    return Iterables.transform(includeRoots, WindowsPreprocessor::prependIncludeFlag);
}

From source file:com.facebook.buck.step.CompositeStep.java

@Override
public String getShortName() {
    return Joiner.on("_&&_").join(Iterables.transform(steps, new Function<Step, String>() {
        @Override//from  w w w  .  java  2 s.  com
        public String apply(Step step) {
            return step.getShortName();
        }
    }));
}

From source file:com.cloudera.oryx.app.serving.als.MostPopularItems.java

static List<IDCount> mapTopCountsToIDCounts(Map<String, Integer> counts, int howMany, int offset,
        final Rescorer rescorer) {
    Iterable<Pair<String, Integer>> countPairs = Iterables.transform(counts.entrySet(),
            new Function<Map.Entry<String, Integer>, Pair<String, Integer>>() {
                @Override//from  w w  w  .j  ava2  s .c o m
                public Pair<String, Integer> apply(Map.Entry<String, Integer> input) {
                    return new Pair<>(input.getKey(), input.getValue());
                }
            });

    if (rescorer != null) {
        countPairs = Iterables.filter(countPairs, new Predicate<Pair<String, Integer>>() {
            @Override
            public boolean apply(Pair<String, Integer> input) {
                return !rescorer.isFiltered(input.getFirst());
            }
        });
    }

    List<Pair<String, Integer>> allTopCountPairs = Ordering.from(PairComparators.<Integer>bySecond())
            .greatestOf(countPairs, howMany + offset);
    List<Pair<String, Integer>> topCountPairs = selectedSublist(allTopCountPairs, howMany, offset);

    return Lists.transform(topCountPairs, new Function<Pair<String, Integer>, IDCount>() {
        @Override
        public IDCount apply(Pair<String, Integer> idCount) {
            return new IDCount(idCount.getFirst(), idCount.getSecond());
        }
    });
}

From source file:org.axdt.avm.scoping.AvmTypeScope.java

@Override
protected Iterable<IEObjectDescription> getCandidates() {
    AvmTypeAccess access = AvmTypeAccess.Factory.thisAccess(element).setStatic(true);
    if (ctx != null && ctx != element) {
        for (EObject current = ctx; current != null;) {
            EObject next = current.eContainer();
            if (next == element) {
                if (current instanceof AvmMember) {
                    AvmMember member = (AvmMember) current;
                    if (member.isStatic())
                        access.setInstance(false);
                }//from w ww . ja v  a  2s.  co  m
                break;
            }
            current = next;
        }
    }
    List<AvmMember> result = Lists.newArrayList();
    collectAllMembers(access, result, true, true);
    return Iterables.transform(result, GetDesciption);
}