/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package learnrxjava; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.function.BiFunction; import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Predicate; /** * */ public class ComposableList extends ArrayList { public static ComposableList of(T... args) { ComposableList results = new ComposableList(); for (T value : args) { results.add(value); } return results; } public static class JSON extends HashMap { }; public static JSON json(Object... keyOrValue) { JSON json = new JSON(); for (int counter = 0; counter < keyOrValue.length; counter += 2) { json.put((String) keyOrValue[counter], keyOrValue[counter + 1]); } return json; } /** * @param args the command line arguments */ public static void main(String[] args) { (new ComposableList()).exercise1(); } /* Traversing a List */ public void exercise1() { ComposableList names = ComposableList.of("Ben", "Jafar", "Matt", "Priya", "Brian"); for (String name : names) { System.out.println(name); } } /* Traversing a ComposableList Unlike Lists and other collections that implement the Iterable interface, you can _not_ use Java’s for each loop to traverse a Stream. Instead Stream provides a forEach method to which you pass a lambda. The ComposableList invokes your lambda once for every item in the stream, and passes the item to the lambda each time. To match the Stream interface, our ComposableList also has a forEach method (inherited from ArrayList). Let's solve the same problem we solved above, but this time we'll use forEach() instead of the Java for each loop. Note that the code is very similar, and we get the same result whether we are using the Java for each syntax or the forEach method on the ComposableList. Note: This exercise is already finished, so just move on. */ public void exercise2() { ComposableList names = ComposableList.of("Ben", "Jafar", "Matt", "Priya", "Brian"); names.forEach(name -> { System.out.println(name); }); } /* Projecting Lists Applying a function to a value and creating a new value is called a projection. To project one List into another, we apply a projection function to each item in the List and collect the results in a new List. exercise 3: Project a list of videos into a list of {id,title} JSON objects using forEach For each video, add a projected {id, title} json to the videoAndTitlePairs List. You can create JSON objects like this: */ class Bookmark { public int id; public int offset; public Bookmark(int id, int offset) { this.id = id; this.offset = offset; } } class InterestingMoment { String type; int time; public InterestingMoment(String type, int time) { this.type = type; this.time = time; } } class Video { public int id; public String title; public double rating; private ComposableList bookmarks; private ComposableList boxarts; private ComposableList interestingMoments; public Video(int id, String title, double rating) { this.id = id; this.title = title; this.rating = rating; } public Video(int id, String title, double rating, ComposableList bookmarks, ComposableList boxarts) { this(id, title, rating); this.bookmarks = bookmarks; this.boxarts = boxarts; } public Video(int id, String title, double rating, ComposableList bookmarks, ComposableList boxarts, ComposableList interestingMoments) { this(id, title, rating); this.bookmarks = bookmarks; this.boxarts = boxarts; this.interestingMoments = interestingMoments; } } public ComposableList exercise3() { ComposableList