From be2a321118d39f4e0acb32e75ed08205a707fc09 Mon Sep 17 00:00:00 2001
From: Ivan Semkiv <ivansemkiv@MacBook-Air-Ivan.local>
Date: Thu, 12 Sep 2024 09:58:18 +0200
Subject: [PATCH 1/2] =?UTF-8?q?=D0=92=D0=B8=D0=BA=D0=BE=D0=BD=D0=B0=D0=BD?=
 =?UTF-8?q?=D1=96=20=D0=B7=D0=B0=D0=B2=D0=B4=D0=B0=D0=BD=D0=BD=D1=8F=20?=
 =?UTF-8?q?=D0=B7=20java-fundamentals-exercises?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../com/bobocode/intro/ExerciseIntroduction.java   | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 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 35d925636..0b758fe7d 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,5 +1,5 @@
 package com.bobocode.intro;
-
+import java.util.Base64;
 import com.bobocode.util.ExerciseNotCompletedException;
 
 /**
@@ -23,9 +23,9 @@ 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!";
     }
+    
 
     /**
      * Method encodeMessage accepts one {@link String} parameter and returns encoded {@link String}.
@@ -38,8 +38,8 @@ public String getWelcomeMessage() {
      * @param message input message
      * @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();
-    }
+
+public String encodeMessage(String message) {
+    return Base64.getEncoder().encodeToString(message.getBytes());  
 }
+}
\ No newline at end of file

From 8c0c8c7d9d272bcffd18458e773a200a2a589aa9 Mon Sep 17 00:00:00 2001
From: Ivan Semkiv <ivansemkiv@MacBook-Air-Ivan.local>
Date: Fri, 1 Nov 2024 03:33:54 +0100
Subject: [PATCH 2/2] =?UTF-8?q?=D0=92=D0=B8=D0=BA=D0=BE=D0=BD=D0=B0=D0=BD?=
 =?UTF-8?q?=D1=96=20=D0=B7=D0=B0=D0=B2=D0=B4=D0=B0=D0=BD=D0=BD=D1=8F=20?=
 =?UTF-8?q?=D0=B7=20java-fundamentals-exercises?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../main/java/com/bobocode/basics/Box.java    |  12 +-
 .../com/bobocode/basics/CrazyGenerics.java    | 110 ++++++++++++------
 2 files changed, 78 insertions(+), 44 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 5a2d860ee..1613fcfc3 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 @@
  * <p>
  * 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<T> {
+    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
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 751d5899f..ea0252325 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,11 @@
 package com.bobocode.basics;
 
 import com.bobocode.basics.util.BaseEntity;
-import com.bobocode.util.ExerciseNotCompletedException;
 import lombok.Data;
 
 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 +30,8 @@ public class CrazyGenerics {
      * @param <T> – 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<T> { // todo: refactor class to introduce type parameter and make value generic
+        private T value;
         private String source;
     }
 
@@ -45,11 +42,11 @@ public static class Sourced { // todo: refactor class to introduce type paramete
      * @param <T> – actual, min and max type
      */
     @Data
-    public static class Limited {
+    public static class Limited<T extends Number> {
         // 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 +56,9 @@ public static class Limited {
      * @param <T> – source object type
      * @param <R> - converted result type
      */
-    public interface Converter { // todo: introduce type parameters
+    public interface Converter<T, R> { // todo: introduce type parameters
         // todo: add convert method
+        R convert(T param);
     }
 
     /**
@@ -70,10 +68,10 @@ public interface Converter { // todo: introduce type parameters
      *
      * @param <T> – value type
      */
-    public static class MaxHolder { // todo: refactor class to make it generic
-        private Object max;
+    public static class MaxHolder<T extends Comparable<? super T>> { // todo: refactor class to make it generic
+        private T max;
 
-        public MaxHolder(Object max) {
+        public MaxHolder(T max) {
             this.max = max;
         }
 
@@ -82,11 +80,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;
+            } // todo: update parameter and implement the method
         }
 
-        public Object getMax() {
+        public T getMax() {
             return max;
         }
     }
@@ -97,8 +97,8 @@ public Object getMax() {
      *
      * @param <T> – the type of objects that can be processed
      */
-    interface StrictProcessor { // todo: make it generic
-        void process(Object obj);
+    interface StrictProcessor<T extends Serializable & Comparable<? super T>> { // todo: make it generic
+        void process(T obj);
     }
 
     /**
@@ -108,19 +108,19 @@ interface StrictProcessor { // todo: make it generic
      * @param <T> – a type of the entity that should be a subclass of {@link BaseEntity}
      * @param <C> – a type of any collection
      */
-    interface CollectionRepository { // todo: update interface according to the javadoc
-        void save(Object entity);
+    interface CollectionRepository<T extends BaseEntity, C extends Collection<T>> { // todo: update interface according to the javadoc
+        void save(T entity);
 
-        Collection<Object> getEntityCollection();
+        C getEntityCollection();
     }
 
     /**
      * {@link ListRepository} extends {@link CollectionRepository} but specifies the underlying collection as
-     * {@link java.util.List}.
+     * {@link List}.
      *
      * @param <T> – a type of the entity that should be a subclass of {@link BaseEntity}
      */
-    interface ListRepository { // todo: update interface according to the javadoc
+    interface ListRepository<T extends BaseEntity> extends CollectionRepository<T, List<T>> { // todo: update interface according to the javadoc
     }
 
     /**
@@ -133,7 +133,10 @@ interface ListRepository { // todo: update interface according to the javadoc
      *
      * @param <E> a type of collection elements
      */
-    interface ComparableCollection { // todo: refactor it to make generic and provide a default impl of compareTo
+    interface ComparableCollection<E> extends Collection<E>, Comparable<Collection<?>> { // todo: refactor it to make generic and provide a default impl of compareTo
+        default int compareTo(Collection<?> other) {
+            return Integer.compare(this.size(), other.size());
+        }
     }
 
     /**
@@ -147,7 +150,7 @@ static class CollectionUtil {
          *
          * @param list
          */
-        public static void print(List<Integer> 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 +163,13 @@ public static void print(List<Integer> list) {
          * @param entities provided collection of entities
          * @return true if at least one of the elements has null id
          */
-        public static boolean hasNewEntities(Collection<BaseEntity> entities) {
-            throw new ExerciseNotCompletedException(); // todo: refactor parameter and implement method
+        public static boolean hasNewEntities(Collection<? extends BaseEntity> entities) {
+            for (BaseEntity e : entities) {
+                if (e.getUuid() == null) {
+                    return true; // todo: refactor parameter and implement method
+                }
+            }
+            return false; // todo: refactor parameter and implement method
         }
 
         /**
@@ -173,8 +181,10 @@ public static boolean hasNewEntities(Collection<BaseEntity> 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<? extends BaseEntity> entities, Predicate<? super BaseEntity> validationPredicate) {
+            return entities.stream()
+                    .allMatch(validationPredicate);
+
         }
 
         /**
@@ -187,8 +197,10 @@ public static boolean isValidCollection() {
          * @param <T>          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 <T extends BaseEntity> boolean hasDuplicates(List<T> entities, T targetEntity) {
+            return entities.stream()
+                    .filter(entity -> entity.getUuid().equals(targetEntity.getUuid()))
+                    .count() > 1;
         }
 
         /**
@@ -202,10 +214,23 @@ public static boolean hasDuplicates() {
          */
         // todo: create a method and implement its logic manually without using util method from JDK
 
+        public  static <T> Optional<T> findMax(Iterable<T> elements, Comparator<? super T> comparator) {
+            if (elements.iterator().hasNext()) {
+                T max = elements.iterator().next();
+                for (T e : elements) {
+                    if (comparator.compare(e, max) > 0) {
+                        max = e;
+                    }
+                }
+                return Optional.of(max);
+            }
+            return Optional.empty();
+        }
+
         /**
          * findMostRecentlyCreatedEntity is a generic util method that accepts a collection of entities and returns the
          * one that is the most recently created. If collection is empty,
-         * it throws {@link java.util.NoSuchElementException}.
+         * it throws {@link NoSuchElementException}.
          * <p>
          * This method reuses findMax method and passes entities along with prepare comparator instance,
          * that is stored as constant CREATED_ON_COMPARATOR.
@@ -216,6 +241,11 @@ public static boolean hasDuplicates() {
          */
         // todo: create a method according to JavaDoc and implement it using previous method
 
+        public static <T extends BaseEntity> T findMostRecentlyCreatedEntity(Collection<T> 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
          * i will be located on index j, and the element with index j, will be located on the index i.
@@ -226,10 +256,14 @@ 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(j, elements.size());
-            throw new ExerciseNotCompletedException(); // todo: complete method implementation 
+            swapHelper(elements, i, j);
+        }
+
+        private static <T> void swapHelper(List<T> 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