Skip to content

Commit

Permalink
fix rx java dep, add part4, progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Jul 19, 2024
1 parent b27d42d commit 9c4cdba
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 6 deletions.
17 changes: 15 additions & 2 deletions dev_projects/RxJava/RxJava/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,23 @@
rxjava
https://mvnrepository.com/artifact/io.reactivex/rxjava
-->
<!-- <dependency>-->
<!-- <groupId>io.reactivex</groupId>-->
<!-- <artifactId>rxjava</artifactId>-->
<!-- <version>1.3.8</version>-->
<!-- </dependency>-->

<!-- https://mvnrepository.com/artifact/com.netflix.rxjava/rxjava-core -->
<dependency>
<groupId>com.netflix.rxjava</groupId>
<artifactId>rxjava-core</artifactId>
<version>0.20.7</version>
</dependency>

<dependency>
<groupId>io.reactivex</groupId>
<groupId>io.reactivex.rxjava3</groupId>
<artifactId>rxjava</artifactId>
<version>1.3.8</version>
<version>3.1.6</version>
</dependency>

</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
import javax.swing.*;
import rx.Completable;

import io.reactivex.rxjava3.core.Completable;
import rx.Observable;
import rx.functions.Action0;

public class part3 {
public static void main(String[] args) {
Expand Down Expand Up @@ -36,6 +36,7 @@ public static void main(String[] args) {
System.out.println("5 sec passed " + item);
});

// TODO : FIX BELOW
//Action action2 = () -> System.out.println("hello world!!");
Action action =
new AbstractAction() {
Expand All @@ -45,7 +46,7 @@ public void actionPerformed(ActionEvent e) {
}
};

Completable completable = Completable.fromAction((Action0) action);
Completable completable = Completable.fromAction((io.reactivex.rxjava3.functions.Action) action);

completable.subscribe(() -> {
System.out.println("---> Action ends");
Expand Down
104 changes: 104 additions & 0 deletions dev_projects/RxJava/RxJava/src/main/java/com/yen/courseV1/part4.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package com.yen.courseV1;

// https://youtu.be/EIwAerzQDvA?si=NQNL6Lgl9tc9qsU8

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Maybe;
import io.reactivex.rxjava3.core.MaybeObserver;
import io.reactivex.rxjava3.core.Single;
import io.reactivex.rxjava3.core.SingleObserver;
import io.reactivex.rxjava3.disposables.Disposable;


public class part4 {
public static void main(String[] args) {

/** Example 1 : Single
*
* Single : ONLY emit once and ONLY one item, other situation treated as error
*/
// Single<String>, NOTE how this different from regular Obervable
Single<String> single = createSingle();
single.subscribe(new SingleObserver<String>(){
@Override
public void onSubscribe(@NonNull Disposable d) {

}

@Override
public void onSuccess(@NonNull String s) {
System.out.println("(onSuccess) s = " + s);
}

@Override
public void onError(@NonNull Throwable e) {
System.out.println("(onError) = " + e.getMessage());
}
});

/** Example 2 : Maybe
*
* Maybe : emit one or no item
*/

// Maybe
Maybe<String> maybe = createMaybe();

maybe.subscribe(new MaybeObserver<String>() {
@Override
public void onSubscribe(@NonNull Disposable d) {

}

@Override
public void onSuccess(@NonNull String s) {
System.out.println("(onSuccess) s = " + s);
}

@Override
public void onError(@NonNull Throwable e) {
System.out.println("(onError) = " + e.getMessage());
}

@Override
public void onComplete() {
System.out.println("completed! no new content");
}
});

}

public static Single<String> createSingle(){
return Single.create(emitter -> {
String user = fetchUser();
if (user != null){
emitter.onSuccess(user);
}else{
emitter.onError(new Exception("user not found"));
}
});
}

public static String fetchUser(){
//return "May";
return null;
}

public static Maybe createMaybe(){
return Maybe.create(emitter -> {
String newContent = readFile();
if (newContent != null){
emitter.onSuccess(newContent);
}else{
emitter.onComplete();
}
});
}

public static String readFile(){
//return "content of some file";
return null;
}

}

4 changes: 3 additions & 1 deletion dev_projects/RxJava/progress.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
## 20240719
- https://youtu.be/7mbjhNCWqvs?si=GtCY3Wk5AFTdjYgS
- https://youtu.be/ApUUN55V2RE?si=zOpzZ14He9IzmqGZ
- https://youtu.be/ApUUN55V2RE?si=ZpZ6K7coesyXk3nO
- https://youtu.be/ApUUN55V2RE?si=ZpZ6K7coesyXk3nO
- https://youtu.be/EIwAerzQDvA?si=RufdyI47fIpTJ0JU
- https://youtu.be/EIwAerzQDvA?si=YyLHTw7Wut_qw8QZ

0 comments on commit 9c4cdba

Please sign in to comment.