定义either类
在我们日常开发中,stream流里面如果出现了异常是没法直接抛出的,需要进行try-catch进行捕获然后进行处理,或者干脆直接抛出异常停止
这个时候我们需要一个either工具类,区分成功和失败的执行结果,并且单独对失败的这些结果进行处理,以下是一些简单的实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| package com.tthk.inland.ticket.core.utils.stream;
import java.util.List; import java.util.Random; import java.util.stream.Collectors; import java.util.stream.Stream;
public class Either<L, R> { public static void main(String[] args) { List<EitherUtil<String,String>> collect = Stream.iterate(1, i -> i + 1).limit(100).map(Either::readline).collect(Collectors.toList()); EitherUtil<String, List<String>> sequence = EitherUtil.sequence(collect, (s1, s2) -> s1 + s2); if(sequence.isLeft()){ System.out.println(sequence.getLeft()); }else{ List<String> right = sequence.getRight(); for (String s : right) { System.out.println(s); } } }
public static EitherUtil<String,String> readline(int i){ if((new Random().nextInt() %2) >= 0){ return EitherUtil.right("成功"); }else { return EitherUtil.left("错误"); } } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
| package com.tthk.inland.ticket.core.utils.stream;
import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor;
import java.util.List; import java.util.function.BinaryOperator; import java.util.function.Function; import java.util.stream.Collectors;
@Data @AllArgsConstructor @NoArgsConstructor public class EitherUtil<L,R> {
private L left;
private R right;
public boolean isLeft(){ return left != null; }
public boolean isRight(){ return right != null; }
public static <L,R> EitherUtil<L,R> left(L exception){ EitherUtil<L,R> e = new EitherUtil<L,R>(); e.left = exception; return e; }
public static <L,R> EitherUtil<L,R> right(R value){ EitherUtil<L,R> e = new EitherUtil<L,R>(); e.right = value; return e; }
public <T> EitherUtil<L,T> map(Function<R,T> function){ if(isLeft()){ return left(left); }else { return right(function.apply(right)); } }
public static <L,R> EitherUtil<L, List<R>> sequence(List<EitherUtil<L,R>> eitherList, BinaryOperator<L> accumulator){ if(eitherList.stream().allMatch(EitherUtil::isRight)){ return right(eitherList.stream().map(EitherUtil::getRight).collect(Collectors.toList())); }else {
return left(eitherList.stream().filter(EitherUtil::isLeft).map(EitherUtil::getLeft).reduce(accumulator).orElseThrow()); } }
public static <L,R> EitherUtil<L, List<R>> sequenceLift(List<EitherUtil<L,R>> eitherList, BinaryOperator<L> accumulator){ return left(eitherList.stream().filter(EitherUtil::isLeft).map(EitherUtil::getLeft).reduce(accumulator).orElseThrow()); } }
|