From c2101a6682cddb47c1834d228caecde59e8d3d7f Mon Sep 17 00:00:00 2001 From: anastasiiakostenko14 Date: Mon, 9 Sep 2024 21:42:48 +0300 Subject: [PATCH 1/6] completed 0-0 task --- .../java/com/bobocode/intro/ExerciseIntroduction.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/0-0-intro/src/main/java/com/bobocode/intro/ExerciseIntroduction.java b/0-0-intro/src/main/java/com/bobocode/intro/ExerciseIntroduction.java index 35d92563..d41a228a 100644 --- a/0-0-intro/src/main/java/com/bobocode/intro/ExerciseIntroduction.java +++ b/0-0-intro/src/main/java/com/bobocode/intro/ExerciseIntroduction.java @@ -1,6 +1,6 @@ package com.bobocode.intro; -import com.bobocode.util.ExerciseNotCompletedException; +import java.util.Base64; /** * Welcome! This is an introduction exercise that will show you a simple example of Bobocode exercises. @@ -23,8 +23,7 @@ public class ExerciseIntroduction { * @return "The key to efficient learning is practice!" */ public String getWelcomeMessage() { - // todo: implement a method and return a message according to javadoc - throw new ExerciseNotCompletedException(); + return "The key to efficient learning is practice!"; } /** @@ -39,7 +38,6 @@ public String getWelcomeMessage() { * @return encoded message */ public String encodeMessage(String message) { - // todo: switch to branch "completed" in order to see how it should be implemented - throw new ExerciseNotCompletedException(); + return Base64.getEncoder().encodeToString(message.getBytes()); } } From 9c1db91c4c9bb790b6a4dac0e6ba41d43e7ca5ba Mon Sep 17 00:00:00 2001 From: anastasiiakostenko14 Date: Mon, 9 Sep 2024 21:50:00 +0300 Subject: [PATCH 2/6] completed 0-0 task --- .../src/main/java/com/bobocode/intro/ExerciseIntroduction.java | 1 + 1 file changed, 1 insertion(+) diff --git a/0-0-intro/src/main/java/com/bobocode/intro/ExerciseIntroduction.java b/0-0-intro/src/main/java/com/bobocode/intro/ExerciseIntroduction.java index d41a228a..2351b25d 100644 --- a/0-0-intro/src/main/java/com/bobocode/intro/ExerciseIntroduction.java +++ b/0-0-intro/src/main/java/com/bobocode/intro/ExerciseIntroduction.java @@ -41,3 +41,4 @@ public String encodeMessage(String message) { return Base64.getEncoder().encodeToString(message.getBytes()); } } +/*comment*/ From 5e2395d08a68936f24956ecc7beba99051803ad6 Mon Sep 17 00:00:00 2001 From: anastasiiakostenko14 Date: Mon, 16 Sep 2024 18:10:33 +0300 Subject: [PATCH 3/6] COMPLETED TASK WITH 7 TESTS PASSED --- .../src/main/java/com/bobocode/basics/Box.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/1-0-java-basics/1-3-0-hello-generics/src/main/java/com/bobocode/basics/Box.java b/1-0-java-basics/1-3-0-hello-generics/src/main/java/com/bobocode/basics/Box.java index 5a2d860e..1613fcfc 100644 --- a/1-0-java-basics/1-3-0-hello-generics/src/main/java/com/bobocode/basics/Box.java +++ b/1-0-java-basics/1-3-0-hello-generics/src/main/java/com/bobocode/basics/Box.java @@ -7,18 +7,18 @@ *

* todo: refactor this class so it uses generic type "T" and run {@link com.bobocode.basics.BoxTest} to verify it */ -public class Box { - private Object value; +public class Box { + private T value; - public Box(Object value) { + public Box(T value) { this.value = value; } - public Object getValue() { + public T getValue() { return value; } - public void setValue(Object value) { + public void setValue(T value) { this.value = value; } -} +} \ No newline at end of file From 55954be56bb8aa4f5fe559a7b398edb483c48c87 Mon Sep 17 00:00:00 2001 From: anastasiiakostenko14 Date: Mon, 16 Sep 2024 20:56:52 +0300 Subject: [PATCH 4/6] ALL TESTS WERE PASSED. 0 FAILURE --- .../com/bobocode/basics/CrazyGenerics.java | 110 +++++++++++------- 1 file changed, 69 insertions(+), 41 deletions(-) diff --git a/1-0-java-basics/1-3-1-crazy-generics/src/main/java/com/bobocode/basics/CrazyGenerics.java b/1-0-java-basics/1-3-1-crazy-generics/src/main/java/com/bobocode/basics/CrazyGenerics.java index 751d5899..12da8786 100644 --- a/1-0-java-basics/1-3-1-crazy-generics/src/main/java/com/bobocode/basics/CrazyGenerics.java +++ b/1-0-java-basics/1-3-1-crazy-generics/src/main/java/com/bobocode/basics/CrazyGenerics.java @@ -1,14 +1,12 @@ package com.bobocode.basics; import com.bobocode.basics.util.BaseEntity; -import com.bobocode.util.ExerciseNotCompletedException; import lombok.Data; +import lombok.val; import java.io.Serializable; -import java.util.Collection; -import java.util.Comparator; -import java.util.List; -import java.util.Objects; +import java.util.*; +import java.util.function.Predicate; /** * {@link CrazyGenerics} is an exercise class. It consists of classes, interfaces and methods that should be updated @@ -33,8 +31,8 @@ public class CrazyGenerics { * @param – value type */ @Data - public static class Sourced { // todo: refactor class to introduce type parameter and make value generic - private Object value; + public static class Sourced { // todo: refactor class to introduce type parameter and make value generic + private T value; private String source; } @@ -45,11 +43,11 @@ public static class Sourced { // todo: refactor class to introduce type paramete * @param – actual, min and max type */ @Data - public static class Limited { + public static class Limited { // todo: refactor class to introduce type param bounded by number and make fields generic numbers - private final Object actual; - private final Object min; - private final Object max; + private final T actual; + private final T min; + private final T max; } /** @@ -59,8 +57,9 @@ public static class Limited { * @param – source object type * @param - converted result type */ - public interface Converter { // todo: introduce type parameters + public interface Converter { // todo: add convert method + R convert(T obj); } /** @@ -70,10 +69,10 @@ public interface Converter { // todo: introduce type parameters * * @param – value type */ - public static class MaxHolder { // todo: refactor class to make it generic - private Object max; + public static class MaxHolder> { // todo: refactor class to make it generic + private T max; - public MaxHolder(Object max) { + public MaxHolder(T max) { this.max = max; } @@ -82,11 +81,13 @@ public MaxHolder(Object max) { * * @param val a new value */ - public void put(Object val) { - throw new ExerciseNotCompletedException(); // todo: update parameter and implement the method + public void put(T val) { + if (val.compareTo(max) > 0) { + max = val; + } } - public Object getMax() { + public T getMax() { return max; } } @@ -97,8 +98,8 @@ public Object getMax() { * * @param – the type of objects that can be processed */ - interface StrictProcessor { // todo: make it generic - void process(Object obj); + interface StrictProcessor> { // todo: make it generic + void process(T obj); } /** @@ -108,10 +109,9 @@ interface StrictProcessor { // todo: make it generic * @param – a type of the entity that should be a subclass of {@link BaseEntity} * @param – a type of any collection */ - interface CollectionRepository { // todo: update interface according to the javadoc - void save(Object entity); - - Collection getEntityCollection(); + interface CollectionRepository> { // todo: update interface according to the javadoc + void save(T entity); + C getEntityCollection(); } /** @@ -120,7 +120,7 @@ interface CollectionRepository { // todo: update interface according to the java * * @param – a type of the entity that should be a subclass of {@link BaseEntity} */ - interface ListRepository { // todo: update interface according to the javadoc + interface ListRepository extends CollectionRepository> { // todo: update interface according to the javadoc } /** @@ -133,12 +133,14 @@ interface ListRepository { // todo: update interface according to the javadoc * * @param a type of collection elements */ - interface ComparableCollection { // todo: refactor it to make generic and provide a default impl of compareTo + interface ComparableCollection extends Collection, Comparable> { // todo: refactor it to make generic and provide a default impl of compareTo + + @Override + default int compareTo(Collection o) { + return Integer.compare(this.size(), o.size()); + } } - /** - * {@link CollectionUtil} is an util class that provides various generic helper methods. - */ static class CollectionUtil { static final Comparator CREATED_ON_COMPARATOR = Comparator.comparing(BaseEntity::getCreatedOn); @@ -147,7 +149,7 @@ static class CollectionUtil { * * @param list */ - public static void print(List list) { + public static void print(List list) { // todo: refactor it so the list of any type can be printed, not only integers list.forEach(element -> System.out.println(" – " + element)); } @@ -160,8 +162,9 @@ public static void print(List list) { * @param entities provided collection of entities * @return true if at least one of the elements has null id */ - public static boolean hasNewEntities(Collection entities) { - throw new ExerciseNotCompletedException(); // todo: refactor parameter and implement method + public static boolean hasNewEntities(Collection entities) { + return entities.stream() + .anyMatch(e -> e.getUuid() == null); } /** @@ -173,8 +176,10 @@ public static boolean hasNewEntities(Collection entities) { * @param validationPredicate criteria for validation * @return true if all entities fit validation criteria */ - public static boolean isValidCollection() { - throw new ExerciseNotCompletedException(); // todo: add method parameters and implement the logic + public static boolean isValidCollection(Collection entities, + Predicate validationPredicate) { + return entities.stream() + .allMatch(validationPredicate); // todo: refactor parameter and implement method } /** @@ -187,8 +192,10 @@ public static boolean isValidCollection() { * @param entity type * @return true if entities list contains target entity more than once */ - public static boolean hasDuplicates() { - throw new ExerciseNotCompletedException(); // todo: update method signature and implement it + public static boolean hasDuplicates(Collection entities,T targetEntity) { + return entities.stream() + .filter(e -> e.getUuid().equals(targetEntity.getUuid())) + .count() > 1; } /** @@ -200,7 +207,20 @@ public static boolean hasDuplicates() { * @param type of elements * @return optional max value */ - // todo: create a method and implement its logic manually without using util method from JDK + public static Optional findMax(Iterable elements,Comparator comparator) { + var iterator = elements.iterator(); + if (!iterator.hasNext()) { + return Optional.empty(); + } + var max = iterator.next(); + while (iterator.hasNext()) { + var element = iterator.next(); + if (comparator.compare(element, max) > 0) { + max = element; + } + } + return Optional.of(max); + } /** * findMostRecentlyCreatedEntity is a generic util method that accepts a collection of entities and returns the @@ -214,7 +234,10 @@ public static boolean hasDuplicates() { * @param entity type * @return an entity from the given collection that has the max createdOn value */ - // todo: create a method according to JavaDoc and implement it using previous method + public static T findMostRecentlyCreatedEntity(Collection entities) { + return findMax(entities, CREATED_ON_COMPARATOR) + .orElseThrow(); + } /** * An util method that allows to swap two elements of any list. It changes the list so the element with the index @@ -226,10 +249,15 @@ public static boolean hasDuplicates() { * @param j index of the other element to swap */ public static void swap(List elements, int i, int j) { - Objects.checkIndex(i, elements.size()); + Objects.checkIndex(i, elements.size()); // todo: complete method implementation Objects.checkIndex(j, elements.size()); - throw new ExerciseNotCompletedException(); // todo: complete method implementation + swapHelper(elements, i, j); } + private static void swapHelper(List elements, int i, int j) { + T temp = elements.get(i); + elements.set(i, elements.get(j)); + elements.set(j, temp); + } } -} +} \ No newline at end of file From 45b3377bc051822756e4ab17879b5c7c32298d2b Mon Sep 17 00:00:00 2001 From: anastasiiakostenko14 Date: Mon, 16 Sep 2024 21:00:02 +0300 Subject: [PATCH 5/6] 1-3-0, 1-3-1 COMPLETED --- .../main/java/com/bobocode/basics/Box.java | 3 +- .../com/bobocode/basics/CrazyGenerics.java | 28 ++++++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/1-0-java-basics/1-3-0-hello-generics/src/main/java/com/bobocode/basics/Box.java b/1-0-java-basics/1-3-0-hello-generics/src/main/java/com/bobocode/basics/Box.java index 1613fcfc..4ade3b78 100644 --- a/1-0-java-basics/1-3-0-hello-generics/src/main/java/com/bobocode/basics/Box.java +++ b/1-0-java-basics/1-3-0-hello-generics/src/main/java/com/bobocode/basics/Box.java @@ -21,4 +21,5 @@ public T getValue() { public void setValue(T value) { this.value = value; } -} \ No newline at end of file +} +// \ No newline at end of file diff --git a/1-0-java-basics/1-3-1-crazy-generics/src/main/java/com/bobocode/basics/CrazyGenerics.java b/1-0-java-basics/1-3-1-crazy-generics/src/main/java/com/bobocode/basics/CrazyGenerics.java index 12da8786..a55ef5a3 100644 --- a/1-0-java-basics/1-3-1-crazy-generics/src/main/java/com/bobocode/basics/CrazyGenerics.java +++ b/1-0-java-basics/1-3-1-crazy-generics/src/main/java/com/bobocode/basics/CrazyGenerics.java @@ -260,4 +260,30 @@ private static void swapHelper(List elements, int i, int j) { elements.set(j, temp); } } -} \ No newline at end of file +} + + + + + + + + + + + + + + + + + + + + + + + + + +// \ No newline at end of file From 169003f61013921d12af5371c135daee5a62df35 Mon Sep 17 00:00:00 2001 From: anastasiiakostenko14 Date: Mon, 16 Sep 2024 21:00:36 +0300 Subject: [PATCH 6/6] 1-3-0, 1-3-1 COMPLETED --- .../src/main/java/com/bobocode/basics/Box.java | 1 - .../src/main/java/com/bobocode/basics/CrazyGenerics.java | 1 - 2 files changed, 2 deletions(-) diff --git a/1-0-java-basics/1-3-0-hello-generics/src/main/java/com/bobocode/basics/Box.java b/1-0-java-basics/1-3-0-hello-generics/src/main/java/com/bobocode/basics/Box.java index 4ade3b78..180603a2 100644 --- a/1-0-java-basics/1-3-0-hello-generics/src/main/java/com/bobocode/basics/Box.java +++ b/1-0-java-basics/1-3-0-hello-generics/src/main/java/com/bobocode/basics/Box.java @@ -22,4 +22,3 @@ public void setValue(T value) { this.value = value; } } -// \ No newline at end of file diff --git a/1-0-java-basics/1-3-1-crazy-generics/src/main/java/com/bobocode/basics/CrazyGenerics.java b/1-0-java-basics/1-3-1-crazy-generics/src/main/java/com/bobocode/basics/CrazyGenerics.java index a55ef5a3..2530cee4 100644 --- a/1-0-java-basics/1-3-1-crazy-generics/src/main/java/com/bobocode/basics/CrazyGenerics.java +++ b/1-0-java-basics/1-3-1-crazy-generics/src/main/java/com/bobocode/basics/CrazyGenerics.java @@ -286,4 +286,3 @@ private static void swapHelper(List elements, int i, int j) { -// \ No newline at end of file