首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >VAVR编写尝试和列表

VAVR编写尝试和列表
EN

Stack Overflow用户
提问于 2019-05-10 06:56:07
回答 1查看 1.4K关注 0票数 2

我想知道使用VAVR's Try的惯用法是什么。我正在查看的用例必须执行以下步骤:

获取鞋的列表(该调用可以抛出选中的每一只鞋(该调用可以抛出每只鞋的选中的列表)(该调用可以抛出一个选中的列表exception)

  • clean exception)

  • restore exception)

  • return

以下是我的示例玩具代码,其中processRequest方法购买n双鞋,清洁和修复它们;如果有错误,则打印错误:

代码语言:javascript
复制
// definitions
ShoeStore java.util.List<Shoe> buy(int numberOfPairs) throws OutOfStockException;
ShoeCleaningService Shoe clean(Shoe dirtyShoe) throws OutOfShoePolishException;
ShoeRestoreService Shoe restore(Shoe oldShoe) throws OutOfSparePartsException;

class EnterpriseShoeService {
    // constructor
    ...

    public List<Shoe> processRequest(int numberOfPairs) {
        Try<List<Shoe>> shoes = Try.of(() -> shoeStore.buy(numberOfPairs));
        Try<List<Try<Shoe>>> cleanedAndRestoredShoes = shoes.map(xs -> xs.stream().map(shoe ->
                Try.success(shoe)
                        .andThenTry(shoeCleaningService::clean)
                        .andThenTry(shoeRestoreService::restore))
                .collect(Collectors.toList()));

        List<Shoe> result = cleanedAndRestoredShoes
                .getOrElseGet(err -> {
                    System.out.println(err.getMessage());
                    return Collections.emptyList();
                })
                .stream()
                .map(shoeTry -> shoeTry.onFailure(err -> System.out.println(err.getMessage())))
                .filter(Try::isSuccess)
                .map(Try::get)
                .collect(Collectors.toList());

        return result;

    }
}

我的问题是:如何简化这个逻辑?有没有可以消除的方法调用?可读性可以提高吗?

EN

回答 1

Stack Overflow用户

发布于 2019-05-12 01:36:12

代码语言:javascript
复制
import io.vavr.collection.List;
import io.vavr.control.Try;


public class TryListComposition {

   ShoeStore store;

   ShoeCleaningService cleaningService;

   ShoeRestoreService restoreService;

   public java.util.List<Shoe> processRequest(int numberOfPairs) {
    return processShoesRequest(numberOfPairs).getOrElse(List.empty()).toJavaList();
   }

   public Try<List<Shoe>> processShoesRequest(int numberOfPairs) {
      return this.buy(numberOfPairs)
            .map(shoes -> shoes
                    .map(this::cleanAndRestore)
                    .flatMap(x -> x)
            );
   }

   public Try<Shoe> cleanAndRestore(Shoe shoe) {
      return clean(shoe).flatMap(this::restore);
   }


   Try<List<Shoe>> buy(int numberOfPairs) {
      return Try.of(() -> 
        List.ofAll(store.buy(numberOfPairs).stream());
   }

   Try<Shoe> clean(Shoe dirtyShoe) {
      return Try.of(() -> cleaningService.clean(dirtyShoe));
   }

   Try<Shoe> restore(Shoe oldShoe) {
      return Try.of(() -> restoreService.restore(oldShoe));
   }

}

class Shoe {

}

interface ShoeStore {
   java.util.List<Shoe> buy(int numberOfPairs) throws 
   OutOfStockException;
}

interface ShoeCleaningService {
   Shoe clean(Shoe dirtyShoe) throws OutOfShoePolishException;
}

interface ShoeRestoreService {
   Shoe restore(Shoe oldShoe) throws OutOfSparePartsException;
}

class OutOfStockException extends Exception {

}

class OutOfShoePolishException extends Exception {

}

class OutOfSparePartsException extends Exception {

}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56068599

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档