package crosscuttingconcerns;

import java.util.function.Consumer;

import org.requirementsascode.SystemReactionTrigger;
import org.requirementsascode.UseCaseRunner;

public class CrossCuttingConcerns01_MeasurePerformanceExample {
	private int resultOfAddition;
	private double resultOfPower;
	
	public void start() {
		UseCaseRunner useCaseRunner = new UseCaseRunner();
		useCaseRunner.adaptSystemReaction(withMeasuringPerformance());
		
		useCaseRunner.useCaseModel().useCase("Measure performance of simple mathematical operations")
			.basicFlow()
				.step("S1").system(addTwoNumbers())
				.step("S2").system(calculateTwoToThePowerOfAThousand())
				.step("S3").system(displayResults());
		
		useCaseRunner.run();
	}

	private Consumer<SystemReactionTrigger> withMeasuringPerformance() {
		return systemReactionTrigger -> {
			long timeBefore = System.nanoTime();
			systemReactionTrigger.trigger();
			long timeafter = System.nanoTime();
			long timeElapsed = timeafter - timeBefore;
			
			System.out.println("Step " + systemReactionTrigger.useCaseStep() + 
				" took " + timeElapsed + " nanoseconds.");
		};
	}

	private Runnable addTwoNumbers() {
		return () -> {
			resultOfAddition = 123456789 + 123456789;
		};
	}
	
	private Runnable calculateTwoToThePowerOfAThousand() {
		return () -> {
			resultOfPower = Math.pow(2, 1000);
		};
	}
	
	private Runnable displayResults() {
		return () ->{
			System.out.println("Result of addition: " + resultOfAddition);	
			System.out.println("Result of 2 to the power of a thousand = " + resultOfPower);	
		};
	}

	public static void main(String[] args){
		new CrossCuttingConcerns01_MeasurePerformanceExample().start();
	}
}

@@ -28,45 +28,8 @@
 s;%0A%0A
-import java.util.function.Consumer;%0A%0A
 impo
@@ -77,16 +77,16 @@
 rigger;%0A
+
 import o
@@ -363,29 +363,29 @@
 eaction(
-withM
+this::m
 easur
-ing
+e
 Performa
@@ -387,18 +387,16 @@
 formance
-()
 );%0A%09%09%0A%09%09
@@ -672,24 +672,25 @@
 r.run();%0A%09%7D%0A
+%09
 %0A%09private Co
@@ -691,77 +691,53 @@
 ate 
-Consumer%3CSystemReactionTrigger%3E withMeasuringPerformance() %7B%0A%09%09return
+void measurePerformance(SystemReactionTrigger
  sys
@@ -754,19 +754,17 @@
 nTrigger
- -%3E
+)
  %7B%0A%09%09%09lo
@@ -994,16 +994,16 @@
 ep() + %0A
+
 %09%09%09%09%22 to
@@ -1041,21 +1041,16 @@
 nds.%22);%0A
-%09%09%7D;%0A
 %09%7D%0A%0A%09pri

Refactor cross cutting concerns example