Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. public final class RxJavaStrategy extends AbstractStrategy<Strategy.ProviderStrategy> implements Strategy.ProviderStrategy {
  2.  
  3. @Override
  4. public <C> void apply(final Bytecode<C> bytecode) {
  5. if (bytecode.getParent().isEmpty()) { // root bytecode
  6. final String id = UUID.randomUUID().toString();
  7. bytecode.addSourceInstruction(RxJavaProcessor.RX_BYCODE_ID, id);
  8. } else if (!BytecodeUtil.hasSourceInstruction(bytecode, CommonCompiler.Symbols.WITH_PROCESSOR)) {
  9. if (RxJavaStrategy.isSimple(bytecode)) {
  10. bytecode.addSourceInstruction(CommonCompiler.Symbols.WITH_PROCESSOR, RxJavaProcessor.class, Map.of(RxJavaProcessor.RXJAVA_THREADS, 0)); // guaranteed serial execution
  11. } else {
  12. final Bytecode<C> root = BytecodeUtil.getRootBytecode(bytecode);
  13. final List<SourceInstruction> processors = BytecodeUtil.getSourceInstructions(root, CommonCompiler.Symbols.WITH_PROCESSOR); // potential parallel execution
  14. for (final SourceInstruction sourceInstruction : processors) {
  15. bytecode.addSourceInstruction(sourceInstruction.op(), sourceInstruction.args());
  16. }
  17. }
  18. }
  19. }
  20.  
  21. private static boolean isSimple(final Bytecode<?> bytecode) {
  22. final CompositeCompiler compiler = BytecodeUtil.getCompilers(bytecode);
  23. return bytecode.getInstructions().size() < 4 && bytecode.getInstructions().stream().noneMatch(i -> {
  24. final FunctionType functionType = compiler.getFunctionType(i.op());
  25. return FunctionType.FLATMAP == functionType || FunctionType.BRANCH == functionType;
  26. });
  27. }
  28.  
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement