@@ -449,16 +449,20 @@ e.g.
449
449
import com.softwaremill.jox.structured.JoxScopeExecutionException ;
450
450
import com.softwaremill.jox.structured.Scopes ;
451
451
452
- ...
453
- try {
454
- Scopes . supervised(scope - > {
455
- throw new TestException (" x" );
456
- });
457
- } catch (JoxScopeExecutionException e) {
458
- e. unwrapAndThrow(OtherException . class, TestException . class, YetAnotherException . class);
459
- throw e;
452
+ public class Demo {
453
+ public static void main (String [] args ) throws ExecutionException , InterruptedException {
454
+ /* ... */
455
+ try {
456
+ Scopes . supervised(scope - > {
457
+ throw new TestException (" x" );
458
+ });
459
+ } catch (JoxScopeExecutionException e) {
460
+ e. unwrapAndThrow(OtherException . class, TestException . class, YetAnotherException . class);
461
+ throw e;
462
+ }
463
+ /* ... */
464
+ }
460
465
}
461
- ...
462
466
```
463
467
464
468
#### Other types of scopes & forks
@@ -546,89 +550,6 @@ public class Demo {
546
550
// result = 5
547
551
```
548
552
549
- ## (Hot) Streaming
550
-
551
- ### Dependency
552
-
553
- Maven:
554
-
555
- ``` xml
556
-
557
- <dependency >
558
- <groupId >com.softwaremill.jox</groupId >
559
- <artifactId >channel-ops</artifactId >
560
- <version >0.3.1</version >
561
- </dependency >
562
- ```
563
-
564
- Gradle:
565
-
566
- ``` groovy
567
- implementation 'com.softwaremill.jox:channel-ops:0.3.1'
568
- ```
569
-
570
- ### Usage
571
-
572
- Using this module you can run operations on streams which require starting background threads. To do that,
573
- you need to pass an active concurrency scope (started using ` supervised ` ) to the ` SourceOps ` constructor.
574
-
575
- Each method from ` SourceOps ` causes a new fork (virtual thread) to be started, which starts running its logic
576
- immediately (producing elements / consuming and transforming elements from the given source). Thus, this is an
577
- implementation of "hot streams".
578
-
579
- #### Creating streams
580
-
581
- Sources from iterables, or tick-sources, can be created by calling methods on ` SourceOps ` :
582
-
583
- ``` java
584
- import java.util.concurrent.ExecutionException ;
585
-
586
- import static com.softwaremill.jox.structured.Scopes.supervised ;
587
-
588
- public class Demo {
589
- public static void main (String [] args ) throws ExecutionException , InterruptedException {
590
- supervised(scope - > {
591
- new SourceOps (scope)
592
- .tick(500 , " tick" )
593
- .toSource()
594
- .forEach(v - > System . out. println(v));
595
- return null ; // unreachable, as `tick` produces infinitely many elements
596
- });
597
- }
598
- }
599
- ```
600
-
601
- A tick-source can also be used in the usual way, by calling ` .receive ` on it, or by using it in ` select ` 's clauses.
602
-
603
- #### Transforming streams
604
-
605
- Streams can be transformed by calling the appropriate methods on the object returned by
606
- ` SourceOps.forSource(scope, source) ` .
607
-
608
- ` collect ` combines the functionality of ` map ` and ` filter ` : elements are mapped, and when the mapping function returns
609
- ` null ` , the element is skipped:
610
-
611
- ``` java
612
- import java.util.List ;
613
- import java.util.concurrent.ExecutionException ;
614
-
615
- import static com.softwaremill.jox.structured.Scopes.supervised ;
616
-
617
- public class Demo {
618
- public static void main (String [] args ) throws ExecutionException , InterruptedException {
619
- var result = supervised(scope - > new SourceOps (scope)
620
- .fromIterable(List . of(1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ))
621
- .collect(n - > {
622
- if (n % 2 == 0 ) return null ;
623
- else return n * 10 ;
624
- })
625
- .toSource(). toList());
626
- System . out. println(" result = " + result);
627
- }
628
- }
629
- // result = [10, 30, 50, 70, 90]
630
- ```
631
-
632
553
## (Lazy) Streaming - Flows
633
554
634
555
### Dependency
0 commit comments