Skip to content

Commit 5e2e6f3

Browse files
Observable Exercises, Examples and Unit Tests
1 parent 3ff1b63 commit 5e2e6f3

30 files changed

+1978
-364
lines changed

src/main/java/learnrxjava/ComposableListExercises.java

Lines changed: 9 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,12 @@
99
import java.util.function.Predicate;
1010

1111
import learnrxjava.types.Bookmark;
12-
import learnrxjava.types.BookmarkRow;
1312
import learnrxjava.types.BoxArt;
14-
import learnrxjava.types.BoxArtRow;
1513
import learnrxjava.types.ComposableList;
1614
import learnrxjava.types.InterestingMoment;
1715
import learnrxjava.types.JSON;
1816
import learnrxjava.types.MovieList;
19-
import learnrxjava.types.MovieListRow;
2017
import learnrxjava.types.Video;
21-
import learnrxjava.types.VideoRow;
2218

2319
/**
2420
* Mastering concurrency is challenging, But we can make it much easier by simply choosing the right
@@ -473,7 +469,7 @@ One approach could be to select an item in the list as the assumed largest numbe
473469
If it's larger, we keep track of it. Finally we're left with a single boxart which must necessarily be the largest.
474470
*/
475471
public static BoxArt exercise13() {
476-
ComposableListSolution<BoxArt> boxarts = ComposableListSolution.of(
472+
ComposableListSolutions<BoxArt> boxarts = ComposableListSolutions.of(
477473
new BoxArt(200, 200, "http://cdn-0.nflximg.com/images/2891/Fracture200.jpg"),
478474
new BoxArt(150, 200, "http://cdn-0.nflximg.com/images/2891/Fracture150.jpg"),
479475
new BoxArt(425, 150, "http://cdn-0.nflximg.com/images/2891/Fracture425.jpg"),
@@ -977,151 +973,6 @@ public static ComposableList<JSON> exercise23() {
977973
throw new UnsupportedOperationException("Not implemented yet.");
978974
}
979975

980-
/*
981-
Exercise 24: Converting Lists to Trees
982-
983-
Now that we've learned the five operators let's flex our muscles and write some powerful queries.
984-
985-
When information is organized hierarchically, parent hold references to their children. In relational systems like databases, children hold references to their parents. Both ways of organizing information are equivalent, and depending on the circumstances we might get data organized in one way or another. It may surprise you to learn that you can use the methods you already know to easily convert between these representations. In other words, not only can you transform trees into lists, you can transform lists into trees.
986-
987-
We have 2 lists each containing lists, and videos respectively. Each video has a listId field indicating its parent list. We want to build a list of movie list objects, each with a name and a videos array. The videos list will contain the video's id and title. In other words we want to build the following structure:
988-
*/
989-
public static ComposableList<List<MovieList>> exercise24() {
990-
ComposableListExercises<MovieListRow> lists = ComposableListExercises.of(
991-
new MovieListRow(
992-
5434364,
993-
"New Releases"
994-
),
995-
new MovieListRow(
996-
65456475,
997-
"Thrillers"
998-
)
999-
);
1000-
ComposableList<VideoRow> videos = ComposableListExercises.of(
1001-
new VideoRow(
1002-
5434364,
1003-
65432445,
1004-
"The Chamber"
1005-
),
1006-
new VideoRow(
1007-
5434364,
1008-
675465,
1009-
"Fracture"
1010-
),
1011-
new VideoRow(
1012-
65456475,
1013-
70111470,
1014-
"Die Hard"
1015-
),
1016-
new VideoRow(
1017-
65456475,
1018-
654356453,
1019-
"Bad Boys"
1020-
)
1021-
);
1022-
1023-
// return lists. // complete this expression
1024-
throw new UnsupportedOperationException("Not implemented yet.");
1025-
}
1026-
1027-
/*
1028-
Exercise 25: Converting Lists to Deeper Trees
1029-
1030-
Let's try creating a deeper tree structure. This time we have 4 seperate lists each containing movie lists, videos, boxarts, and bookmarks respectively. Each object has a parent id, indicating its parent. We want to build a list of movie list objects, each with a name and a videos array. The videos list will contain the video's id, title, bookmark time, and smallest boxart url. In other words we want to build the following structure:
1031-
1032-
[
1033-
{
1034-
"name": "New Releases",
1035-
"videos": [
1036-
{
1037-
"id": 65432445,
1038-
"title": "The Chamber",
1039-
"time": 32432,
1040-
"boxart": "http://cdn-0.nflximg.com/images/2891/TheChamber130.jpg"
1041-
},
1042-
{
1043-
"id": 675465,
1044-
"title": "Fracture",
1045-
"time": 3534543,
1046-
"boxart": "http://cdn-0.nflximg.com/images/2891/Fracture120.jpg"
1047-
}
1048-
]
1049-
},
1050-
{
1051-
"name": "Thrillers",
1052-
"videos": [
1053-
{
1054-
"id": 70111470,
1055-
"title": "Die Hard",
1056-
"time": 645243,
1057-
"boxart": "http://cdn-0.nflximg.com/images/2891/DieHard150.jpg"
1058-
},
1059-
{
1060-
"id": 654356453,
1061-
"title": "Bad Boys",
1062-
"time": 984934,
1063-
"boxart": "http://cdn-0.nflximg.com/images/2891/BadBoys140.jpg"
1064-
}
1065-
]
1066-
}
1067-
]
1068-
*/
1069-
public static ComposableList<List<MovieList>> exercise25() {
1070-
ComposableListExercises<MovieListRow> lists = ComposableListExercises.of(
1071-
new MovieListRow(
1072-
5434364,
1073-
"New Releases"
1074-
),
1075-
new MovieListRow(
1076-
65456475,
1077-
"Thrillers"
1078-
)
1079-
);
1080-
ComposableListExercises<VideoRow> videos = ComposableListExercises.of(
1081-
new VideoRow(
1082-
5434364,
1083-
65432445,
1084-
"The Chamber"
1085-
),
1086-
new VideoRow(
1087-
5434364,
1088-
675465,
1089-
"Fracture"
1090-
),
1091-
new VideoRow(
1092-
65456475,
1093-
70111470,
1094-
"Die Hard"
1095-
),
1096-
new VideoRow(
1097-
65456475,
1098-
654356453,
1099-
"Bad Boys"
1100-
)
1101-
);
1102-
ComposableListExercises<BoxArtRow> boxarts = ComposableListExercises.of(
1103-
new BoxArtRow(65432445, 130, 200, "http://cdn-0.nflximg.com/images/2891/TheChamber130.jpg"),
1104-
new BoxArtRow(65432445, 200, 200, "http://cdn-0.nflximg.com/images/2891/TheChamber200.jpg"),
1105-
new BoxArtRow(675465, 200, 200, "http://cdn-0.nflximg.com/images/2891/Fracture200.jpg"),
1106-
new BoxArtRow(675465, 120, 200, "http://cdn-0.nflximg.com/images/2891/Fracture120.jpg"),
1107-
new BoxArtRow(675465, 300, 200, "http://cdn-0.nflximg.com/images/2891/Fracture300.jpg"),
1108-
new BoxArtRow(70111470, 150, 200, "http://cdn-0.nflximg.com/images/2891/DieHard150.jpg"),
1109-
new BoxArtRow(70111470, 200, 200, "http://cdn-0.nflximg.com/images/2891/DieHard200.jpg"),
1110-
new BoxArtRow(654356453, 200, 200, "http://cdn-0.nflximg.com/images/2891/BadBoys200.jpg"),
1111-
new BoxArtRow(654356453, 140, 200, "http://cdn-0.nflximg.com/images/2891/BadBoys140.jpg")
1112-
);
1113-
ComposableListExercises<BookmarkRow> bookmarks = ComposableListExercises.of(
1114-
new BookmarkRow(65432445, 32432),
1115-
new BookmarkRow(675465, 3534543),
1116-
new BookmarkRow(70111470, 645243),
1117-
new BookmarkRow(654356453, 984934)
1118-
);
1119-
1120-
// return lists. // complete this expression
1121-
1122-
throw new UnsupportedOperationException("Not implemented yet.");
1123-
1124-
}
1125976

1126977
// This function can be used to build JSON objects within an expression
1127978
private static JSON json(Object... keyOrValue) {
@@ -1143,4 +994,12 @@ public static <T> ComposableListExercises<T> of(T... args) {
1143994
}
1144995
return results;
1145996
}
997+
998+
public static <T> ComposableListSolutions<T> of(List<T> list) {
999+
ComposableListSolutions<T> results = new ComposableListSolutions<T>();
1000+
for (T value : list) {
1001+
results.add(value);
1002+
}
1003+
return results;
1004+
}
11461005
}

0 commit comments

Comments
 (0)