Skip to content

Commit 8c0c8c7

Browse files
Ivan SemkivIvan Semkiv
authored andcommitted
Виконані завдання з java-fundamentals-exercises
1 parent be2a321 commit 8c0c8c7

File tree

2 files changed

+78
-44
lines changed
  • 1-0-java-basics
    • 1-3-0-hello-generics/src/main/java/com/bobocode/basics
    • 1-3-1-crazy-generics/src/main/java/com/bobocode/basics

2 files changed

+78
-44
lines changed

1-0-java-basics/1-3-0-hello-generics/src/main/java/com/bobocode/basics/Box.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@
77
* <p>
88
* todo: refactor this class so it uses generic type "T" and run {@link com.bobocode.basics.BoxTest} to verify it
99
*/
10-
public class Box {
11-
private Object value;
10+
public class Box<T> {
11+
private T value;
1212

13-
public Box(Object value) {
13+
public Box(T value) {
1414
this.value = value;
1515
}
1616

17-
public Object getValue() {
17+
public T getValue() {
1818
return value;
1919
}
2020

21-
public void setValue(Object value) {
21+
public void setValue(T value) {
2222
this.value = value;
2323
}
24-
}
24+
}

1-0-java-basics/1-3-1-crazy-generics/src/main/java/com/bobocode/basics/CrazyGenerics.java

Lines changed: 72 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
package com.bobocode.basics;
22

33
import com.bobocode.basics.util.BaseEntity;
4-
import com.bobocode.util.ExerciseNotCompletedException;
54
import lombok.Data;
65

76
import java.io.Serializable;
8-
import java.util.Collection;
9-
import java.util.Comparator;
10-
import java.util.List;
11-
import java.util.Objects;
7+
import java.util.*;
8+
import java.util.function.Predicate;
129

1310
/**
1411
* {@link CrazyGenerics} is an exercise class. It consists of classes, interfaces and methods that should be updated
@@ -33,8 +30,8 @@ public class CrazyGenerics {
3330
* @param <T> – value type
3431
*/
3532
@Data
36-
public static class Sourced { // todo: refactor class to introduce type parameter and make value generic
37-
private Object value;
33+
public static class Sourced<T> { // todo: refactor class to introduce type parameter and make value generic
34+
private T value;
3835
private String source;
3936
}
4037

@@ -45,11 +42,11 @@ public static class Sourced { // todo: refactor class to introduce type paramete
4542
* @param <T> – actual, min and max type
4643
*/
4744
@Data
48-
public static class Limited {
45+
public static class Limited<T extends Number> {
4946
// todo: refactor class to introduce type param bounded by number and make fields generic numbers
50-
private final Object actual;
51-
private final Object min;
52-
private final Object max;
47+
private final T actual;
48+
private final T min;
49+
private final T max;
5350
}
5451

5552
/**
@@ -59,8 +56,9 @@ public static class Limited {
5956
* @param <T> – source object type
6057
* @param <R> - converted result type
6158
*/
62-
public interface Converter { // todo: introduce type parameters
59+
public interface Converter<T, R> { // todo: introduce type parameters
6360
// todo: add convert method
61+
R convert(T param);
6462
}
6563

6664
/**
@@ -70,10 +68,10 @@ public interface Converter { // todo: introduce type parameters
7068
*
7169
* @param <T> – value type
7270
*/
73-
public static class MaxHolder { // todo: refactor class to make it generic
74-
private Object max;
71+
public static class MaxHolder<T extends Comparable<? super T>> { // todo: refactor class to make it generic
72+
private T max;
7573

76-
public MaxHolder(Object max) {
74+
public MaxHolder(T max) {
7775
this.max = max;
7876
}
7977

@@ -82,11 +80,13 @@ public MaxHolder(Object max) {
8280
*
8381
* @param val a new value
8482
*/
85-
public void put(Object val) {
86-
throw new ExerciseNotCompletedException(); // todo: update parameter and implement the method
83+
public void put(T val) {
84+
if (val.compareTo(max) > 0) {
85+
max = val;
86+
} // todo: update parameter and implement the method
8787
}
8888

89-
public Object getMax() {
89+
public T getMax() {
9090
return max;
9191
}
9292
}
@@ -97,8 +97,8 @@ public Object getMax() {
9797
*
9898
* @param <T> – the type of objects that can be processed
9999
*/
100-
interface StrictProcessor { // todo: make it generic
101-
void process(Object obj);
100+
interface StrictProcessor<T extends Serializable & Comparable<? super T>> { // todo: make it generic
101+
void process(T obj);
102102
}
103103

104104
/**
@@ -108,19 +108,19 @@ interface StrictProcessor { // todo: make it generic
108108
* @param <T> – a type of the entity that should be a subclass of {@link BaseEntity}
109109
* @param <C> – a type of any collection
110110
*/
111-
interface CollectionRepository { // todo: update interface according to the javadoc
112-
void save(Object entity);
111+
interface CollectionRepository<T extends BaseEntity, C extends Collection<T>> { // todo: update interface according to the javadoc
112+
void save(T entity);
113113

114-
Collection<Object> getEntityCollection();
114+
C getEntityCollection();
115115
}
116116

117117
/**
118118
* {@link ListRepository} extends {@link CollectionRepository} but specifies the underlying collection as
119-
* {@link java.util.List}.
119+
* {@link List}.
120120
*
121121
* @param <T> – a type of the entity that should be a subclass of {@link BaseEntity}
122122
*/
123-
interface ListRepository { // todo: update interface according to the javadoc
123+
interface ListRepository<T extends BaseEntity> extends CollectionRepository<T, List<T>> { // todo: update interface according to the javadoc
124124
}
125125

126126
/**
@@ -133,7 +133,10 @@ interface ListRepository { // todo: update interface according to the javadoc
133133
*
134134
* @param <E> a type of collection elements
135135
*/
136-
interface ComparableCollection { // todo: refactor it to make generic and provide a default impl of compareTo
136+
interface ComparableCollection<E> extends Collection<E>, Comparable<Collection<?>> { // todo: refactor it to make generic and provide a default impl of compareTo
137+
default int compareTo(Collection<?> other) {
138+
return Integer.compare(this.size(), other.size());
139+
}
137140
}
138141

139142
/**
@@ -147,7 +150,7 @@ static class CollectionUtil {
147150
*
148151
* @param list
149152
*/
150-
public static void print(List<Integer> list) {
153+
public static void print(List<?> list) {
151154
// todo: refactor it so the list of any type can be printed, not only integers
152155
list.forEach(element -> System.out.println(" – " + element));
153156
}
@@ -160,8 +163,13 @@ public static void print(List<Integer> list) {
160163
* @param entities provided collection of entities
161164
* @return true if at least one of the elements has null id
162165
*/
163-
public static boolean hasNewEntities(Collection<BaseEntity> entities) {
164-
throw new ExerciseNotCompletedException(); // todo: refactor parameter and implement method
166+
public static boolean hasNewEntities(Collection<? extends BaseEntity> entities) {
167+
for (BaseEntity e : entities) {
168+
if (e.getUuid() == null) {
169+
return true; // todo: refactor parameter and implement method
170+
}
171+
}
172+
return false; // todo: refactor parameter and implement method
165173
}
166174

167175
/**
@@ -173,8 +181,10 @@ public static boolean hasNewEntities(Collection<BaseEntity> entities) {
173181
* @param validationPredicate criteria for validation
174182
* @return true if all entities fit validation criteria
175183
*/
176-
public static boolean isValidCollection() {
177-
throw new ExerciseNotCompletedException(); // todo: add method parameters and implement the logic
184+
public static boolean isValidCollection(Collection<? extends BaseEntity> entities, Predicate<? super BaseEntity> validationPredicate) {
185+
return entities.stream()
186+
.allMatch(validationPredicate);
187+
178188
}
179189

180190
/**
@@ -187,8 +197,10 @@ public static boolean isValidCollection() {
187197
* @param <T> entity type
188198
* @return true if entities list contains target entity more than once
189199
*/
190-
public static boolean hasDuplicates() {
191-
throw new ExerciseNotCompletedException(); // todo: update method signature and implement it
200+
public static <T extends BaseEntity> boolean hasDuplicates(List<T> entities, T targetEntity) {
201+
return entities.stream()
202+
.filter(entity -> entity.getUuid().equals(targetEntity.getUuid()))
203+
.count() > 1;
192204
}
193205

194206
/**
@@ -202,10 +214,23 @@ public static boolean hasDuplicates() {
202214
*/
203215
// todo: create a method and implement its logic manually without using util method from JDK
204216

217+
public static <T> Optional<T> findMax(Iterable<T> elements, Comparator<? super T> comparator) {
218+
if (elements.iterator().hasNext()) {
219+
T max = elements.iterator().next();
220+
for (T e : elements) {
221+
if (comparator.compare(e, max) > 0) {
222+
max = e;
223+
}
224+
}
225+
return Optional.of(max);
226+
}
227+
return Optional.empty();
228+
}
229+
205230
/**
206231
* findMostRecentlyCreatedEntity is a generic util method that accepts a collection of entities and returns the
207232
* one that is the most recently created. If collection is empty,
208-
* it throws {@link java.util.NoSuchElementException}.
233+
* it throws {@link NoSuchElementException}.
209234
* <p>
210235
* This method reuses findMax method and passes entities along with prepare comparator instance,
211236
* that is stored as constant CREATED_ON_COMPARATOR.
@@ -216,6 +241,11 @@ public static boolean hasDuplicates() {
216241
*/
217242
// todo: create a method according to JavaDoc and implement it using previous method
218243

244+
public static <T extends BaseEntity> T findMostRecentlyCreatedEntity(Collection<T> entities) {
245+
return findMax(entities, CREATED_ON_COMPARATOR).orElseThrow();
246+
}
247+
248+
219249
/**
220250
* An util method that allows to swap two elements of any list. It changes the list so the element with the index
221251
* 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() {
226256
* @param j index of the other element to swap
227257
*/
228258
public static void swap(List<?> elements, int i, int j) {
229-
Objects.checkIndex(i, elements.size());
230-
Objects.checkIndex(j, elements.size());
231-
throw new ExerciseNotCompletedException(); // todo: complete method implementation
259+
swapHelper(elements, i, j);
260+
}
261+
262+
private static <T> void swapHelper(List<T> elements, int i, int j) {
263+
T temp = elements.get(i);
264+
elements.set(i, elements.get(j));
265+
elements.set(j, temp);
232266
}
233267

234268
}
235-
}
269+
}

0 commit comments

Comments
 (0)