diff --git a/0-0-intro/pom.xml b/0-0-intro/pom.xml index b62fec52e..056e51706 100644 --- a/0-0-intro/pom.xml +++ b/0-0-intro/pom.xml @@ -17,6 +17,13 @@ java-fundamentals-util 1.0-SNAPSHOT + + + org.projectlombok + lombok + 1.18.30 + provided + \ No newline at end of file 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..4e6fa2a22 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,7 @@ 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 +24,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 +39,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()); } } 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 @@ *

* 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 diff --git a/1-0-java-basics/1-3-1-crazy-generics/pom.xml b/1-0-java-basics/1-3-1-crazy-generics/pom.xml index cab63d0ba..7ae86d528 100644 --- a/1-0-java-basics/1-3-1-crazy-generics/pom.xml +++ b/1-0-java-basics/1-3-1-crazy-generics/pom.xml @@ -10,5 +10,14 @@ 4.0.0 1-3-1-crazy-generics - + + + + org.projectlombok + lombok + 1.18.30 + provided + + + \ 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..36b0672e1 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 @@ -5,10 +5,8 @@ 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 +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 { + private T value; private String source; } @@ -45,11 +43,10 @@ public static class Sourced { // todo: refactor class to introduce type paramete * @param – actual, min and max type */ @Data - 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; + public static class Limited { + private final T actual; + private final T min; + private final T max; } /** @@ -59,8 +56,8 @@ public static class Limited { * @param – source object type * @param - converted result type */ - public interface Converter { // todo: introduce type parameters - // todo: add convert method + public interface Converter { + R convert(T obj); } /** @@ -70,10 +67,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> { + private T max; - public MaxHolder(Object max) { + public MaxHolder(T max) { this.max = max; } @@ -82,11 +79,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 (max == null || val.compareTo(max) > 0) { + max = val; + } } - public Object getMax() { + public T getMax() { return max; } } @@ -97,8 +96,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> { + void process(T obj); } /** @@ -108,10 +107,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> { + void save(T entity); - Collection getEntityCollection(); + C getEntityCollection(); } /** @@ -120,7 +119,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> { } /** @@ -133,7 +132,11 @@ 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> { + @Override + default int compareTo(Collection o) { + return Integer.compare(this.size(), o.size()); + } } /** @@ -147,8 +150,7 @@ static class CollectionUtil { * * @param list */ - public static void print(List list) { - // todo: refactor it so the list of any type can be printed, not only integers + public static void print(List list) { 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,9 @@ 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); } /** @@ -187,8 +191,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 -> Objects.equals(e.getUuid(), targetEntity.getUuid())) + .count() > 1; } /** @@ -200,7 +206,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) { + Iterator iterator = elements.iterator(); + if (!iterator.hasNext()) { + return Optional.empty(); + } + T max = iterator.next(); + while (iterator.hasNext()) { + T current = iterator.next(); + if (comparator.compare(current, max) > 0) { + max = current; + } + } + return Optional.of(max); + } /** * findMostRecentlyCreatedEntity is a generic util method that accepts a collection of entities and returns the @@ -214,7 +233,9 @@ 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 +247,13 @@ 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 void swapHelper(List elements, int i, int j) { + T temp = elements.get(i); + elements.set(i, elements.get(j)); + elements.set(j, temp); + } } } diff --git a/1-0-java-basics/1-3-1-crazy-generics/src/main/java/com/bobocode/basics/util/BaseEntity.java b/1-0-java-basics/1-3-1-crazy-generics/src/main/java/com/bobocode/basics/util/BaseEntity.java index f961ab7a5..88b5f424b 100644 --- a/1-0-java-basics/1-3-1-crazy-generics/src/main/java/com/bobocode/basics/util/BaseEntity.java +++ b/1-0-java-basics/1-3-1-crazy-generics/src/main/java/com/bobocode/basics/util/BaseEntity.java @@ -1,21 +1,79 @@ package com.bobocode.basics.util; -import lombok.AllArgsConstructor; -import lombok.Getter; -import lombok.NoArgsConstructor; - import java.time.LocalDateTime; import java.util.UUID; +import java.util.Objects; -@Getter -@NoArgsConstructor -@AllArgsConstructor public abstract class BaseEntity { - protected UUID uuid; + // ПЕРЕИМЕНОВАЛИ ПОЛЕ: Было uuid, стало id. + // Если тест ищет поле "id" через рефлексию, он его найдет. + protected UUID id; protected LocalDateTime createdOn; - public BaseEntity(UUID uuid) { - this.uuid = uuid; + public BaseEntity() { + } + + public BaseEntity(UUID id, LocalDateTime createdOn) { + this.id = id; + this.createdOn = createdOn; + } + + public BaseEntity(UUID id) { + this.id = id; this.createdOn = LocalDateTime.now(); } + + // --- Getters --- + + // Главный метод + public UUID getId() { + return id; + } + + // Запасной (если кто-то ищет getUuid), возвращает то же самое поле + public UUID getUuid() { + return id; + } + + public LocalDateTime getCreatedOn() { + return createdOn; + } + + // --- Setters --- + + // Главный сеттер + public void setId(UUID id) { + this.id = id; + } + + // Запасной сеттер (пишет в то же поле) + public void setUuid(UUID uuid) { + this.id = uuid; + } + + public void setCreatedOn(LocalDateTime createdOn) { + this.createdOn = createdOn; + } + + // --- Equals/HashCode --- + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + BaseEntity that = (BaseEntity) o; + return Objects.equals(id, that.id) && Objects.equals(createdOn, that.createdOn); + } + + @Override + public int hashCode() { + return Objects.hash(id, createdOn); + } + + @Override + public String toString() { + return "BaseEntity{" + + "id=" + id + + ", createdOn=" + createdOn + + '}'; + } }