简体   繁体   English

如何在Java8中强制立即评估流

[英]How to force an immediate evaluation on stream in Java8

I am wondering how can I force an immediate evaluate on each map function in Java8 ? 我想知道如何强制立即评估Java8中的每个map函数? The situation I have now is that I need to do multiple levels transforming (from ObjectA -> ObjectB -> ObjectC -> ObjectD), and there could be a failure (throw exception) on every level of this transforming for some object. 我现在的情况是我需要进行多个级别的转换(来自ObjectA - > ObjectB - > ObjectC - > ObjectD),并且在某个对象的转换的每个级别上都可能存在失败(抛出异常)。 For example 例如

 // stream -> map -> collect sequence
 lists.stream()
      .map(aToB)
      .collect(Collectors.toList())
      .stream()
      .map(bToC)
      .collect(Collectors.toList())
      .stream()
      .map(cToD)
      .collect(Collectors.toList())

 // Try api is from javaslangs
 Function<ObjectA, ObjectB> aToB = a -> Try.of(() -> .....)
                                           .onFailure(....)
                                           .get();

 Function<ObjectB, ObjectC> bToC = b -> Try.of(() -> .....)
                                           .onFailure(....)
                                           .get();

 Function<ObjectC, ObjectD> cToD = c -> Try.of(() -> .....)
                                           .onFailure(....)
                                           .get();

I wanna to test each transition in my unit test such as test if the test thrown an exception, and if A unsuccessfully transformed to C when there's an exception when transforming A to B something like that, but with lazy evaluation, this becomes impossible to test, and the only way I can think of is to do this sequence of steam() -> map(...) -> collect(...) calls to force an immediate evaluation. 我想测试我的单元测试中的每个转换,例如测试是否测试抛出异常,以及如果A在将A转换为B之类的异常时未成功转换为C,但是使用惰性求值,则无法测试,我能想到的唯一方法是执行这个序列的steam() -> map(...) -> collect(...)调用以强制立即进行评估。 I am wondering is there a better way to write this. 我想知道是否有更好的方式来写这个。

Some simple thoughts here: 一些简单的想法:

  • Test the whole chain of streaming operations on single-element collections. 测试单元素集合上的整个流操作链。 Verify that exceptions are thrown where you expect. 验证在您期望的位置抛出异常。
  • Don't worry about testing the stream framework; 不要担心测试流框架; just apply the functions directly, yourself, in tests, rather than going through the streams. 只是直接在测试中应用函数,而不是通过流。

Generally speaking, don't try to deal with the lazy evaluation in your tests: test the functions, and trust the stream framework to do the right thing in real code. 一般来说,不要试图在测试中处理延迟评估:测试函数,并信任流框架在实际代码中做正确的事情。

You can write a static helper method that collects into a collection and then returns that collection's stream and wrap individual steps of the stream pipeline into that helper. 您可以编写一个静态帮助器方法,该方法收集到一个集合中,然后返回该集合的流并将流管道的各个步骤包装到该帮助器中。

You should be aware that this alters the semantics of the stream. 您应该知道这会改变流的语义。

For example an infinite stream source might fail if handled in this matter even though the complete stream pipeline might succeed if it is terminated with a short-circuiting operation. 例如,如果在此问题中处理,则无限流源可能会失败,即使完整流管道如果以短路操作终止也可能成功。 Similarly parallelism and concurrency properties might get altered. 类似地,并行性和并发性可能会发生变化。 So you would end up testing something different from the stream pipeline itself. 因此,您最终将测试与流管道本身不同的东西。

Instead it's probably a better idea to just test the full stream pipeline to deliver the expected outputs for possible input scenarios and possibly test the mapping Function separately on various kinds of inputs. 相反,仅测试全流管道以提供可能的输入场景的预期输出并且可能分别在各种输入上测试映射功能可能是更好的想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM