Skip to content

Commit 1a73061

Browse files
ochejaadamd1985
authored andcommitted
BAEL-922 Type Erasure Explained (eugenp#2100)
* Define beans for handling different message types in a lean chat app * Add class based spring beans configuration * Define spring configuration in XML for constructor based bean injection * Refactor package structure to separate constructor based bean injection code set from setter based bean injection code set * Define configuration and classes specific to setter-based bean injection. * Implement tests for constructor-based and setter-based bean injections * develop codes for explaining type erasure * Write unit tests for type erasure examples * Remove evaluation article code * Modify type erasure examples and unit tests * Modify type erasure examples and unit tests * Add expected exception in TypeErasureUnitTest * Correct grammar in class name
1 parent f04ceab commit 1a73061

5 files changed

Lines changed: 126 additions & 0 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.typeerasure;
2+
3+
public class ArrayContentPrintUtil {
4+
5+
public static <E> void printArray(E[] array) {
6+
for (E element : array) {
7+
System.out.printf("%s ", element);
8+
}
9+
}
10+
11+
public static <E extends Comparable<E>> void printArray(E[] array) {
12+
for (E element : array) {
13+
System.out.printf("%s ", element);
14+
}
15+
}
16+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baeldung.typeerasure;
2+
3+
import java.util.Arrays;
4+
5+
public class BoundStack<E extends Comparable<E>> {
6+
7+
private E[] stackContent;
8+
private int total;
9+
10+
public BoundStack(int capacity) {
11+
this.stackContent = (E[]) new Object[capacity];
12+
}
13+
14+
public void push(E data) {
15+
if (total == stackContent.length) {
16+
resize(2 * stackContent.length);
17+
}
18+
stackContent[total++] = data;
19+
}
20+
21+
public E pop() {
22+
if (!isEmpty()) {
23+
E datum = stackContent[total];
24+
stackContent[total--] = null;
25+
return datum;
26+
}
27+
return null;
28+
}
29+
30+
private void resize(int capacity) {
31+
Arrays.copyOf(stackContent, capacity);
32+
}
33+
34+
public boolean isEmpty() {
35+
return total == 0;
36+
}
37+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.typeerasure;
2+
3+
public class IntegerStack extends Stack<Integer> {
4+
5+
public IntegerStack(int capacity) {
6+
super(capacity);
7+
}
8+
9+
public void push(Integer value) {
10+
System.out.println("Pushing into my integerStack");
11+
super.push(value);
12+
}
13+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.baeldung.typeerasure;
2+
3+
import java.util.Arrays;
4+
5+
public class Stack<E> {
6+
7+
private E[] stackContent;
8+
private int total;
9+
10+
public Stack(int capacity) {
11+
this.stackContent = (E[]) new Object[capacity];
12+
}
13+
14+
public void push(E data) {
15+
System.out.println("In base stack push#");
16+
if (total == stackContent.length) {
17+
resize(2 * stackContent.length);
18+
}
19+
stackContent[total++] = data;
20+
}
21+
22+
public E pop() {
23+
if (!isEmpty()) {
24+
E datum = stackContent[total];
25+
stackContent[total--] = null;
26+
return datum;
27+
}
28+
return null;
29+
}
30+
31+
private void resize(int capacity) {
32+
Arrays.copyOf(stackContent, capacity);
33+
}
34+
35+
public boolean isEmpty() {
36+
return total == 0;
37+
}
38+
39+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.typeerasure;
2+
3+
import org.junit.Test;
4+
5+
public class TypeErasureUnitTest {
6+
7+
@Test(expected = ClassCastException.class)
8+
public void givenIntegerStack_whenStringPushedAndAssignPoppedValueToInteger_shouldFail() {
9+
IntegerStack integerStack = new IntegerStack(5);
10+
Stack stack = integerStack;
11+
stack.push("Hello");
12+
Integer data = integerStack.pop();
13+
}
14+
15+
@Test
16+
public void givenAnyArray_whenInvokedPrintArray_shouldSucceed() {
17+
Integer[] scores = new Integer[] { 100, 200, 10, 99, 20 };
18+
ArrayContentPrintUtil.printArray(scores);
19+
}
20+
21+
}

0 commit comments

Comments
 (0)