5
5
import lombok .Data ;
6
6
7
7
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 ;
12
10
13
11
/**
14
12
* {@link CrazyGenerics} is an exercise class. It consists of classes, interfaces and methods that should be updated
@@ -33,8 +31,8 @@ public class CrazyGenerics {
33
31
* @param <T> – value type
34
32
*/
35
33
@ 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 ;
38
36
private String source ;
39
37
}
40
38
@@ -45,11 +43,10 @@ public static class Sourced { // todo: refactor class to introduce type paramete
45
43
* @param <T> – actual, min and max type
46
44
*/
47
45
@ 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 ;
53
50
}
54
51
55
52
/**
@@ -59,8 +56,8 @@ public static class Limited {
59
56
* @param <T> – source object type
60
57
* @param <R> - converted result type
61
58
*/
62
- public interface Converter { // todo: introduce type parameters
63
- // todo: add convert method
59
+ public interface Converter < T , R > {
60
+ R convert ( T value );
64
61
}
65
62
66
63
/**
@@ -70,10 +67,10 @@ public interface Converter { // todo: introduce type parameters
70
67
*
71
68
* @param <T> – value type
72
69
*/
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 ;
75
72
76
- public MaxHolder (Object max ) {
73
+ public MaxHolder (T max ) {
77
74
this .max = max ;
78
75
}
79
76
@@ -82,11 +79,13 @@ public MaxHolder(Object max) {
82
79
*
83
80
* @param val a new value
84
81
*/
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
+ }
87
86
}
88
87
89
- public Object getMax () {
88
+ public T getMax () {
90
89
return max ;
91
90
}
92
91
}
@@ -97,8 +96,8 @@ public Object getMax() {
97
96
*
98
97
* @param <T> – the type of objects that can be processed
99
98
*/
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 );
102
101
}
103
102
104
103
/**
@@ -108,19 +107,21 @@ interface StrictProcessor { // todo: make it generic
108
107
* @param <T> – a type of the entity that should be a subclass of {@link BaseEntity}
109
108
* @param <C> – a type of any collection
110
109
*/
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 );
113
112
114
- Collection < Object > getEntityCollection ();
113
+ C getEntityCollection ();
115
114
}
116
115
116
+
117
117
/**
118
118
* {@link ListRepository} extends {@link CollectionRepository} but specifies the underlying collection as
119
119
* {@link java.util.List}.
120
120
*
121
121
* @param <T> – a type of the entity that should be a subclass of {@link BaseEntity}
122
122
*/
123
- interface ListRepository { // todo: update interface according to the javadoc
123
+ interface ListRepository <T extends BaseEntity > extends CollectionRepository <T , List <T >> {
124
+
124
125
}
125
126
126
127
/**
@@ -133,7 +134,12 @@ interface ListRepository { // todo: update interface according to the javadoc
133
134
*
134
135
* @param <E> a type of collection elements
135
136
*/
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
+ }
137
143
}
138
144
139
145
/**
@@ -147,8 +153,8 @@ static class CollectionUtil {
147
153
*
148
154
* @param list
149
155
*/
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
+
152
158
list .forEach (element -> System .out .println (" – " + element ));
153
159
}
154
160
@@ -160,8 +166,9 @@ public static void print(List<Integer> list) {
160
166
* @param entities provided collection of entities
161
167
* @return true if at least one of the elements has null id
162
168
*/
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 );
165
172
}
166
173
167
174
/**
@@ -173,10 +180,13 @@ public static boolean hasNewEntities(Collection<BaseEntity> entities) {
173
180
* @param validationPredicate criteria for validation
174
181
* @return true if all entities fit validation criteria
175
182
*/
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 );
178
187
}
179
188
189
+
180
190
/**
181
191
* hasDuplicates is a generic util method checks if a list of entities contains target entity more than once.
182
192
* 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() {
187
197
* @param <T> entity type
188
198
* @return true if entities list contains target entity more than once
189
199
*/
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 ;
192
204
}
193
205
194
206
/**
@@ -200,8 +212,21 @@ public static boolean hasDuplicates() {
200
212
* @param <T> type of elements
201
213
* @return optional max value
202
214
*/
203
- // todo: create a method and implement its logic manually without using util method from JDK
204
215
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
+ }
205
230
/**
206
231
* findMostRecentlyCreatedEntity is a generic util method that accepts a collection of entities and returns the
207
232
* one that is the most recently created. If collection is empty,
@@ -214,7 +239,11 @@ public static boolean hasDuplicates() {
214
239
* @param <T> entity type
215
240
* @return an entity from the given collection that has the max createdOn value
216
241
*/
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
+ }
218
247
219
248
/**
220
249
* 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() {
228
257
public static void swap (List <?> elements , int i , int j ) {
229
258
Objects .checkIndex (i , elements .size ());
230
259
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 );
232
267
}
233
268
234
269
}
0 commit comments