From 11d589a1aae01b0aaaaa199c6186b8bb1637fcd9 Mon Sep 17 00:00:00 2001 From: yelyzavetalubenets Date: Mon, 22 Jan 2024 12:25:17 +0100 Subject: [PATCH] YL-1 Add: Implementation to crazy generics class --- .../com/bobocode/basics/CrazyGenerics.java | 121 +++++++++++++----- 1 file changed, 87 insertions(+), 34 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 8aeb3997b..491447a86 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 java.io.Serializable; -import java.util.Collection; -import java.util.Comparator; -import java.util.List; -import java.util.Objects; +import java.time.LocalDateTime; +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,11 @@ public static class Limited { * @param – source object type * @param - converted result type */ - public interface Converter { // todo: introduce type parameters + public interface Converter { // todo: introduce type parameters // todo: add convert method + private R convert(T parameter) { + return (R) parameter; + } } /** @@ -70,10 +71,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 +83,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) { + this.max = val; + } } - public Object getMax() { + public T getMax() { return max; } } @@ -97,8 +100,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 & Serializable> { // todo: make it generic + void process(T obj); } /** @@ -108,10 +111,10 @@ 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); + interface CollectionRepository> { // todo: update interface according to the javadoc + void save(T entity); - Collection getEntityCollection(); + C getEntityCollection(); } /** @@ -120,7 +123,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,7 +136,12 @@ 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()); + } } /** @@ -147,7 +155,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 +168,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(entity -> entity.getUuid() == null);// todo: refactor parameter and implement method } /** @@ -173,8 +182,8 @@ 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: add method parameters and implement the logic } /** @@ -187,8 +196,12 @@ 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) { + List filteredEntities = entities.stream() + .filter(entity -> entity.getUuid().equals(targetEntity.getUuid())) + .toList(); + + return filteredEntities.size() > 1;// todo: update method signature and implement it } /** @@ -198,9 +211,20 @@ public static boolean hasDuplicates() { * @param elements provided iterable of elements * @param comparator an object that will be used to compare elements * @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) { + T maxEl = elements.iterator().next(); + for (T element : elements) { + int result = comparator.compare(maxEl, element); + + if (result <= -1) { + maxEl = element; + } + } + + return Optional.of(maxEl); + } /** * findMostRecentlyCreatedEntity is a generic util method that accepts a collection of entities and returns the @@ -215,6 +239,26 @@ public static boolean hasDuplicates() { * @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) { + final Comparator CREATED_ON_COMPARATOR = new Comparator() { + @Override + public int compare(T o1, T o2) { + int comparator = 0; + final LocalDateTime createdOnDate1stObj = o1.getCreatedOn(); + final LocalDateTime createdOnDate2ndObj = o2.getCreatedOn(); + + if (createdOnDate1stObj == createdOnDate2ndObj) { + return comparator; + } else if (createdOnDate1stObj.isBefore(createdOnDate2ndObj)) { + comparator = -1; + } else comparator = 1; + + return comparator; + } + }; + + return findMax(entities, CREATED_ON_COMPARATOR).orElseThrow(() -> new NoSuchElementException("The element was not found!")); + } /** * An util method that allows to swap two elements of any list. It changes the list so the element with the index @@ -228,7 +272,16 @@ public static boolean hasDuplicates() { 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 + + // todo: complete method implementation + swapElements(elements, i, j); + } + + private static void swapElements(List elements, int i, int j) { + T elementI = elements.get(i); + T elementJ = elements.get(j); + elements.set(i, elementJ); + elements.set(j, elementI); } }