Advertisement
Guest User

Untitled

a guest
Jul 21st, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.64 KB | None | 0 0
  1. import com.google.auto.value.AutoValue;
  2. import io.vavr.collection.List;
  3.  
  4. import static io.vavr.API.*;
  5. import static io.vavr.Predicates.instanceOf;
  6.  
  7. interface Recommendation {
  8.  
  9.     String id();
  10. }
  11.  
  12. @AutoValue
  13. abstract class EventRecommendation implements Recommendation {
  14.  
  15.     public abstract String id();
  16.  
  17.     public abstract int numberOfEpisodeTitles();
  18.  
  19.     public static EventRecommendation create(String id, int numberOfEpisodeTitles) {
  20.         return new AutoValue_EventRecommendation(id, numberOfEpisodeTitles);
  21.     }
  22.  
  23. }
  24.  
  25. @AutoValue
  26. abstract class ContentRecommendation implements Recommendation {
  27.  
  28.     public abstract String id();
  29.  
  30.     public static ContentRecommendation create(String id) {
  31.         return new AutoValue_ContentRecommendation(id);
  32.     }
  33. }
  34.  
  35. public final class Test {
  36.  
  37.     public static void main(String[] args) {
  38.         List<Recommendation> recommendations = List.of(
  39.                 EventRecommendation.create("event-111", 2),
  40.                 EventRecommendation.create("event-222", 3),
  41.                 EventRecommendation.create("event-333", 1),
  42.                 ContentRecommendation.create("content-111"),
  43.                 ContentRecommendation.create("content-222"),
  44.                 ContentRecommendation.create("content-333"));
  45.         int sum = recommendations.toStream()
  46.                 .map(recommendation -> Match(recommendation).of(
  47.                         Case($(instanceOf(EventRecommendation.class)), EventRecommendation::numberOfEpisodeTitles),
  48.                         Case($(), 0)))
  49.                 .sum()
  50.                 .intValue();
  51.         System.out.println(sum); // should be 6
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement