Skip to content

Commit e99d353

Browse files
committed
GP-119 update after review
* add missing tests * update javadoc * add README.MD
1 parent 06abef9 commit e99d353

File tree

3 files changed

+162
-55
lines changed

3 files changed

+162
-55
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# <img src="https://raw.githubusercontent.com/bobocode-projects/resources/master/image/logo_transparent_background.png" height=50/>Crazy Generics
2+
#### Learn tricky nuances and become the master of type parametrization in Java 💪
3+
4+
### WHY ❓
5+
Generics in Java provide **safe type parametrization.** Sou can write the logic of a method or a whole class and
6+
**reuse it for different types**. Before Java 5 (when generics were introduced), common logic could be reused as well.
7+
However, it required **using `Object` everywhere and casting it everytime you want to get an actual type**.
8+
It **could easily cause `ClassCastException` at runtime which is not safe.** When using generics, it's still the same `Object`,
9+
but **now compiler creates casts for you implicitly, and makes sure you didn't mess up with types**, which **moves errors from
10+
runtime to compile time and makes type parametrization safer.**
11+
12+
### Objectives
13+
* create a generic class with **simple type parameter**
14+
* create a generic class with **bounded type parameter**
15+
* create a generic class with **multiples type parameter**
16+
* create a generic class with **recursively bounded type parameter**
17+
* create a **generic method**
18+
* create a method that accepts a generic class on any type using **wildcard**
19+
* create method parameter using **bounded wildcard**
20+
21+
### Related materials ℹ️
22+
* [Effective Java, Chapter 5 – Generics](https://read.amazon.com/kp/embed?asin=B078H61SCH&preview=newtab&linkCode=kpe&ref_=cm_sw_r_kb_dp_SADNB2C41TWARGY4QGKZ) 📘
23+
24+
25+
---
26+
#### 🆕 First time here? – [See Introduction](https://github.com/bobocode-projects/java-fundamentals-course/tree/main/0-0-intro#introduction)
27+
#### ➡️ Have any feedback? – [Please fill the form ](https://forms.gle/UADe1YKHiFVXQJLF8)
28+

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ public interface Converter { // todo: introduce type parameters
6565
*
6666
* @param <T> – value type
6767
*/
68-
public static class MaxHolder { // todo: refactor class to make it generic
69-
private Object max;
68+
public static class MaxHolder<T extends Comparable<T>> { // todo: refactor class to make it generic
69+
private T max;
7070

71-
public MaxHolder(Object max) {
71+
public MaxHolder(T max) {
7272
this.max = max;
7373
}
7474

@@ -77,11 +77,13 @@ public MaxHolder(Object max) {
7777
*
7878
* @param val a new value
7979
*/
80-
public void put(Object val) {
81-
throw new ExerciseNotCompletedException(); // todo: update parameter and implement the method
80+
public void put(T val) {
81+
if (val.compareTo(max) > 0) {
82+
max = val;
83+
}
8284
}
8385

84-
public Object getMax() {
86+
public T getMax() {
8587
return max;
8688
}
8789
}
@@ -160,8 +162,9 @@ public static boolean hasNewEntities(Collection<BaseEntity> entities) {
160162
}
161163

162164
/**
163-
* Util method that checks if a provided collection of entities is valid. A validation criteria can be different
164-
* for different cases, so it is passed as second parameter.
165+
* Util method that checks if a provided collection of entities is valid. An entity is any subclass of
166+
* a {@link BaseEntity} A validation criteria can be different for different cases, so it is passed
167+
* as second parameter.
165168
*
166169
* @param entities provided collection of entities
167170
* @param validationPredicate criteria for validation

0 commit comments

Comments
 (0)