Skip to content

Lecture 6 & Lecture 11 #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
340 changes: 340 additions & 0 deletions .idea/caches/deviceStreaming.xml

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions .idea/runConfigurations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion src/main/java/com/rxmobileteam/lecture1/Exercise1Main.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.rxmobileteam.lecture1;

import com.rxmobileteam.lecture1.data.ProductDao;
import com.rxmobileteam.lecture1.factory.ProductServiceFactory;
import com.rxmobileteam.lecture1.service.Product;
import com.rxmobileteam.lecture1.service.ProductService;

public class Exercise1Main {
public static void main(String[] args) {
ProductService productService = new ProductServiceFactory().createProductService();
ProductDao dao = new ProductDao();
ProductService productService = new ProductServiceFactory().createProductService(dao);

Product iPhone12 = new Product(
"1",
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/rxmobileteam/lecture1/data/IDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.rxmobileteam.lecture1.data;

import com.rxmobileteam.lecture1.service.Product;

import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.Set;

public interface IDao {
boolean add(@NotNull Product product);
@NotNull Set<Product> findAll();
@NotNull List<Product> findByQuery(String query);
}
19 changes: 14 additions & 5 deletions src/main/java/com/rxmobileteam/lecture1/data/ProductDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import com.rxmobileteam.lecture1.service.Product;
import com.rxmobileteam.utils.ExerciseNotCompletedException;

import org.jetbrains.annotations.NotNull;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
Expand All @@ -14,7 +16,7 @@
* todo: 1. Implement a method {@link ProductDao#add(Product)} that store new product into the set
* todo: 2. Implement a method {@link ProductDao#findAll()} that returns a set of all products
*/
public class ProductDao {
public class ProductDao implements IDao {
private final Set<Product> products = new HashSet<>();

/**
Expand All @@ -24,8 +26,7 @@ public class ProductDao {
* @return {@code true} if a product was stored, {@code false} otherwise
*/
public boolean add(@NotNull Product product) {
// TODO: implement this method
throw new ExerciseNotCompletedException();
return products.add(product);
}

/**
Expand All @@ -35,8 +36,16 @@ public boolean add(@NotNull Product product) {
*/
@NotNull
public Set<Product> findAll() {
// TODO: implement this method
throw new ExerciseNotCompletedException();
return products;
}

@Override
public @NotNull List<Product> findByQuery(String query) {
return products.stream().filter(product ->
product.getName().toLowerCase().contains(
query.toLowerCase()) ||
product.getDescription().toLowerCase().contains(query.toLowerCase())
).toList();
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.rxmobileteam.lecture1.factory;

import com.rxmobileteam.lecture1.data.IDao;
import com.rxmobileteam.lecture1.service.ProductService;
import com.rxmobileteam.utils.ExerciseNotCompletedException;

import org.jetbrains.annotations.NotNull;

/**
Expand All @@ -17,8 +19,7 @@ public class ProductServiceFactory {
* @return ProductService
*/
@NotNull
public ProductService createProductService() {
// TODO: implement this method
throw new ExerciseNotCompletedException();
public ProductService createProductService(IDao dao) {
return new ProductService(dao);
}
}
8 changes: 8 additions & 0 deletions src/main/java/com/rxmobileteam/lecture1/service/Product.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,12 @@ public String toString() {
", price=" + price +
'}';
}

public @NotNull String getName() {
return name;
}

public @NotNull String getDescription() {
return description;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.rxmobileteam.lecture1.service;

import com.rxmobileteam.lecture1.data.IDao;
import com.rxmobileteam.lecture1.data.ProductDao;
import com.rxmobileteam.utils.ExerciseNotCompletedException;

import org.jetbrains.annotations.NotNull;

import java.util.List;
Expand All @@ -13,6 +15,11 @@
* TODO: 2. Using {@link ProductDao} implement method {@link ProductService#searchProducts(String)}
*/
public class ProductService {
private final IDao dao;

public ProductService(IDao dao) {
this.dao = dao;
}

/**
* Adds a new product to the system.
Expand All @@ -21,8 +28,7 @@ public class ProductService {
* @return {@code true} if a product was added, {@code false} otherwise.
*/
public boolean addProduct(@NotNull Product product) {
// TODO: implement this method
throw new ExerciseNotCompletedException();
return dao.add(product);
}

/**
Expand All @@ -33,7 +39,7 @@ public boolean addProduct(@NotNull Product product) {
*/
@NotNull
public List<Product> searchProducts(@NotNull String query) {
// TODO: implement this method
throw new ExerciseNotCompletedException();
List<Product> list = dao.findByQuery(query);
return dao.findByQuery(query);
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/rxmobileteam/lecture11/Riddle1.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ object Riddle1 {
*/
fun solve(value: Int): Observable<Int> {
// TODO: implement this method
throw ExerciseNotCompletedException()
return Observable.just(value)
}
}

Expand Down
11 changes: 8 additions & 3 deletions src/main/java/com/rxmobileteam/lecture11/Riddle10.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ object Riddle10 {
*
* Use case: Get some user data and perform a network request with the user data and have both data accessible afterwards.
*/
fun solve(first: Observable<Int>, function: (Int) -> Observable<String>): Observable<Pair<Int, String>> {
// TODO: implement this method
throw ExerciseNotCompletedException()
fun solve(
first: Observable<Int>,
function: (Int) -> Observable<String>
): Observable<Pair<Int, String>> {
return first.flatMap(
{ value -> function(value) },
{ value: Int, functionResult: String -> Pair(value, functionResult) }
)
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/rxmobileteam/lecture11/Riddle11.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.rxmobileteam.lecture11

import com.rxmobileteam.utils.ExerciseNotCompletedException
import io.reactivex.rxjava3.core.Observable
import java.util.concurrent.TimeUnit

object Riddle11 {
/**
Expand All @@ -10,7 +11,6 @@ object Riddle11 {
* Use case: Handle the click of a button right away but prevent double clicking by not handling multiple click events within a given time window.
*/
fun solve(source: Observable<Unit>): Observable<Unit> {
// TODO: implement this method
throw ExerciseNotCompletedException()
return source.throttleFirst(300L, TimeUnit.MILLISECONDS)
}
}
3 changes: 1 addition & 2 deletions src/main/java/com/rxmobileteam/lecture11/Riddle12.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ object Riddle12 {
* Use case: Getting a network error and you want to recover and show some default state.
*/
fun solve(source: Observable<Int>): Observable<Int> {
// TODO: implement this method
throw ExerciseNotCompletedException()
return source.onErrorReturnItem(5)
}
}
3 changes: 1 addition & 2 deletions src/main/java/com/rxmobileteam/lecture11/Riddle13.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ object Riddle13 {
* Use case: You only want to observe changes of a value but don't care if the same value has been emitted consecutively.
*/
fun solve(source: Observable<Int>): Observable<Int> {
// TODO: implement this method
throw ExerciseNotCompletedException()
return source.distinctUntilChanged()
}
}
3 changes: 1 addition & 2 deletions src/main/java/com/rxmobileteam/lecture11/Riddle14.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ object Riddle14 {
* Use case: Retry an operation for a number of times or until a valid error occurred.
*/
fun solve(source: Single<Unit>): Single<Unit> {
// TODO: implement this method
throw ExerciseNotCompletedException()
return source.retry(2) { t -> t !is IllegalArgumentException }
}
}
3 changes: 1 addition & 2 deletions src/main/java/com/rxmobileteam/lecture11/Riddle15.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ object Riddle15 {
* Use case: You have two sources of your data (cache & network request). You want to subscribe to both right away and keep the emission order.
*/
fun solve(first: Observable<Int>, second: Observable<Int>): Observable<Int> {
// TODO: implement this method
throw ExerciseNotCompletedException()
return Observable.concatEager(listOf(first, second))
}
}
5 changes: 3 additions & 2 deletions src/main/java/com/rxmobileteam/lecture11/Riddle16.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ object Riddle16 {
* Use case: The [source] Observable is a TextField and you want to issue a network request while disposing the old requests in case the user has typed something new.
*/
fun solve(source: Observable<String>, function: (String) -> Single<Int>): Observable<Int> {
// TODO: implement this method
throw ExerciseNotCompletedException()
return source.switchMapSingle {
function(it)
}
}
}
3 changes: 1 addition & 2 deletions src/main/java/com/rxmobileteam/lecture11/Riddle17.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ object Riddle17 {
* Use case: Reactive types are lazy by default. Hence, you might also want to get the value upon the subscription and not execution time.
*/
fun solve(function: () -> Int): Single<Int> {
// TODO: implement this method
throw ExerciseNotCompletedException()
return Single.fromSupplier(function)
}
}
3 changes: 1 addition & 2 deletions src/main/java/com/rxmobileteam/lecture11/Riddle18.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ object Riddle18 {
* Use case: You have multiple sources and want to get the data from either one and then be consistent and not switch between multiple sources.
*/
fun solve(first: Observable<Int>, second: Observable<Int>): Observable<Int> {
// TODO: implement this method
throw ExerciseNotCompletedException()
return Observable.amb(listOf(first, second))
}
}
5 changes: 3 additions & 2 deletions src/main/java/com/rxmobileteam/lecture11/Riddle19.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ object Riddle19 {
* Use case: Transform any listener into an Observable.
*/
fun solve(interaction: Interaction): Observable<Int> {
// TODO: implement this method
throw ExerciseNotCompletedException()
return Observable.create { emitter ->
interaction.listener = emitter::onNext
}
}

interface Interaction {
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/rxmobileteam/lecture11/Riddle2.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ object Riddle2 {
* Use case: You want to transform the data.
*/
fun solve(source: Observable<Int>): Observable<Int> {
// TODO: implement this method
throw ExerciseNotCompletedException()
return source.map { it + 1 }
}
}
3 changes: 1 addition & 2 deletions src/main/java/com/rxmobileteam/lecture11/Riddle20.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ object Riddle20 {
* Use case: There something you want to execute and in your UI you have multiple trigger points.
*/
fun solve(first: Observable<Int>, second: Observable<Int>): Observable<Int> {
// TODO: implement this method
throw ExerciseNotCompletedException()
return first.mergeWith(second)
}
}
3 changes: 1 addition & 2 deletions src/main/java/com/rxmobileteam/lecture11/Riddle21.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ object Riddle21 {
* Use case: Sometimes you can't do everything reactively and need to break out of it.
*/
fun solve(source: Observable<Int>): Int {
// TODO: implement this method
throw ExerciseNotCompletedException()
return source.blockingFirst()
}
}
3 changes: 1 addition & 2 deletions src/main/java/com/rxmobileteam/lecture11/Riddle22.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ object Riddle22 {
* Use case: Group related data while skipping over some of it.
*/
fun solve(source: Observable<Int>): Observable<List<Int>> {
// TODO: implement this method
throw ExerciseNotCompletedException()
return source.buffer(2, 3)
}
}
3 changes: 1 addition & 2 deletions src/main/java/com/rxmobileteam/lecture11/Riddle23.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ object Riddle23 {
* Use case: You get some data from a bad source and know for sure it's of a certain type that you require.
*/
fun solve(source: Observable<Any>): Observable<String> {
// TODO: implement this method
throw ExerciseNotCompletedException()
return source.map { it as String }
}
}
3 changes: 1 addition & 2 deletions src/main/java/com/rxmobileteam/lecture11/Riddle24.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ object Riddle24 {
* Use case: Know how many emissions have been sent out.
*/
fun solve(source: Observable<Any>): Single<Long> {
// TODO: implement this method
throw ExerciseNotCompletedException()
return source.count()
}
}
3 changes: 1 addition & 2 deletions src/main/java/com/rxmobileteam/lecture11/Riddle25.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ object Riddle25 {
* Use case: Continue with data if the stream is empty.
*/
fun solve(source: Observable<Int>): Observable<Int> {
// TODO: implement this method
throw ExerciseNotCompletedException()
return source.defaultIfEmpty(5)
}
}
Loading