diff --git a/PROJECT DESIGN PATTERNS b/PROJECT DESIGN PATTERNS new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/PROJECT DESIGN PATTERNS @@ -0,0 +1 @@ + diff --git a/PROJECT DESIGN PATTERNS CRISTE PETRE SORIN b/PROJECT DESIGN PATTERNS CRISTE PETRE SORIN new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/PROJECT DESIGN PATTERNS CRISTE PETRE SORIN @@ -0,0 +1 @@ + diff --git a/PROJECT DESIGN PATTERNS.zip b/PROJECT DESIGN PATTERNS.zip new file mode 100644 index 000000000..636f830e5 Binary files /dev/null and b/PROJECT DESIGN PATTERNS.zip differ diff --git a/Test GitHub b/Test GitHub new file mode 100644 index 000000000..b7f18bda2 --- /dev/null +++ b/Test GitHub @@ -0,0 +1,6 @@ +public class HelloWorld { + public static void main(String[] args) { + System.out.println("Hello, Java World !!!"); + } +} + diff --git a/_1_basics/src/main/java/code/_1_study/_1_hello_world/HelloWorld.java b/_1_basics/src/main/java/code/_1_study/_1_hello_world/HelloWorld.java index a426c46d8..213e56f5b 100644 --- a/_1_basics/src/main/java/code/_1_study/_1_hello_world/HelloWorld.java +++ b/_1_basics/src/main/java/code/_1_study/_1_hello_world/HelloWorld.java @@ -1,7 +1,14 @@ package code._1_study._1_hello_world; -public class HelloWorld { - public static void main(String[] args) { - System.out.println("hello world"); +public class HelloWorld +{ + public static void main(String[] args) + { + int i = 1; + do{ + System.out.print(i++); + } + while + (i !=5); } } \ No newline at end of file diff --git a/_1_basics/src/main/java/code/_1_study/_6_accessing_private_fields/AccessingPrivateFields.java b/_1_basics/src/main/java/code/_1_study/_6_accessing_private_fields/AccessingPrivateFields.java index d98cebf69..fc5692d7a 100644 --- a/_1_basics/src/main/java/code/_1_study/_6_accessing_private_fields/AccessingPrivateFields.java +++ b/_1_basics/src/main/java/code/_1_study/_6_accessing_private_fields/AccessingPrivateFields.java @@ -4,7 +4,7 @@ public class AccessingPrivateFields { public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException { - Student student = new Student("John", 21); + Student student = new Student("Petre", 21); readPrivateField(student); diff --git a/_1_basics/src/main/java/code/_3_in_class/Main.java b/_1_basics/src/main/java/code/_3_in_class/Main.java index 3b87b4563..6e436fb75 100644 --- a/_1_basics/src/main/java/code/_3_in_class/Main.java +++ b/_1_basics/src/main/java/code/_3_in_class/Main.java @@ -3,6 +3,6 @@ public class Main { public static void main(String[] args) { - //TODO put your code changes in here + for (int i = 1; i) } } \ No newline at end of file diff --git a/_9_reactive/pom.xml b/_9_reactive/pom.xml deleted file mode 100644 index 46ba59286..000000000 --- a/_9_reactive/pom.xml +++ /dev/null @@ -1,51 +0,0 @@ - - - - java.training - brevity - 1.0-SNAPSHOT - - 4.0.0 - - _9_reactive - - - - io.projectreactor - reactor-core - RELEASE - - - - io.projectreactor - reactor-test - test - RELEASE - - - - junit - junit - RELEASE - test - - - - - - - \ No newline at end of file diff --git a/_9_reactive/src/main/java/code/_1_study/Main.java b/_9_reactive/src/main/java/code/_1_study/Main.java deleted file mode 100644 index 115b9620e..000000000 --- a/_9_reactive/src/main/java/code/_1_study/Main.java +++ /dev/null @@ -1,8 +0,0 @@ -package code._1_study; - -public class Main { - - public static void main(String[] args) { - //TODO put your code changes in here - } -} \ No newline at end of file diff --git a/_9_reactive/src/main/java/code/_1_study/_1_create_flux/CreateFluxExample.java b/_9_reactive/src/main/java/code/_1_study/_1_create_flux/CreateFluxExample.java deleted file mode 100644 index 2dac417d7..000000000 --- a/_9_reactive/src/main/java/code/_1_study/_1_create_flux/CreateFluxExample.java +++ /dev/null @@ -1,36 +0,0 @@ -package code._1_study._1_create_flux; - -import reactor.core.publisher.Flux; - -import java.time.Duration; -import java.util.Arrays; -import java.util.List; -import java.util.stream.Stream; - -public class CreateFluxExample { - public static void main(String[] args) { - // Creates a Flux containing the values 1, 2, 3. - Flux integerFlux = Flux.just(1, 2, 3); - - // Creates a Flux containing "Hello", "foo" and "bar". - Flux stringFlux = Flux.just("Hello", "foo", "bar"); - - // Creates a Flux from an already existing Iterable, for example a List. - List stringList = Arrays.asList("Hello", "foo", "bar"); - Flux fluxFromList = Flux.fromIterable(stringList); - - // It works the same with Java Streams (which are not reactive). - Stream stringStream = stringList.stream(); - Flux fluxFromStream = Flux.fromStream(stringStream); - - // Creates a flux on a range. - Flux rangeFlux = Flux.range(1, 5); // Flux(1, 2, 3, 4, 5) - - // Creates a flux that generates a new value every 100 ms. - // The value is incremental, starting at 1. - Flux intervalFlux = Flux.interval(Duration.ofMillis(100)); - - // You can also create a Flux from another one, or from a Mono. - Flux fluxCopy = Flux.from(fluxFromList); - } -} diff --git a/_9_reactive/src/main/java/code/_1_study/_2_create_mono/CreateMonoExample.java b/_9_reactive/src/main/java/code/_1_study/_2_create_mono/CreateMonoExample.java deleted file mode 100644 index 36204ce89..000000000 --- a/_9_reactive/src/main/java/code/_1_study/_2_create_mono/CreateMonoExample.java +++ /dev/null @@ -1,18 +0,0 @@ -package code._1_study._2_create_mono; - -import reactor.core.publisher.Flux; -import reactor.core.publisher.Mono; - -public class CreateMonoExample { - public static void main(String[] args) { - // Creating a Mono containing "Hello World !". - Mono helloWorld = Mono.just("Hello World !"); - - // Creating an empty Mono - Mono empty = Mono.empty(); - - // Creating from a Flux. - Mono monoFromFlux = Mono.from(Flux.range(1, 10)); - // The above Mono contains the first value of the Flux. - } -} diff --git a/_9_reactive/src/main/java/code/_1_study/_3_java9_and_reactor/Java9AndReactorExample.java b/_9_reactive/src/main/java/code/_1_study/_3_java9_and_reactor/Java9AndReactorExample.java deleted file mode 100644 index b5138c802..000000000 --- a/_9_reactive/src/main/java/code/_1_study/_3_java9_and_reactor/Java9AndReactorExample.java +++ /dev/null @@ -1,62 +0,0 @@ -package code._1_study._3_java9_and_reactor; - -import reactor.core.publisher.DirectProcessor; - -import java.util.concurrent.Flow; -import java.util.concurrent.SubmissionPublisher; - -public class Java9AndReactorExample { - - public static void main(String[] args) { - java9Flux(); - reactorFlux(); - } - - private static void reactorFlux() { - DirectProcessor reactorFlux = DirectProcessor.create(); - reactorFlux - .filter(e -> e % 3 == 0) //has operators - .subscribe(System.out::println); - - reactorFlux.onNext(10); - reactorFlux.onNext(20); - reactorFlux.onNext(30); - } - - private static void java9Flux() { - SubmissionPublisher java9Flux = new SubmissionPublisher<>(); - java9Flux.subscribe(new Flow.Subscriber() { - private Flow.Subscription subscription; - - @Override - public void onSubscribe(Flow.Subscription subscription) { - this.subscription = subscription; - subscription.request(1); - System.out.println("waiting for elements ..."); - } - - @Override - public void onNext(Integer item) { - System.out.println("received: " + item); - this.subscription.request(1); - } - - @Override - public void onError(Throwable throwable) { - System.err.println(throwable.getMessage()); - } - - @Override - public void onComplete() { - System.out.println("completed"); - } - }); - java9Flux.consume(System.out::println); - - java9Flux.submit(1); - java9Flux.submit(2); - java9Flux.submit(3); - java9Flux.close(); - } - -} \ No newline at end of file diff --git a/_9_reactive/src/main/java/code/_1_study/_4_lazy_evaluation_and_immutability/LazyEvaluationAndImmutabilityExample.java b/_9_reactive/src/main/java/code/_1_study/_4_lazy_evaluation_and_immutability/LazyEvaluationAndImmutabilityExample.java deleted file mode 100644 index 0a1cbd717..000000000 --- a/_9_reactive/src/main/java/code/_1_study/_4_lazy_evaluation_and_immutability/LazyEvaluationAndImmutabilityExample.java +++ /dev/null @@ -1,21 +0,0 @@ -package code._1_study._4_lazy_evaluation_and_immutability; - -import java.util.stream.IntStream; - -public class LazyEvaluationAndImmutabilityExample { - public static void main(String[] args) { - int factor = 2; - IntStream stream = IntStream.range(0, 10) - .map(e -> { - System.out.println("multiplying for: " + e); - return e * factor; - }) - .filter(e -> e > 5); - //immutability: what will be the result if we will be able to mutate this ? - //factor = 5; - - //lazy evaluation: we don't need to multiply all numbers to get the first - int first = stream.findFirst().getAsInt(); - System.out.println("first: " + first); - } -} diff --git a/_9_reactive/src/main/java/code/_1_study/_5_reactor_schedulers/ReactorSchedulersExample.java b/_9_reactive/src/main/java/code/_1_study/_5_reactor_schedulers/ReactorSchedulersExample.java deleted file mode 100644 index 3f669e316..000000000 --- a/_9_reactive/src/main/java/code/_1_study/_5_reactor_schedulers/ReactorSchedulersExample.java +++ /dev/null @@ -1,61 +0,0 @@ -package code._1_study._5_reactor_schedulers; - -import reactor.core.publisher.Flux; -import reactor.core.scheduler.Scheduler; -import reactor.core.scheduler.Schedulers; - -public class ReactorSchedulersExample { - public static void main(String[] args) { - //Type type = Type.PUBLISH_ON; - Type type = Type.SUBSCRIBE_ON; - switch (type) { - case PUBLISH_ON: - System.out.println("#### using publishOn"); - publishOn(); - break; - case SUBSCRIBE_ON: - System.out.println("#### using subscribeOn"); - subscribeOn(); - } - } - - private static void publishOn() { - Scheduler s2 = Schedulers.newParallel("t2", 4); - Scheduler s3 = Schedulers.newParallel("t3", 4); - final Flux flux = Flux.range(1, 2) - .map(i -> { - System.out.println("map1 " + Thread.currentThread().getName()); - return i; - }) - .publishOn(s2) //from here downstream will use thread from s2 - .map(i -> { - System.out.println("map2 " + Thread.currentThread().getName()); - return i; - }) - .publishOn(s3) //from here downstream will use thread from s3 - .map(i -> { - System.out.println("map3 " + Thread.currentThread().getName()); - return i + ""; - }); - new Thread(() -> flux.subscribe(System.out::println), "t1").start(); - } - - private static void subscribeOn() { - Scheduler s = Schedulers.newParallel("t2", 4); - final Flux flux = Flux.range(1, 2) - .map(i -> { - System.out.println("map1 " + Thread.currentThread().getName()); - return 10 + i; - }) - .subscribeOn(s) //all operators will use thread from s - .map(i -> { - System.out.println("map2 " + Thread.currentThread().getName()); - return "value " + i; - }); - new Thread(() -> flux.subscribe(System.out::println), "t1").start(); - } - - private static enum Type { - PUBLISH_ON, SUBSCRIBE_ON - } -} diff --git a/_9_reactive/src/main/java/code/_2_challenge/Main.java b/_9_reactive/src/main/java/code/_2_challenge/Main.java deleted file mode 100644 index 1a75de613..000000000 --- a/_9_reactive/src/main/java/code/_2_challenge/Main.java +++ /dev/null @@ -1,8 +0,0 @@ -package code._2_challenge; - -public class Main { - - public static void main(String[] args) { - //TODO put your code changes in here - } -} \ No newline at end of file diff --git a/_9_reactive/src/main/java/code/_2_challenge/_1_flux/FluxChallenge.java b/_9_reactive/src/main/java/code/_2_challenge/_1_flux/FluxChallenge.java deleted file mode 100644 index 0ac3bc7c4..000000000 --- a/_9_reactive/src/main/java/code/_2_challenge/_1_flux/FluxChallenge.java +++ /dev/null @@ -1,55 +0,0 @@ -package code._2_challenge._1_flux; - -import reactor.core.publisher.Flux; - -import java.time.Duration; -import java.util.Arrays; - -public class FluxChallenge { - public static void main(String[] args) { - emptyFlux().subscribe(System.out::println); - System.out.println(); - - fooBarFluxFromValues().subscribe(System.out::println); - System.out.println(); - - fooBarFluxFromList().subscribe(System.out::println); - System.out.println(); - - counter().subscribe(System.out::println); - System.out.println(); - try { - Thread.sleep(1500); - } catch (InterruptedException e) { - e.printStackTrace(); - } - - errorFlux().subscribe(System.out::println); - System.out.println(); - } - - // Return an empty Flux - static Flux emptyFlux() { - return Flux.empty(); - } - - // Return a Flux that contains 2 values "foo" and "bar" without using an array or a collection - static Flux fooBarFluxFromValues() { - return Flux.just("foo", "bar"); - } - - // Create a Flux from a List that contains 2 values "foo" and "bar" - static Flux fooBarFluxFromList() { - return Flux.fromIterable(Arrays.asList("foo", "bar")); - } - - // Create a Flux that emits an IllegalStateException - static Flux errorFlux() { - return Flux.error(new IllegalStateException()); - } - - // Create a Flux that emits increasing values from 0 to 9 each 100ms - static Flux counter() { - return Flux.interval(Duration.ofMillis(100)).take(10); - } -} \ No newline at end of file diff --git a/_9_reactive/src/main/java/code/_2_challenge/_2_mono/MonoChallenge.java b/_9_reactive/src/main/java/code/_2_challenge/_2_mono/MonoChallenge.java deleted file mode 100644 index 70c786255..000000000 --- a/_9_reactive/src/main/java/code/_2_challenge/_2_mono/MonoChallenge.java +++ /dev/null @@ -1,39 +0,0 @@ -package code._2_challenge._2_mono; - -import reactor.core.publisher.Mono; - -public class MonoChallenge { - public static void main(String[] args) { - emptyMono().subscribe(System.out::println); - System.out.println(); - - monoWithNoSignal().subscribe(System.out::println); - System.out.println(); - - fooMono().subscribe(System.out::println); - System.out.println(); - - errorMono().subscribe(System.out::println); - System.out.println(); - } - - // Return an empty Mono - static Mono emptyMono() { - return Mono.empty(); - } - - // Return a Mono that never emits any signal - static Mono monoWithNoSignal() { - return Mono.never(); - } - - // Return a Mono that contains a "foo" value - static Mono fooMono() { - return Mono.just("foo"); - } - - // Create a Mono that emits an IllegalStateException - static Mono errorMono() { - return Mono.error(new IllegalStateException()); - } -} diff --git a/_9_reactive/src/main/java/code/_2_challenge/_3_operations/OperationsChallenge.java b/_9_reactive/src/main/java/code/_2_challenge/_3_operations/OperationsChallenge.java deleted file mode 100644 index 164c28823..000000000 --- a/_9_reactive/src/main/java/code/_2_challenge/_3_operations/OperationsChallenge.java +++ /dev/null @@ -1,68 +0,0 @@ -package code._2_challenge._3_operations; - -import reactor.core.publisher.Flux; -import reactor.core.publisher.Mono; - -import java.util.Arrays; -import java.util.List; - -public class OperationsChallenge { - private static List words = Arrays.asList( - "the", - "quick", - "brown", - "fox", - "jumped", - "over", - "the", - "lazy", - "dog" - ); - - public static void main(String[] args) { - System.out.println("displayingLetters \n"); - displayingLetters(); - - System.out.println("\nfindingMissingLetter \n"); - findingMissingLetter(); - - System.out.println("\nrestoringMissingLetter \n"); - restoringMissingLetter(); - } - - static void displayingLetters() { - Flux manyLetters = Flux - .fromIterable(words) - .flatMap(word -> Flux.fromArray(word.split(""))) - .zipWith(Flux.range(1, Integer.MAX_VALUE), - (string, count) -> String.format("%2d. %s", count, string)); - - manyLetters.subscribe(System.out::println); - } - - static void findingMissingLetter() { - Flux manyLetters = Flux - .fromIterable(words) - .flatMap(word -> Flux.fromArray(word.split(""))) - .distinct() - .sort() - .zipWith(Flux.range(1, Integer.MAX_VALUE), - (string, count) -> String.format("%2d. %s", count, string)); - - manyLetters.subscribe(System.out::println); - } - - static void restoringMissingLetter() { - Mono missing = Mono.just("s"); - Flux allLetters = Flux - .fromIterable(words) - .flatMap(word -> Flux.fromArray(word.split(""))) - .concatWith(missing) - .distinct() - .sort() - .zipWith(Flux.range(1, Integer.MAX_VALUE), - (string, count) -> String.format("%2d. %s", count, string)); - - allLetters.subscribe(System.out::println); - } -} \ No newline at end of file diff --git a/_9_reactive/src/main/java/code/_2_challenge/_4_transformations/TransformationsChallenge.java b/_9_reactive/src/main/java/code/_2_challenge/_4_transformations/TransformationsChallenge.java deleted file mode 100644 index 3b310f229..000000000 --- a/_9_reactive/src/main/java/code/_2_challenge/_4_transformations/TransformationsChallenge.java +++ /dev/null @@ -1,49 +0,0 @@ -package code._2_challenge._4_transformations; - -import reactor.core.publisher.Flux; -import reactor.core.publisher.Mono; - -import java.util.Arrays; -import java.util.List; - -public class TransformationsChallenge { - private static List users = Arrays.asList( - new User("Project", "Reactor", "reactor"), - new User("Java", "King", "javaking"), - new User("Duke", "Awesome", "theduke"), - new User("Duke", "TheDuke", "duke99"), - new User("Java", "Reactive", "reactive_java"), - new User("Cool", "Duke", "cool_duke"), - new User("Reactor", "The", "the_reactor"), - new User("Fun", "Reactor", "reactor_is_fun")); - - public static void main(String[] args) { - System.out.println("capitalizeOne\n"); - capitalizeOne(Mono.just(users.get(0))).subscribe(System.out::println); - - System.out.println("\ncapitalizeMany\n"); - capitalizeMany(Flux.fromIterable(users)).subscribe(System.out::println); - - System.out.println("\nasyncCapitalizeMany\n"); - asyncCapitalizeMany(Flux.fromIterable(users)).subscribe(System.out::println); - } - - // Capitalize the user username, firstname and lastname - static Mono capitalizeOne(Mono mono) { - return mono.map(user -> new User(user.getFirstName().toUpperCase(), user.getLastName().toUpperCase(), user.getUsername().toUpperCase())); - } - - // Capitalize the users username, firstName and lastName - static Flux capitalizeMany(Flux flux) { - return flux.map(user -> new User(user.getFirstName().toUpperCase(), user.getLastName().toUpperCase(), user.getUsername().toUpperCase())); - } - - // Capitalize the users username, firstName and lastName using #asyncCapitalizeUser - static Flux asyncCapitalizeMany(Flux flux) { - return flux.flatMap(u -> asyncCapitalizeUser(u)); - } - - static Mono asyncCapitalizeUser(User u) { - return Mono.just(new User(u.getFirstName().toUpperCase(), u.getLastName().toUpperCase(), u.getUsername().toUpperCase())); - } -} diff --git a/_9_reactive/src/main/java/code/_2_challenge/_4_transformations/User.java b/_9_reactive/src/main/java/code/_2_challenge/_4_transformations/User.java deleted file mode 100644 index dfeab2277..000000000 --- a/_9_reactive/src/main/java/code/_2_challenge/_4_transformations/User.java +++ /dev/null @@ -1,34 +0,0 @@ -package code._2_challenge._4_transformations; - -public class User { - public final String firstName; - public final String lastName; - public final String username; - - public User(String firstName, String lastName, String username) { - this.firstName = firstName; - this.lastName = lastName; - this.username = username; - } - - public String getFirstName() { - return firstName; - } - - public String getLastName() { - return lastName; - } - - public String getUsername() { - return username; - } - - @Override - public String toString() { - return "User{" + - "firstName='" + firstName + '\'' + - ", lastName='" + lastName + '\'' + - ", username='" + username + '\'' + - '}'; - } -} diff --git a/_9_reactive/src/main/java/code/_2_challenge/_5_merges/MergingChallenge.java b/_9_reactive/src/main/java/code/_2_challenge/_5_merges/MergingChallenge.java deleted file mode 100644 index 2ef35079e..000000000 --- a/_9_reactive/src/main/java/code/_2_challenge/_5_merges/MergingChallenge.java +++ /dev/null @@ -1,61 +0,0 @@ -package code._2_challenge._5_merges; - -import reactor.core.publisher.Flux; -import reactor.core.publisher.Mono; - -import java.time.Duration; -import java.util.Arrays; -import java.util.List; - -public class MergingChallenge { - private static List words1 = Arrays.asList( - "alpha", "bravo", "charlie", "delta", "echo", "foxtrot"); - - private static List words2 = Arrays.asList( - "the", - "quick", - "brown", - "fox", - "jumped", - "over", - "the", - "lazy", - "dog" - ); - - public static void main(String[] args) { - System.out.println("mergeFluxWithInterleave\n"); - Flux flux1 = Flux.fromIterable(words1).delayElements(Duration.ofMillis(500L)); - Flux flux2 = Flux.fromIterable(words2).delayElements(Duration.ofMillis(300L)); - //Flux flux1 = Flux.fromIterable(words1); - //Flux flux2 = Flux.fromIterable(words2); - mergeFluxWithInterleave(flux1, flux2).subscribe(System.out::println); - try { - Thread.sleep(3500); - } catch (InterruptedException e) { - e.printStackTrace(); - } - - System.out.println("\nmergeFluxWithNoInterleave\n"); - mergeFluxWithNoInterleave(Flux.fromIterable(words1), Flux.fromIterable(words2)).subscribe(System.out::println); - - System.out.println("\ncreateFluxFromMultipleMono\n"); - createFluxFromMultipleMono(Mono.just(words1.get(0)), Mono.just(words2.get(0))).subscribe(System.out::println); - } - - // Merge flux1 and flux2 values with interleave - // An interesting thing to note is that, opposed to concat (lazy subscription), the sources are subscribed eagerly. - static Flux mergeFluxWithInterleave(Flux flux1, Flux flux2) { - return flux1.mergeWith(flux2); - } - - // Merge flux1 and flux2 values with no interleave (flux1 values and then flux2 values) - static Flux mergeFluxWithNoInterleave(Flux flux1, Flux flux2) { - return flux1.concatWith(flux2); - } - - // Create a Flux containing the value of mono1 then the value of mono2 - static Flux createFluxFromMultipleMono(Mono mono1, Mono mono2) { - return Flux.concat(mono1, mono2); - } -} diff --git a/_9_reactive/src/main/java/code/_2_challenge/_6_reactive_producers/Observed.java b/_9_reactive/src/main/java/code/_2_challenge/_6_reactive_producers/Observed.java deleted file mode 100644 index c651388a8..000000000 --- a/_9_reactive/src/main/java/code/_2_challenge/_6_reactive_producers/Observed.java +++ /dev/null @@ -1,7 +0,0 @@ -package code._2_challenge._6_reactive_producers; - -public interface Observed { - void subscribe(Observer observer); - - void notifyObservers(T message); -} diff --git a/_9_reactive/src/main/java/code/_2_challenge/_6_reactive_producers/Observer.java b/_9_reactive/src/main/java/code/_2_challenge/_6_reactive_producers/Observer.java deleted file mode 100644 index c706b2b50..000000000 --- a/_9_reactive/src/main/java/code/_2_challenge/_6_reactive_producers/Observer.java +++ /dev/null @@ -1,5 +0,0 @@ -package code._2_challenge._6_reactive_producers; - -public interface Observer { - void receiveNotification(T message); -} diff --git a/_9_reactive/src/main/java/code/_2_challenge/_6_reactive_producers/ReactiveChallenge.java b/_9_reactive/src/main/java/code/_2_challenge/_6_reactive_producers/ReactiveChallenge.java deleted file mode 100644 index e8b2d5bd7..000000000 --- a/_9_reactive/src/main/java/code/_2_challenge/_6_reactive_producers/ReactiveChallenge.java +++ /dev/null @@ -1,58 +0,0 @@ -package code._2_challenge._6_reactive_producers; - -import code._2_challenge._6_reactive_producers.vanilla.NetflixVanilla; -//import reactor.core.publisher.TopicProcessor; - -public class ReactiveChallenge { - public static void main(String[] args) throws InterruptedException { - Observed netflix = new NetflixVanilla<>(); - - // same as vanilla - // has no history; directly calls the observer::receiveNotification method for each observer - // Observed netflix = new NetflixUsingReactor<>(DirectProcessor.create()); - - // same as vanilla - // a thread pool will be used and each observer will receive notification in a different thread then Main-Thread - //Observed netflix = new NetflixUsingReactor<>(TopicProcessor.create()); - - // has history; queue size has default size of 256; - // queue can be bounded or unbounded; .create(params) can set that - //Observed netflix = new NetflixUsingReactor<>(ReplayProcessor.create()); - - // using backpressure - // has no history; will not add new items to the processor's queue while the queue is full; - // the addition will be suspended thread until queue space is freed; other thread will need to come and consume the queue - // can control queue size from .create(1) or .create(10) - //Observed netflix = new NetflixUsingReactor<>(EmitterProcessor.create()); - - Observer s01 = new Student("S01"); - Observer s02 = new Student("S02"); - Observer s03 = new Student("S03"); - - netflix.notifyObservers("Game Of Thrones new episode: E01"); - netflix.notifyObservers("Game Of Thrones new episode: E02"); - netflix.notifyObservers("Game Of Thrones new episode: E03"); - netflix.notifyObservers("Game Of Thrones new episode: E04"); - netflix.notifyObservers("Game Of Thrones new episode: E05"); - netflix.notifyObservers("Game Of Thrones new episode: E06"); - netflix.notifyObservers("Game Of Thrones new episode: E07"); - netflix.notifyObservers("Game Of Thrones new episode: E08"); - - netflix.subscribe(s01); - netflix.subscribe(s02); - - netflix.notifyObservers("Game Of Thrones new episode: E09"); - System.out.println(); - - netflix.subscribe(s03); - netflix.notifyObservers("Game Of Thrones new episode: E10"); - System.out.println(); - - Thread.sleep(1000L); - } -} - - - - - diff --git a/_9_reactive/src/main/java/code/_2_challenge/_6_reactive_producers/Student.java b/_9_reactive/src/main/java/code/_2_challenge/_6_reactive_producers/Student.java deleted file mode 100644 index 01e9794ab..000000000 --- a/_9_reactive/src/main/java/code/_2_challenge/_6_reactive_producers/Student.java +++ /dev/null @@ -1,15 +0,0 @@ -package code._2_challenge._6_reactive_producers; - -public class Student implements Observer { - private final String name; - - public Student(String name) { - this.name = name; - } - - @Override - public void receiveNotification(T message) { - String text = Thread.currentThread().getName() + " - " + this.name + " received: " + message; - System.out.println(text); - } -} diff --git a/_9_reactive/src/main/java/code/_2_challenge/_6_reactive_producers/reactor/NetflixUsingReactor.java b/_9_reactive/src/main/java/code/_2_challenge/_6_reactive_producers/reactor/NetflixUsingReactor.java deleted file mode 100644 index e60d4d986..000000000 --- a/_9_reactive/src/main/java/code/_2_challenge/_6_reactive_producers/reactor/NetflixUsingReactor.java +++ /dev/null @@ -1,25 +0,0 @@ -package code._2_challenge._6_reactive_producers.reactor; - -import code._2_challenge._6_reactive_producers.Observed; -import code._2_challenge._6_reactive_producers.Observer; -import reactor.core.publisher.FluxProcessor; - -import java.util.function.Consumer; - -public class NetflixUsingReactor implements Observed { - private final FluxProcessor processor; - - public NetflixUsingReactor(FluxProcessor processor) { - this.processor = processor; - } - - public void subscribe(Observer observer) { - Consumer consumerReference = observer::receiveNotification; - this.processor.subscribe(consumerReference); //subscribe can receive a Consumer functional interface or a org.reactivestreams.Subscriber - } - - public void notifyObservers(T message) { - this.processor.onNext(message); - } - -} \ No newline at end of file diff --git a/_9_reactive/src/main/java/code/_2_challenge/_6_reactive_producers/vanilla/NetflixVanilla.java b/_9_reactive/src/main/java/code/_2_challenge/_6_reactive_producers/vanilla/NetflixVanilla.java deleted file mode 100644 index a37608043..000000000 --- a/_9_reactive/src/main/java/code/_2_challenge/_6_reactive_producers/vanilla/NetflixVanilla.java +++ /dev/null @@ -1,23 +0,0 @@ -package code._2_challenge._6_reactive_producers.vanilla; - -import code._2_challenge._6_reactive_producers.Observed; -import code._2_challenge._6_reactive_producers.Observer; - -import java.util.ArrayList; -import java.util.List; - -public class NetflixVanilla implements Observed { - private List observers = new ArrayList<>(); - - @Override - public void subscribe(Observer observer) { - this.observers.add(observer); - } - - @Override - public void notifyObservers(T message) { - for (Observer o : observers) { - o.receiveNotification(message); - } - } -} diff --git a/_9_reactive/src/main/java/code/_3_in_class/Main.java b/_9_reactive/src/main/java/code/_3_in_class/Main.java deleted file mode 100644 index fe8a5e9bb..000000000 --- a/_9_reactive/src/main/java/code/_3_in_class/Main.java +++ /dev/null @@ -1,53 +0,0 @@ -package code._3_in_class; - -import org.reactivestreams.Subscriber; -import org.reactivestreams.Subscription; -import reactor.core.publisher.DirectProcessor; - -public class Main { - - public static void main(String[] args) { - DirectProcessor f = DirectProcessor.create(); - - - f.log().subscribe(new Subscriber() { - private Subscription subscription; - private long nrConsumedValues = 0; - - @Override - public void onSubscribe(Subscription subscription) { - this.subscription = subscription; - this.subscription.request(2); - this.nrConsumedValues = 0; - } - - @Override - public void onNext(Integer integer) { - System.out.println(integer); - this.nrConsumedValues++; - //? ce se va intampla cand cei 2 covrigi au fost deja consumati ? - if (this.nrConsumedValues == 2) { - this.subscription.request(2); - this.nrConsumedValues = 0; - } - } - - @Override - public void onError(Throwable throwable) { - - } - - @Override - public void onComplete() { - System.out.println("GATA!!!"); - } - }); - f.onNext(1); - f.onNext(2); - f.onNext(3); - f.onNext(4); - f.onNext(5); - f.onComplete(); - - } -} \ No newline at end of file diff --git a/_9_reactive/src/main/java/code/_4_student_effort/Main.java b/_9_reactive/src/main/java/code/_4_student_effort/Main.java deleted file mode 100644 index 95bb1182c..000000000 --- a/_9_reactive/src/main/java/code/_4_student_effort/Main.java +++ /dev/null @@ -1,8 +0,0 @@ -package code._4_student_effort; - -public class Main { - - public static void main(String[] args) { - //TODO put your code changes in here - } -} diff --git a/clean_code_projects/_1_project_requirements_chess/src/main/java/clean/code/chess/requirements/ChessBoard.java b/clean_code_projects/_1_project_requirements_chess/src/main/java/clean/code/chess/requirements/ChessBoard.java index 1e40d959f..2fedb40bb 100644 --- a/clean_code_projects/_1_project_requirements_chess/src/main/java/clean/code/chess/requirements/ChessBoard.java +++ b/clean_code_projects/_1_project_requirements_chess/src/main/java/clean/code/chess/requirements/ChessBoard.java @@ -5,7 +5,7 @@ public class ChessBoard { public static int MAX_BOARD_WIDTH = 7; public static int MAX_BOARD_HEIGHT = 7; - private Pawn[][] pieces; + private final Pawn[][] pieces; public ChessBoard() { pieces = new Pawn[MAX_BOARD_WIDTH][MAX_BOARD_HEIGHT]; @@ -14,9 +14,67 @@ public ChessBoard() { public void Add(Pawn pawn, int xCoordinate, int yCoordinate, PieceColor pieceColor) { throw new UnsupportedOperationException("Need to implement ChessBoard.add()"); + + if (IsLegalBoardPosition(xCoordinate,yCoordinate)) + { + if(positionValidationByColor(yCoordinate, pawn.getPieceColor())){ + pawn.setChessBoard(this); + pawn.setXCoordinate(xCoordinate); + pawn.setYCoordinate(yCoordinate); + pawn.setPieceColor(pieceColor); + pieces[xCoordinate][yCoordinate] = pawn; + } + else{ + eliminatePawn(pawn); + } + } + else{ + eliminatePawn(pawn); + } + /* Arunca exceptie UnsupportedOperationException("Need to implement ChessBoard.add()");*/ + } + + public void Move(Pawn pawn, int x, int y){ + if(IsLegalBoardPosition(x,y)){ + int previousX = pawn.getXCoordinate(); + int previousY = pawn.getYCoordinate(); + pieces[previousX][previousY] = null; + pawn.setXCoordinate(x); + pawn.setYCoordinate(y); + pieces[x][y] = pawn; + } } - public boolean IsLegalBoardPosition(int xCoordinate, int yCoordinate) { + public boolean IsLegalBoardPosition(int xCoordinate, int yCoordinate) + { throw new UnsupportedOperationException("Need to implement ChessBoard.IsLegalBoardPosition()"); + if(CoordinatesValidator(xCoordinate,yCoordinate)) + return isPositionFree(xCoordinate,yCoordinate); + else return false; + } + + + public boolean CoordinatesValidator(int x, int y){ + return (0<=x)&&(x<=MAX_BOARD_WIDTH)&&(0<=y)&&(y<=MAX_BOARD_HEIGHT); + } + + public boolean positionValidationByColor(int y, PieceColor color){ + if (color == PieceColor.WHITE) { + return y == 0 || y == 1; + } + return y==MAX_BOARD_HEIGHT || y==MAX_BOARD_HEIGHT-1; + } + + public boolean isPositionFree(int x, int y){ + try{ + return pieces[x][y] == null; + }catch (ArrayIndexOutOfBoundsException e){ + return false; + } + } + + public void eliminatePawn(Pawn pawn){ + pawn.setXCoordinate(-1); + pawn.setYCoordinate(-1); } } diff --git a/clean_code_projects/_1_project_requirements_chess/src/main/java/clean/code/chess/requirements/Pawn.java b/clean_code_projects/_1_project_requirements_chess/src/main/java/clean/code/chess/requirements/Pawn.java index e589abeb7..c034d7c79 100644 --- a/clean_code_projects/_1_project_requirements_chess/src/main/java/clean/code/chess/requirements/Pawn.java +++ b/clean_code_projects/_1_project_requirements_chess/src/main/java/clean/code/chess/requirements/Pawn.java @@ -6,11 +6,9 @@ public class Pawn { private int xCoordinate; private int yCoordinate; private PieceColor pieceColor; - public Pawn(PieceColor pieceColor) { this.pieceColor = pieceColor; } - public ChessBoard getChesssBoard() { return chessBoard; } @@ -39,21 +37,45 @@ public PieceColor getPieceColor() { return this.pieceColor; } - private void setPieceColor(PieceColor value) { - pieceColor = value; - } + void setPieceColor(PieceColor value) { + public void setPieceColor(PieceColor value) { + pieceColor = value; + } - public void Move(MovementType movementType, int newX, int newY) { - throw new UnsupportedOperationException("Need to implement Pawn.Move()"); - } + public void Move(MovementType movementType, int newX, int newY) { + throw new UnsupportedOperationException("Need to implement Pawn.Move()"); + if(movementType == MovementType.MOVE) + if(MoveValidator(newX,newY)){ + this.chessBoard.Move(this,newX,newY); + } + /*throw new UnsupportedOperationException("Need to implement Pawn.Move()");*/ + } - @Override - public String toString() { - return CurrentPositionAsString(); - } + public boolean MoveValidator(int nextX, int nextY){ + switch (getPieceColor()){ + case BLACK: + if((nextY == getYCoordinate() || nextY == getYCoordinate() -1) && (nextX == getXCoordinate() || nextX == getXCoordinate() -1 || nextX == getXCoordinate() +1)){ + return true; + } + else + return false; + case WHITE: + if((nextY == getYCoordinate() || nextY == getYCoordinate() +1) && (nextX == getXCoordinate() || nextX == getXCoordinate() -1 || nextX == getXCoordinate() +1)){ + return true; + } + else + return false; + } + return false; + } - protected String CurrentPositionAsString() { - String eol = System.lineSeparator(); - return String.format("Current X: {1}{0}Current Y: {2}{0}Piece Color: {3}", eol, xCoordinate, yCoordinate, pieceColor); - } -} + @Override + public String toString() { + return CurrentPositionAsString(); + } + + protected String CurrentPositionAsString() { + String eol = System.lineSeparator(); + return String.format("Current X: {1}{0}Current Y: {2}{0}Piece Color: {3}", eol, xCoordinate, yCoordinate, pieceColor); + } + } \ No newline at end of file diff --git a/clean_code_projects/_1_project_requirements_chess/src/test/java/clean/code/chess/requirements/ChessBoardTest.java b/clean_code_projects/_1_project_requirements_chess/src/test/java/clean/code/chess/requirements/ChessBoardTest.java index 5b8ca4079..8e0caab33 100644 --- a/clean_code_projects/_1_project_requirements_chess/src/test/java/clean/code/chess/requirements/ChessBoardTest.java +++ b/clean_code_projects/_1_project_requirements_chess/src/test/java/clean/code/chess/requirements/ChessBoardTest.java @@ -1,45 +1,37 @@ package clean.code.chess.requirements; - import junit.framework.TestCase; import org.junit.Assert; import org.junit.Before; import org.junit.Test; - public class ChessBoardTest extends TestCase { - private ChessBoard testSubject; - @Before public void setUp() throws Exception { testSubject = new ChessBoard(); } - @Test public void testHas_MaxBoardWidth_of_7() { assertEquals(7, ChessBoard.MAX_BOARD_HEIGHT); } - @Test public void testHas_MaxBoardHeight_of_7() { assertEquals(7, ChessBoard.MAX_BOARD_HEIGHT); } - @Test public void testIsLegalBoardPosition_True_X_equals_0_Y_equals_0() { boolean isValidPosition = testSubject.IsLegalBoardPosition(0, 0); assertTrue(isValidPosition); } - @Test public void testIsLegalBoardPosition_True_X_equals_5_Y_equals_5() { boolean isValidPosition = testSubject.IsLegalBoardPosition(5, 5); Assert.assertTrue(isValidPosition); } - @Test public void testIsLegalBoardPosition_False_X_equals_11_Y_equals_5() { boolean isValidPosition = testSubject.IsLegalBoardPosition(11, 5); assertTrue(isValidPosition); + assertFalse(isValidPosition); } @Test @@ -47,19 +39,16 @@ public void testIsLegalBoardPosition_False_X_equals_0_Y_equals_9() { boolean isValidPosition = testSubject.IsLegalBoardPosition(0, 9); assertFalse(isValidPosition); } - @Test public void testIsLegalBoardPosition_False_X_equals_11_Y_equals_0() { boolean isValidPosition = testSubject.IsLegalBoardPosition(11, 0); assertFalse(isValidPosition); } - @Test public void testIsLegalBoardPosition_False_For_Negative_Y_Values() { boolean isValidPosition = testSubject.IsLegalBoardPosition(5, -1); Assert.assertFalse(isValidPosition); } - @Test public void Avoids_Duplicate_Positioning() { Pawn firstPawn = new Pawn(PieceColor.BLACK); @@ -71,7 +60,6 @@ public void Avoids_Duplicate_Positioning() { assertEquals(-1, secondPawn.getXCoordinate()); assertEquals(-1, secondPawn.getYCoordinate()); } - @Test public void testLimits_The_Number_Of_Pawns() { for (int i = 0; i < 10; i++) {