代码之家  ›  专栏  ›  技术社区  ›  Andrew Cheong

不存在类型变量U的实例,因此Foo符合CompletionStage<U>

  •  5
  • Andrew Cheong  · 技术社区  · 6 年前

    我已经做了几个小时了,但似乎无法解开这个问题。错误与此段代码有关:

    enter image description here

    错误本身是:

    不存在类型变量U的实例,因此getusersforadccountresponse符合CompletionStage

    在链的最开始 thenCompose s、 我擦掉了 return 使用IntelliJ的 “引入局部变量…” 查看整个链(包括第1118行)返回的类型的功能:

    enter image description here

    结果是

    final CompletionStage<U> uCompletionStage = ...
    

    但是您可以看到包含方法的返回类型是

    public CompletionStage<GetUsersForAdAccountResponse> ...
    

    GetUsersForAdAccountResponse ? (同样,通常 返回

    我还尝试在每个 一路上,他们似乎都是对的。每个产生一个 CompletionStage<Foo> ,下一个 然后合成 Foo 并产生一个 CompletionStage<Bar> ,等等。(在一次代码重组中,我确实看到 CompletionStage<CompletionStage<Foo>> 但我认为那是我自己重写的作品。)


    我不知道它是否有用,但这里是整个方法:

      @Override
      public CompletionStage<GetUsersForAdAccountResponse> getUsersForAdAccount(
          RequestContext context, GetUsersForAdAccountRequest request) {
    
        Uuid adAccountId = request.getAdAccountId();
    
        return verifyAuthorization(context,
            PortcullisTemplates.Action.GET_USERS_FOR_AD_ACCOUNT.getName(),
            portcullisTemplates.topOrganizationResource())
            .thenCompose(auditLogPrincipal -> jdbiExecutor.executeInTransaction(handler -> {
    
                  // We purposely safeguard the account lookup as well behind Portcullis.
                  AdAccountDao adAccountDao = handler.attach(AdAccountDao.class);
                  if (adAccountDao.getAdAccountById(adAccountId) == null) {
                    throw new ValidationException(SERVICE_NAME,
                        "Ad account not found: " + UuidUtils.toString(adAccountId));
                  }
    
                  AdAccountRoleUserMappingDao roleDao = handler.attach(AdAccountRoleUserMappingDao.class);
    
                  List<String> roleNames = request.getRoleNamesList();
                  return roleNames.isEmpty() ?
                         roleDao.getAdAccountRoleUserMappingsByAdAccount(adAccountId) :
                         roleDao.getAdAccountRoleUserMappingsByAdAccountAndRoles(adAccountId, roleNames);
    
            })).thenCompose(adAccountRoleUserMappings -> jdbiExecutor.execute(UserDao.class, userDao -> {
              return userDao
                  .getUsersBy]UserIds(
                      adAccountRoleUserMappings.stream()
                          .map(AdAccountRoleUserMapping::userId)
                          .collect(Collectors.toList())
                  ).stream()
                      .collect(Collectors.toMap(
                          User::userId,
                          user -> new EncryptedFieldsBuilder()
                              .firstName(user.encryptedFirstName())
                              .lastName(user.encryptedLastName())
                              .email(user.encryptedEmail())
                              .build()
                  ));
            }).thenCompose(
                userEncryptedFields -> padlockService.decryptUserAccounts(userEncryptedFields)
            ).thenCompose(decryptedUsers -> GetUsersForAdAccountResponse.newBuilder()
                .addAllUserWithRole(
                    adAccountRoleUserMappings.stream()
                        .filter(mapping -> decryptedUsers.containsKey(mapping.userId()))
                        .map(mapping -> UserWithRole.newBuilder()
                            .setAccount(decryptedUsers.get(mapping.userId()))
                            .setRoleName(mapping.roleName())
                            .build())
                        .collect(Collectors.toSet())
                ).build()
            ));
      }
    
    1 回复  |  直到 5 年前
        1
  •  4
  •   Misha    6 年前

    最后 thenCompose thenApply . 传递给它的函数返回 GetUsersForAdAccountResponse 那不是 CompletionStage .

    然后合成 flatMap 属于 CompletableFuture 完成阶段 把结果压平。