Skip to content

Commit 106306d

Browse files
committed
Merge remote-tracking branch 'origin/main'
2 parents f933d18 + 768edea commit 106306d

File tree

3 files changed

+77
-51
lines changed

3 files changed

+77
-51
lines changed

0-0-intro/src/main/java/com/bobocode/intro/ExerciseIntroduction.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,7 @@ public String getWelcomeMessage() {
2727
}
2828

2929
/**
30-
* Method encodeMessage accepts one {@link String} parameter and returns encoded {@link String}.
31-
* <p>
32-
* PLEASE NOTE THAT YOU WILL GET STUCK ON THIS METHOD INTENTIONALLY! ;)
33-
* <p>
34-
* Every exercise has a completed solution that is stored in the branch "completed". So in case you got stuck
35-
* and don't know what to do, go check out completed solution.
3630
*
37-
* @param message input message
38-
* @return encoded message
3931
*/
4032
public String encodeMessage(String message) {
4133
return Base64.getEncoder().encodeToString(message.getBytes());

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,19 @@
55
* is flexible, because we can store anything we want there. But it is not safe, because it requires runtime casting
66
* and there is no guarantee that we know the type of the stored value.
77
* <p>
8-
* todo: refactor this class so it uses generic type "T" and run {@link com.bobocode.basics.BoxTest} to verify it
98
*/
10-
public class Box {
11-
private Object value;
9+
public class Box<T> {
10+
private T value;
1211

13-
public Box(Object value) {
12+
public Box(T value) {
1413
this.value = value;
1514
}
1615

17-
public Object getValue() {
16+
public T getValue() {
1817
return value;
1918
}
2019

21-
public void setValue(Object value) {
20+
public void setValue(T value) {
2221
this.value = value;
2322
}
2423
}

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

Lines changed: 72 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
import lombok.Data;
66

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

1311
/**
1412
* {@link CrazyGenerics} is an exercise class. It consists of classes, interfaces and methods that should be updated
@@ -33,8 +31,8 @@ public class CrazyGenerics {
3331
* @param <T> – value type
3432
*/
3533
@Data
36-
public static class Sourced { // todo: refactor class to introduce type parameter and make value generic
37-
private Object value;
34+
public static class Sourced<T> { // todo: refactor class to introduce type parameter and make value generic
35+
private T value;
3836
private String source;
3937
}
4038

@@ -45,11 +43,10 @@ public static class Sourced { // todo: refactor class to introduce type paramete
4543
* @param <T> – actual, min and max type
4644
*/
4745
@Data
48-
public static class Limited {
49-
// 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;
46+
public static class Limited<T extends Number> {
47+
private final T actual;
48+
private final T min;
49+
private final T max;
5350
}
5451

5552
/**
@@ -59,8 +56,8 @@ 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
63-
// todo: add convert method
59+
public interface Converter<T, R> {
60+
R convert(T value);
6461
}
6562

6663
/**
@@ -70,10 +67,10 @@ public interface Converter { // todo: introduce type parameters
7067
*
7168
* @param <T> – value type
7269
*/
73-
public static class MaxHolder { // todo: refactor class to make it generic
74-
private Object max;
70+
public static class MaxHolder<T extends Comparable<? super T>> {
71+
private T max;
7572

76-
public MaxHolder(Object max) {
73+
public MaxHolder(T max) {
7774
this.max = max;
7875
}
7976

@@ -82,11 +79,13 @@ public MaxHolder(Object max) {
8279
*
8380
* @param val a new value
8481
*/
85-
public void put(Object val) {
86-
throw new ExerciseNotCompletedException(); // todo: update parameter and implement the method
82+
public void put(T val) {
83+
if(val.compareTo(max) > 0) {
84+
max = val;
85+
}
8786
}
8887

89-
public Object getMax() {
88+
public T getMax() {
9089
return max;
9190
}
9291
}
@@ -97,8 +96,8 @@ public Object getMax() {
9796
*
9897
* @param <T> – the type of objects that can be processed
9998
*/
100-
interface StrictProcessor { // todo: make it generic
101-
void process(Object obj);
99+
interface StrictProcessor<T extends Serializable & Comparable<? super T>> {
100+
void process(T obj);
102101
}
103102

104103
/**
@@ -108,19 +107,21 @@ interface StrictProcessor { // todo: make it generic
108107
* @param <T> – a type of the entity that should be a subclass of {@link BaseEntity}
109108
* @param <C> – a type of any collection
110109
*/
111-
interface CollectionRepository { // todo: update interface according to the javadoc
112-
void save(Object entity);
110+
interface CollectionRepository<T extends BaseEntity, C extends Collection<T>> {
111+
void save(T entity);
113112

114-
Collection<Object> getEntityCollection();
113+
C getEntityCollection();
115114
}
116115

116+
117117
/**
118118
* {@link ListRepository} extends {@link CollectionRepository} but specifies the underlying collection as
119119
* {@link java.util.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>> {
124+
124125
}
125126

126127
/**
@@ -133,7 +134,12 @@ interface ListRepository { // todo: update interface according to the javadoc
133134
*
134135
* @param <E> a type of collection elements
135136
*/
136-
interface ComparableCollection { // todo: refactor it to make generic and provide a default impl of compareTo
137+
interface ComparableCollection<E> extends Collection<E>, Comparable<Collection<?>> {
138+
139+
@Override
140+
default int compareTo(Collection<?> e) {
141+
return Integer.compare(this.size(), e.size());
142+
}
137143
}
138144

139145
/**
@@ -147,8 +153,8 @@ static class CollectionUtil {
147153
*
148154
* @param list
149155
*/
150-
public static void print(List<Integer> list) {
151-
// todo: refactor it so the list of any type can be printed, not only integers
156+
public static void print(List<?> list) {
157+
152158
list.forEach(element -> System.out.println(" – " + element));
153159
}
154160

@@ -160,8 +166,9 @@ public static void print(List<Integer> list) {
160166
* @param entities provided collection of entities
161167
* @return true if at least one of the elements has null id
162168
*/
163-
public static boolean hasNewEntities(Collection<BaseEntity> entities) {
164-
throw new ExerciseNotCompletedException(); // todo: refactor parameter and implement method
169+
public static boolean hasNewEntities(Collection<? extends BaseEntity> entities) {
170+
return entities.stream()
171+
.anyMatch(e -> e.getUuid() == null);
165172
}
166173

167174
/**
@@ -173,10 +180,13 @@ public static boolean hasNewEntities(Collection<BaseEntity> entities) {
173180
* @param validationPredicate criteria for validation
174181
* @return true if all entities fit validation criteria
175182
*/
176-
public static boolean isValidCollection() {
177-
throw new ExerciseNotCompletedException(); // todo: add method parameters and implement the logic
183+
public static boolean isValidCollection(Collection<? extends BaseEntity> entities,
184+
Predicate<? super BaseEntity> validationPredicate) {
185+
return entities.stream()
186+
.allMatch(validationPredicate);
178187
}
179188

189+
180190
/**
181191
* hasDuplicates is a generic util method checks if a list of entities contains target entity more than once.
182192
* In other words, it checks if target entity has duplicates in the provided list. A duplicate is an entity that
@@ -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(Collection<T> entities, T targetEntity) {
201+
return entities.stream()
202+
.filter(e -> e.getUuid().equals(targetEntity.getUuid()))
203+
.count() > 1;
192204
}
193205

194206
/**
@@ -200,8 +212,21 @@ public static boolean hasDuplicates() {
200212
* @param <T> type of elements
201213
* @return optional max value
202214
*/
203-
// todo: create a method and implement its logic manually without using util method from JDK
204215

216+
public static <T> Optional<T> findMax(Iterable<T> elements, Comparator<? super T> comparator) {
217+
Iterator<T> iterator = elements.iterator();
218+
T max = iterator.next();
219+
220+
while (iterator.hasNext()) {
221+
T current = iterator.next();
222+
223+
if (comparator.compare(current, max) > 0) {
224+
max = current;
225+
}
226+
}
227+
228+
return Optional.of(max);
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,
@@ -214,7 +239,11 @@ public static boolean hasDuplicates() {
214239
* @param <T> entity type
215240
* @return an entity from the given collection that has the max createdOn value
216241
*/
217-
// todo: create a method according to JavaDoc and implement it using previous method
242+
243+
public static <T extends BaseEntity> T findMostRecentlyCreatedEntity(Collection<T> entities) {
244+
return findMax(entities, CREATED_ON_COMPARATOR)
245+
.orElseThrow();
246+
}
218247

219248
/**
220249
* An util method that allows to swap two elements of any list. It changes the list so the element with the index
@@ -228,7 +257,13 @@ public static boolean hasDuplicates() {
228257
public static void swap(List<?> elements, int i, int j) {
229258
Objects.checkIndex(i, elements.size());
230259
Objects.checkIndex(j, elements.size());
231-
throw new ExerciseNotCompletedException(); // todo: complete method implementation
260+
swapHelper(elements, i, j);
261+
}
262+
263+
private static <T> void swapHelper(List<T> elements, int i, int j) {
264+
T temp = elements.get(i);
265+
elements.set(i, elements.get(j));
266+
elements.set(j, temp);
232267
}
233268

234269
}

0 commit comments

Comments
 (0)