placidenduwayo 2 lat temu
rodzic
commit
38196a3a9b

+ 4
- 3
src/main/java/fr/natan/akkastreamfileprocessingapi/controller/TvSeriesController.java Wyświetl plik

@@ -13,6 +13,7 @@ import org.springframework.web.bind.annotation.RequestMethod;
13 13
 import org.springframework.web.bind.annotation.RestController;
14 14
 import play.api.libs.json.JsValue;
15 15
 import play.api.libs.json.Json;
16
+import scala.Option;
16 17
 import scala.collection.IndexedSeq;
17 18
 import scala.concurrent.Future;
18 19
 
@@ -35,7 +36,7 @@ public class TvSeriesController {
35 36
 
36 37
     @RequestMapping(value = "/persons/id/{personID}", method = RequestMethod.GET)
37 38
     private String getPersonByID(@PathVariable(name = "personID") String personID) throws ExecutionException, InterruptedException {
38
-        Future<ModelsAndJsonMap.Person> futurePerson = akkaStreamFilesProcessing.getPersonByIdFuture(personID);
39
+        Future<Option<ModelsAndJsonMap.Person>> futurePerson = akkaStreamFilesProcessing.getPersonByIdFuture(personID);
39 40
         CompletableFuture<ModelsAndJsonMap.Person> completableFuture = CompletableFutureBuilder
40 41
                 .buildcompletableFuture1(futurePerson);
41 42
         ModelsAndJsonMap.Person person = completableFuture.get();
@@ -48,7 +49,7 @@ public class TvSeriesController {
48 49
     private String getPersonByName(@PathVariable(name = "primaryName") String primaryName)
49 50
             throws ExecutionException, InterruptedException {
50 51
 
51
-        Future<ModelsAndJsonMap.Person> personFuture = akkaStreamFilesProcessing.getPersonByNameFuture(primaryName);
52
+        Future<Option<ModelsAndJsonMap.Person>> personFuture = akkaStreamFilesProcessing.getPersonByNameFuture(primaryName);
52 53
         CompletableFuture<ModelsAndJsonMap.Person> personCompletableFuture = CompletableFutureBuilder
53 54
                 .buildcompletableFuture1(personFuture);
54 55
         ModelsAndJsonMap.Person person = personCompletableFuture.get();
@@ -59,7 +60,7 @@ public class TvSeriesController {
59 60
     @RequestMapping(value = "/tvseries/id/{tvSerieId}", method = RequestMethod.GET)
60 61
     private String getTvSerieByID(@PathVariable(name = "tvSerieId") String tvSerieID)
61 62
             throws ExecutionException, InterruptedException {
62
-        Future<ModelsAndJsonMap.TvSerie> tvSerieFuture = akkaStreamFilesProcessing.getTvSerieByIdFuture(tvSerieID);
63
+        Future<Option<ModelsAndJsonMap.TvSerie>> tvSerieFuture = akkaStreamFilesProcessing.getTvSerieByIdFuture(tvSerieID);
63 64
         CompletableFuture<ModelsAndJsonMap.TvSerie> tvSerieCompletableFuture = CompletableFutureBuilder
64 65
                 .buildcompletableFuture1(tvSerieFuture);
65 66
 

+ 3
- 2
src/main/java/fr/natan/akkastreamfileprocessingapi/futurecompleteness/CompletableFutureBuilder.java Wyświetl plik

@@ -1,5 +1,6 @@
1 1
 package fr.natan.akkastreamfileprocessingapi.futurecompleteness;
2 2
 
3
+import scala.Option;
3 4
 import scala.collection.IndexedSeq;
4 5
 import scala.concurrent.Future;
5 6
 
@@ -12,7 +13,7 @@ import java.util.logging.Logger;
12 13
 public class CompletableFutureBuilder<T> {
13 14
 
14 15
    private static final Logger logger = Logger.getLogger("log");
15
-    public static<T> CompletableFuture<T> buildcompletableFuture1(Future<T> futureT) {
16
+    public static<T> CompletableFuture<T> buildcompletableFuture1(Future<Option<T>> futureT) {
16 17
         CompletableFuture<T> completableFuture = new CompletableFuture<>();
17 18
 
18 19
 
@@ -24,7 +25,7 @@ public class CompletableFutureBuilder<T> {
24 25
                     throw new RuntimeException(e);
25 26
                 }
26 27
             }
27
-            T task = futureT.value().get().get();
28
+            T task = futureT.value().get().get().get();
28 29
             completableFuture.complete(task);
29 30
         });
30 31
 

+ 3
- 3
src/main/scala/fr/natan/akkastreamfileprocessingapi/service/AkkaStreamFileProcessingFuture.scala Wyświetl plik

@@ -6,10 +6,10 @@ import fr.natan.akkastreamfileprocessingapi.models.ModelsAndJsonMap.{Person, TvS
6 6
 import scala.concurrent.Future
7 7
 
8 8
 trait AkkaStreamFileProcessingFuture {
9
-  def getPersonByIdFuture(personID: String): Future[Person]
10
-  def getPersonByNameFuture(primaryName: String):  Future[Person]
9
+  def getPersonByIdFuture(personID: String): Future[Option[Person]]
10
+  def getPersonByNameFuture(primaryName: String):  Future[Option[Person]]
11 11
 
12
-  def getTvSerieByIdFuture(tvSerieID: String): Future[TvSerie]
12
+  def getTvSerieByIdFuture(tvSerieID: String): Future[Option[TvSerie]]
13 13
   def getTvSeriesByPrimaryTitleFuture(tvSerieTitle: String):Future[IndexedSeq[TvSerie]]
14 14
 
15 15
   def getPersonsForTvSerieByTvSerieTitleFuture(tvSeriePrimaryTitle: String): Future[IndexedSeq[Person]]

+ 10
- 10
src/main/scala/fr/natan/akkastreamfileprocessingapi/service/AkkaStreamFileProcessingImpl.scala Wyświetl plik

@@ -24,13 +24,13 @@ class AkkaStreamFileProcessingImpl extends AkkaStreamFileProcessingFuture {
24 24
 
25 25
   implicit val logger: Logger = Logger(LoggerFactory.getLogger(this.getClass))
26 26
 
27
-  override def getPersonByIdFuture(personID: String): Future[Person] = {
27
+  override def getPersonByIdFuture(personID: String): Future[Option[Person]] = {
28 28
     val source: Source[Map[String, String], _] = buildAndValidateSource(inputFile = nameBasics)
29 29
 
30 30
     val start: Long = System.currentTimeMillis()
31
-    val personFuture: Future[Person] = source
31
+    val personFuture: Future[Option[Person]] = source
32 32
       .via(flow = filterPersonByIdFlow(personID = personID))
33
-      .runWith(Sink.head[Person])
33
+      .runWith(Sink.headOption[Person])
34 34
 
35 35
     personFuture.andThen({
36 36
       case Failure(exception) => logger.error(s"!${exception.printStackTrace()}")
@@ -39,28 +39,28 @@ class AkkaStreamFileProcessingImpl extends AkkaStreamFileProcessingFuture {
39 39
     personFuture
40 40
   }
41 41
 
42
-  override def getPersonByNameFuture(primaryName: String): Future[Person] = {
42
+  override def getPersonByNameFuture(primaryName: String): Future[Option[Person]] = {
43 43
     val source: Source[Map[String, String], _] = buildAndValidateSource(inputFile = nameBasics)
44 44
 
45 45
     val start: Long = System.currentTimeMillis()
46
-    val personFuture: Future[Person] = source
46
+    val personFuture: Future[Option[Person]] = source
47 47
       .via(flow = filterPersonByNameFlow(primaryName = primaryName))
48
-      .runWith(Sink.head[Person])
48
+      .runWith(Sink.headOption[Person])
49 49
 
50 50
     personFuture
51 51
   }
52 52
 
53
-  override def getTvSerieByIdFuture(tvSerieID: String): Future[TvSerie] = {
53
+  override def getTvSerieByIdFuture(tvSerieID: String): Future[Option[TvSerie]] = {
54 54
     val source: Source[Map[String, String], _] = buildAndValidateSource(inputFile = titleBasics)
55 55
 
56 56
     val start: Long = System.currentTimeMillis()
57
-    val tvSerieFuture: Future[TvSerie] = source
57
+    val tvSerieFuture: Future[Option[TvSerie]] = source
58 58
       .via(flow = filterTvSerieByIdFlow(tvSerieID = tvSerieID))
59
-      .runWith(Sink.head[TvSerie])
59
+      .runWith(Sink.headOption[TvSerie])
60 60
 
61 61
     tvSerieFuture.onComplete({
62 62
       case Failure(exception) => logger.info(s"$exception")
63
-      case Success(value: TvSerie) =>
63
+      case Success(value: Option[TvSerie]) =>
64 64
         logger.info(s"$value")
65 65
         logger.info(s"SUCCESS, elapsed time:${(System.currentTimeMillis() - start) / 1000} sec")
66 66
     })

Powered by TurnKey Linux.