java.util.function.Function类的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(15.5k)|赞(0)|评价(0)|浏览(190)

本文整理了Java中java.util.function.Function类的一些代码示例,展示了Function类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Function类的具体详情如下:
包路径:java.util.function.Function
类名称:Function

Function介绍

[英]Represents a function that accepts one argument and produces a result.

This is a functional interface whose functional method is #apply(Object).
[中]表示接受一个参数并生成结果的函数。
这是一个{$0$},其函数方法是#apply(Object)。

代码示例

代码示例来源:origin: prestodb/presto

private static Optional<List<Symbol>> translateSymbols(Iterable<Symbol> partitioning, Function<Symbol, Optional<Symbol>> translator)
{
  ImmutableList.Builder<Symbol> newPartitioningColumns = ImmutableList.builder();
  for (Symbol partitioningColumn : partitioning) {
    Optional<Symbol> translated = translator.apply(partitioningColumn);
    if (!translated.isPresent()) {
      return Optional.empty();
    }
    newPartitioningColumns.add(translated.get());
  }
  return Optional.of(newPartitioningColumns.build());
}

代码示例来源:origin: prestodb/presto

@Override
public Map<String, ColumnHandle> getColumnHandles(ConnectorSession session, ConnectorTableHandle tableHandle)
{
  InformationSchemaTableHandle informationSchemaTableHandle = checkTableHandle(tableHandle);
  ConnectorTableMetadata tableMetadata = TABLES.get(informationSchemaTableHandle.getSchemaTableName());
  return tableMetadata.getColumns().stream()
      .map(ColumnMetadata::getName)
      .collect(toMap(identity(), InformationSchemaColumnHandle::new));
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Constructor with externally managed Reactor Netty resources, including
 * {@link LoopResources} for event loop threads, and {@link ConnectionProvider}
 * for the connection pool.
 * <p>This constructor should be used only when you don't want the client
 * to participate in the Reactor Netty global resources. By default the
 * client participates in the Reactor Netty global resources held in
 * {@link reactor.netty.http.HttpResources}, which is recommended since
 * fixed, shared resources are favored for event loop concurrency. However,
 * consider declaring a {@link ReactorResourceFactory} bean with
 * {@code globalResources=true} in order to ensure the Reactor Netty global
 * resources are shut down when the Spring ApplicationContext is closed.
 * @param factory the resource factory to obtain the resources from
 * @param mapper a mapper for further initialization of the created client
 * @since 5.1
 */
public ReactorClientHttpConnector(ReactorResourceFactory factory, Function<HttpClient, HttpClient> mapper) {
  this.httpClient = defaultInitializer.andThen(mapper).apply(initHttpClient(factory));
}

代码示例来源:origin: apache/incubator-dubbo

public void thenApplyWithContext(Function<Result, Result> fn) {
  this.resultFuture = resultFuture.thenApply(fn.compose(beforeContext).andThen(afterContext));
}

代码示例来源:origin: spring-projects/spring-framework

private static <T, S extends Publisher<T>> S readWithMessageReaders(
    ReactiveHttpInputMessage message, BodyExtractor.Context context, ResolvableType elementType,
    Function<HttpMessageReader<T>, S> readerFunction,
    Function<UnsupportedMediaTypeException, S> errorFunction,
    Supplier<S> emptySupplier) {
  if (VOID_TYPE.equals(elementType)) {
    return emptySupplier.get();
  }
  MediaType contentType = Optional.ofNullable(message.getHeaders().getContentType())
      .orElse(MediaType.APPLICATION_OCTET_STREAM);
  return context.messageReaders().stream()
      .filter(reader -> reader.canRead(elementType, contentType))
      .findFirst()
      .map(BodyExtractors::<T>cast)
      .map(readerFunction)
      .orElseGet(() -> {
        List<MediaType> mediaTypes = context.messageReaders().stream()
            .flatMap(reader -> reader.getReadableMediaTypes().stream())
            .collect(Collectors.toList());
        return errorFunction.apply(
            new UnsupportedMediaTypeException(contentType, mediaTypes, elementType));
      });
}

代码示例来源:origin: jooby-project/jooby

private List<Tag> tags(RouteMethod route, Function<String, Tag> tagFactory) {
 String[] tags = arrayAttribute(route, "tags");
 if (tags != null) {
  List<Tag> result = Stream.of(tags)
    .filter(it -> it.length() > 0)
    .map(tagFactory)
    .collect(Collectors.toList());
  if (result.size() > 0) {
   return result;
  }
 }
 Tag tag = tagFactory.apply(tagger.apply(route));
 route.summary().ifPresent(tag::description);
 return ImmutableList.of(tag);
}

代码示例来源:origin: prestodb/presto

private ConnectorTableMetadata getTableMetadata(SchemaTableName tableName)
  if (!table.isPresent() || table.get().getTableType().equals(TableType.VIRTUAL_VIEW.name())) {
    throw new TableNotFoundException(tableName);
  Function<HiveColumnHandle, ColumnMetadata> metadataGetter = columnMetadataGetter(table.get(), typeManager);
  ImmutableList.Builder<ColumnMetadata> columns = ImmutableList.builder();
  for (HiveColumnHandle columnHandle : hiveColumnHandles(table.get())) {
    columns.add(metadataGetter.apply(columnHandle));
  ImmutableMap.Builder<String, Object> properties = ImmutableMap.builder();
  if (table.get().getTableType().equals(EXTERNAL_TABLE.name())) {
    properties.put(EXTERNAL_LOCATION_PROPERTY, table.get().getStorage().getLocation());
  List<String> partitionedBy = table.get().getPartitionColumns().stream()
      .map(Column::getName)
      .collect(toList());
  if (!partitionedBy.isEmpty()) {
    properties.put(PARTITIONED_BY_PROPERTY, partitionedBy);
  Optional<String> comment = Optional.ofNullable(table.get().getParameters().get(TABLE_COMMENT));
  return new ConnectorTableMetadata(tableName, columns.build(), properties.build(), comment);

代码示例来源:origin: prestodb/presto

if (!driverOptions.suites.isEmpty()) {
  suites = suites.stream()
      .filter(suite -> driverOptions.suites.contains(suite.getName()))
      .collect(Collectors.toList());
suites = ImmutableList.copyOf(suites);
if (driverOptions.queries.isEmpty()) {
  queries = suites.stream()
      .map(suite -> suite.selectQueries(allQueries))
      .flatMap(List::stream)
      .collect(Collectors.toSet());
      .map(Pattern::compile)
      .map(pattern -> allQueries.stream().filter(query -> pattern.matcher(query.getName()).matches()))
      .flatMap(identity())
      .collect(Collectors.toSet());
    driverOptions.debug,
    driverOptions.maxFailures,
    Optional.ofNullable(driverOptions.socksProxy))) {
  for (Suite suite : suites) {
    benchmarkDriver.run(suite);

代码示例来源:origin: prestodb/presto

return ImmutableMap.of();
CacheKey anyKey = stream(keys).findAny().get();
if (stream(keys).anyMatch(k -> !k.getSchema().equals(anyKey.getSchema()) || !k.getTable().equals(anyKey.getTable()) || !k.getFamily().equals(anyKey.getFamily()) || !k.getQualifier().equals(anyKey.getQualifier()))) {
  throw new PrestoException(FUNCTION_IMPLEMENTATION_ERROR, "loadAll called with a non-homogeneous collection of cache keys");
Map<Range, CacheKey> rangeToKey = stream(keys).collect(Collectors.toMap(CacheKey::getRange, Function.identity()));
LOG.debug("rangeToKey size is %s", rangeToKey.size());
  scanner.setRanges(stream(keys).map(CacheKey::getRange).collect(Collectors.toList()));
  scanner.fetchColumn(columnFamily, CARDINALITY_CQ_AS_TEXT);

代码示例来源:origin: prestodb/presto

ImmutableMap.Builder<StageId, StageLinkage> stageLinkages)
ImmutableList.Builder<SqlStageExecution> stages = ImmutableList.builder();
    schedulerStats);
stages.add(stage);
  bucketToPartition = Optional.of(new int[1]);
  bucketToPartition = Optional.of(new int[1]);
      NodePartitionMap nodePartitionMap = partitioningCache.apply(plan.getFragment().getPartitioning());
      if (groupedExecutionForStage) {
        checkState(connectorPartitionHandles.size() == nodePartitionMap.getBucketToPartition().length);
    NodePartitionMap nodePartitionMap = partitioningCache.apply(plan.getFragment().getPartitioning());
    List<Node> partitionToNode = nodePartitionMap.getPartitionToNode();
      stageSchedulers,
      stageLinkages);
  stages.addAll(subTree);
      .map(RemoteTask::getTaskStatus)
      .collect(toList());
return stages.build();

代码示例来源:origin: prestodb/presto

public static Optional<SortExpressionContext> extractSortExpression(Set<Symbol> buildSymbols, Expression filter)
{
  List<Expression> filterConjuncts = ExpressionUtils.extractConjuncts(filter);
  SortExpressionVisitor visitor = new SortExpressionVisitor(buildSymbols);
  List<SortExpressionContext> sortExpressionCandidates = filterConjuncts.stream()
      .filter(DeterminismEvaluator::isDeterministic)
      .map(visitor::process)
      .filter(Optional::isPresent)
      .map(Optional::get)
      .collect(toMap(SortExpressionContext::getSortExpression, identity(), SortExpressionExtractor::merge))
      .values()
      .stream()
      .collect(toImmutableList());
  // For now heuristically pick sort expression which has most search expressions assigned to it.
  // TODO: make it cost based decision based on symbol statistics
  return sortExpressionCandidates.stream()
      .sorted(comparing(context -> -1 * context.getSearchExpressions().size()))
      .findFirst();
}

代码示例来源:origin: Graylog2/graylog2-server

@Override
public SearchResponse decorate(SearchResponse searchResponse, Optional<String> streamId) {
  try {
    final List<SearchResponseDecorator> searchResponseDecorators = streamId.isPresent() ?
        decoratorResolver.searchResponseDecoratorsForStream(streamId.get()) : decoratorResolver.searchResponseDecoratorsForGlobal();
    final Optional<SearchResponseDecorator> metaDecorator = searchResponseDecorators.stream()
        .reduce((f, g) -> (v) -> g.apply(f.apply(v)));
    if (metaDecorator.isPresent()) {
      final Map<String, ResultMessageSummary> originalMessages = searchResponse.messages()
          .stream()
          .collect(Collectors.toMap(this::getMessageKey, Function.identity()));
      final SearchResponse newSearchResponse = metaDecorator.get().apply(searchResponse);
      final Set<String> newFields = extractFields(newSearchResponse.messages());
          .map(resultMessage -> {
            final ResultMessageSummary originalMessage = originalMessages.get(getMessageKey(resultMessage));
            if (originalMessage != null) {
          .collect(Collectors.toList());

代码示例来源:origin: jdbi/jdbi

@Override
public Optional<Handler> buildHandler(Class<?> sqlObjectType, Method method) {
  if (!method.isBridge()) {
    return Optional.empty();
  }
  return Stream.of(sqlObjectType.getMethods())
    .filter(candidate -> !candidate.isBridge()
      && Objects.equals(candidate.getName(), method.getName())
      && candidate.getParameterCount() == method.getParameterCount())
    .filter(candidate -> {
      Class<?>[] candidateParamTypes = candidate.getParameterTypes();
      Class<?>[] methodParamTypes = method.getParameterTypes();
      return IntStream.range(0, method.getParameterCount())
        .allMatch(i -> methodParamTypes[i].isAssignableFrom(candidateParamTypes[i]));
    })
    .<Handler>map(m -> {
      final MethodHandle mh = unreflect(sqlObjectType, m);
      return (target, args, handle) -> Unchecked.<Object[], Object>function(mh.bindTo(target)::invokeWithArguments).apply(args);
    })
    .findFirst();
}

代码示例来源:origin: prestodb/presto

.map(function -> (session, addresses, pages) -> new StandardJoinFilterFunction(function, addresses, pages));
LocalExchangeSinkOperatorFactory sinkOperatorFactory = new LocalExchangeSinkOperatorFactory(localExchangeFactory, 1, new PlanNodeId("sink"), localExchangeSinkFactoryId, Function.identity());
Driver sourceDriver = Driver.createDriver(collectDriverContext,
    valuesOperatorFactory.createOperator(collectDriverContext),
    buildPages.getTypes(),
    rangeList(buildPages.getTypes().size()).stream()
        .map(buildPages.getTypes()::get)
        .collect(toImmutableList()),
    hashChannels.stream()
        .map(buildPages.getTypes()::get)
        .collect(toImmutableList()),
    partitionCount,
    requireNonNull(ImmutableMap.of(), "layout is null"),
    false));
    hashChannels,
    buildPages.getHashChannel()
        .map(OptionalInt::of).orElse(OptionalInt.empty()),
    filterFunctionFactory,
    Optional.empty(),
    ImmutableList.of(),
    100,
    new PagesIndex.TestingFactory(false),

代码示例来源:origin: jdbi/jdbi

private static Map<String, Method> load(Class<?> type) {
  if (Modifier.isPublic(type.getModifiers())) {
    return Arrays.stream(type.getMethods())
      .filter(m -> m.getParameterCount() == 0)
      .collect(Collectors.toMap(Method::getName, Function.identity(), ObjectMethodArguments::bridgeMethodMerge));
  } else {
    final HashMap<String, Method> methodMap = new HashMap<>();
    Optional.ofNullable(type.getSuperclass()).ifPresent(superclass -> methodMap.putAll(load(superclass)));
    Arrays.stream(type.getInterfaces()).forEach(interfaceClass -> methodMap.putAll(load(interfaceClass)));
    return methodMap;
  }
}

代码示例来源:origin: Graylog2/graylog2-server

protected <RemoteInterfaceType, FinalResponseType, RemoteCallResponseType> Map<String, Optional<FinalResponseType>> getForAllNodes(Function<RemoteInterfaceType, Call<RemoteCallResponseType>> fn, Function<String, Optional<RemoteInterfaceType>> interfaceProvider, Function<RemoteCallResponseType, FinalResponseType> transformer) {
  final Map<String, Future<Optional<FinalResponseType>>> futures = this.nodeService.allActive().keySet().stream()
      .collect(Collectors.toMap(Function.identity(), node -> interfaceProvider.apply(node)
          .map(r -> executor.submit(() -> {
            final Call<RemoteCallResponseType> call = fn.apply(r);
            try {
              final Response<RemoteCallResponseType> response = call.execute();
              if (response.isSuccessful()) {
                return Optional.of(transformer.apply(response.body()));
              } else {
                LOG.warn("Unable to call {} on node <{}>, result: {}", call.request().url(), node, response.message());
                return Optional.<FinalResponseType>empty();
      .entrySet()
      .stream()
      .collect(Collectors.toMap(Map.Entry::getKey, entry -> {
        try {
          return entry.getValue().get();

代码示例来源:origin: prestodb/presto

public static Optional<HiveBucketHandle> getHiveBucketHandle(Table table)
{
  Optional<HiveBucketProperty> hiveBucketProperty = table.getStorage().getBucketProperty();
  if (!hiveBucketProperty.isPresent()) {
    return Optional.empty();
  }
  Map<String, HiveColumnHandle> map = getRegularColumnHandles(table).stream()
      .collect(Collectors.toMap(HiveColumnHandle::getName, identity()));
  ImmutableList.Builder<HiveColumnHandle> bucketColumns = ImmutableList.builder();
  for (String bucketColumnName : hiveBucketProperty.get().getBucketedBy()) {
    HiveColumnHandle bucketColumnHandle = map.get(bucketColumnName);
    if (bucketColumnHandle == null) {
      throw new PrestoException(
          HIVE_INVALID_METADATA,
          format("Table '%s.%s' is bucketed on non-existent column '%s'", table.getDatabaseName(), table.getTableName(), bucketColumnName));
    }
    bucketColumns.add(bucketColumnHandle);
  }
  int bucketCount = hiveBucketProperty.get().getBucketCount();
  return Optional.of(new HiveBucketHandle(bucketColumns.build(), bucketCount, bucketCount));
}

代码示例来源:origin: line/armeria

.addAll(baseExceptionHandlers).add(defaultExceptionHandler).build();
final List<RequestConverterFunction> req =
    getAnnotatedInstances(method, clazz, RequestConverter.class, RequestConverterFunction.class)
  resolvers = ImmutableList.of();
final Set<String> requiredParamNames =
    resolvers.stream()
         .filter(AnnotatedValueResolver::isPathVariable)
         .map(AnnotatedValueResolver::httpElementName)
         .collect(Collectors.toSet());
    .map(code -> {
      final int statusCode = code.value();
      checkArgument(statusCode >= 0,
    ? extends Service<HttpRequest, HttpResponse>> initialDecorator;
if (methods.contains(HttpMethod.OPTIONS)) {
  initialDecorator = Function.identity();
} else {
  initialDecorator = delegate -> new SimpleDecoratingService<HttpRequest, HttpResponse>(delegate) {

代码示例来源:origin: line/armeria

VirtualHost decorate(@Nullable Function<Service<HttpRequest, HttpResponse>,
                    Service<HttpRequest, HttpResponse>> decorator) {
  if (decorator == null) {
    return this;
  }
  final List<ServiceConfig> services =
      this.services.stream().map(cfg -> {
        final PathMapping pathMapping = cfg.pathMapping();
        final Service<HttpRequest, HttpResponse> service = decorator.apply(cfg.service());
        final String loggerName = cfg.loggerName().orElse(null);
        return new ServiceConfig(pathMapping, service, loggerName);
      }).collect(Collectors.toList());
  return new VirtualHost(defaultHostname(), hostnamePattern(), sslContext(),
              services, producibleMediaTypes());
}

代码示例来源:origin: ehcache/ehcache3

private <T> Optional<T> extract(Function<BaseCacheType, T> extractor) {
  return sources.stream().map(s -> ofNullable(extractor.apply(s))).filter(Optional::isPresent).map(Optional::get).findFirst();
 }
}

相关文章

微信公众号

最新文章

更多