Skip to content

Commit 15616bd

Browse files
committed
Variance Refactoring
1 parent 577df8b commit 15616bd

13 files changed

Lines changed: 132 additions & 192 deletions

File tree

src/main/java/com/ab/generics/basic/NamingGeneric.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package com.ab.generics.basic;
22

3+
import java.util.HashSet;
4+
35
/**
46
* @author Arpit Bhardwaj
57
*
68
* A Genric Type could be a word or a single letter
79
*/
810

9-
//Allowed
10-
public class NamingGeneric<ClassGeneric> {
1111

12+
public class NamingGeneric<ClassGeneric,GenericX,Y,Number,Object> {
13+
//keep in mind that Number and Object are just words here do not get confused with class names
1214
public <MethodGeneric> MethodGeneric test(MethodGeneric t){
1315
return null;
1416
}
@@ -23,10 +25,18 @@ public <M> ClassGeneric test5(M t){
2325
return null;
2426
}
2527

28+
//here Number is just the generic type not the Number class
29+
public <M> Number test6(Number t){
30+
return null;
31+
}
32+
2633
//here ClassGeneric hides the ClassGeneric Type at class level
2734
public <ClassGeneric> ClassGeneric test2(ClassGeneric t){
2835
return null;
2936
}
37+
38+
public static void main(String[] args) {
39+
}
3040
}
3141
//public class NamingGeneric<Object> {}
3242
//public class NamingGeneric<String>{}

src/main/java/com/ab/generics/collections/ListExamples.java

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/main/java/com/ab/generics/collections/MapExamples.java

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/main/java/com/ab/generics/collections/SetExamples.java

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/main/java/com/ab/generics/erasure/Legacy.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
/**
99
* @author Arpit Bhardwaj
10+
*
11+
* Compiler adds bridge synthetic method for backword compatability
1012
*/
1113
//original source code
1214
public class Legacy implements UnaryOperator<String> {

src/main/java/com/ab/generics/limitation/InstanceOf.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,23 @@
22

33
/**
44
* @author Arpit Bhardwaj
5+
*
6+
* generics in instanceOf not allowed
57
*/
68
public class InstanceOf<T> {
79
@Override
810
public boolean equals(Object obj) {
9-
//generics in instanceOf not allowed
11+
1012
if(obj instanceof InstanceOf/*<T>*/){
1113
return true;
1214
}
1315
return false;
1416
}
1517

1618
public static void main(String[] args) {
17-
System.out.println(new InstanceOf<>() instanceof Object);
19+
InstanceOf<String> obj1 = new InstanceOf<>();
20+
InstanceOf<String> obj2 = new InstanceOf<>();
21+
22+
System.out.println(obj1.equals(obj2));
1823
}
1924
}

src/main/java/com/ab/generics/limitation/Overloading.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
/**
66
* @author Arpit Bhardwaj
7+
*
8+
* You cannot overloads methods by changing generic information only
79
*/
810
public class Overloading {
911
public void print (String param){}
1012
public void print (Integer param){}
1113
public void print (List<String> param){}
1214

13-
//cannot do
14-
//public void print (List<Integer> param){}
15-
//public void print (List<List<Integer>> param){}
15+
//public void print (List<Integer> param){} // DOES NOT COMPILE
16+
//public void print (List<List<Integer>> param){} // DOES NOT COMPILE
1617
}

src/main/java/com/ab/generics/limitation/UncompilableException.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
package com.ab.generics.limitation;
22

33
/**
4-
* Compile time error: Generic class may not extend 'java.lang.Throwable'
54
* @author Arpit Bhardwaj
5+
*
6+
* Generic Exceptions are not allowed in java
7+
* means any class that extends throwable cannot be generic
8+
*
69
*/
710
public class UncompilableException/*<T>*/ extends Throwable {
811

912
public static void main(String[] args) {
1013
try{
1114
throw new UncompilableException();
12-
}catch(UncompilableException/*<T>*/ e){
15+
}catch(UncompilableException e){
1316
e.printStackTrace();
1417
}
1518
}

src/main/java/com/ab/generics/methods/ListMinimum.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,5 @@ private static Object min(List values, Comparator comparator) {
5050
return lowestElement;
5151
}
5252

53-
public static void test(Collection<?> collection){
54-
55-
}
53+
public static void test(Collection<?> collection){}
5654
}

src/main/java/com/ab/generics/methods/ListMinimumGeneric.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ public int compare(Person o1, Person o2) {
4646
System.out.println(min(numbers,Integer::compare));
4747

4848
}
49-
private static <T> T min(List<T> values, Comparator<T> comparator) {
49+
private static <M> M min(List<M> values, Comparator<M> comparator) {
5050
if (values.isEmpty()) {
5151
throw new IllegalArgumentException("List is empty, cannot find minimum");
5252
}
5353

54-
T lowestElement = values.get(0);
54+
M lowestElement = values.get(0);
5555
for (int i = 1; i < values.size(); i++) {
56-
final T element = values.get(i);
56+
final M element = values.get(i);
5757
if (comparator.compare(element, lowestElement) < 0) {
5858
lowestElement = element;
5959
}

0 commit comments

Comments
 (0)