BAEL-922 Type Erasure Explained (#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
This commit is contained in:
parent
f04ceabcc5
commit
1a73061761
|
@ -0,0 +1,16 @@
|
||||||
|
package com.baeldung.typeerasure;
|
||||||
|
|
||||||
|
public class ArrayContentPrintUtil {
|
||||||
|
|
||||||
|
public static <E> void printArray(E[] array) {
|
||||||
|
for (E element : array) {
|
||||||
|
System.out.printf("%s ", element);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <E extends Comparable<E>> void printArray(E[] array) {
|
||||||
|
for (E element : array) {
|
||||||
|
System.out.printf("%s ", element);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,37 @@
|
||||||
|
package com.baeldung.typeerasure;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
public class BoundStack<E extends Comparable<E>> {
|
||||||
|
|
||||||
|
private E[] stackContent;
|
||||||
|
private int total;
|
||||||
|
|
||||||
|
public BoundStack(int capacity) {
|
||||||
|
this.stackContent = (E[]) new Object[capacity];
|
||||||
|
}
|
||||||
|
|
||||||
|
public void push(E data) {
|
||||||
|
if (total == stackContent.length) {
|
||||||
|
resize(2 * stackContent.length);
|
||||||
|
}
|
||||||
|
stackContent[total++] = data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public E pop() {
|
||||||
|
if (!isEmpty()) {
|
||||||
|
E datum = stackContent[total];
|
||||||
|
stackContent[total--] = null;
|
||||||
|
return datum;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void resize(int capacity) {
|
||||||
|
Arrays.copyOf(stackContent, capacity);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isEmpty() {
|
||||||
|
return total == 0;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
package com.baeldung.typeerasure;
|
||||||
|
|
||||||
|
public class IntegerStack extends Stack<Integer> {
|
||||||
|
|
||||||
|
public IntegerStack(int capacity) {
|
||||||
|
super(capacity);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void push(Integer value) {
|
||||||
|
System.out.println("Pushing into my integerStack");
|
||||||
|
super.push(value);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,39 @@
|
||||||
|
package com.baeldung.typeerasure;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
public class Stack<E> {
|
||||||
|
|
||||||
|
private E[] stackContent;
|
||||||
|
private int total;
|
||||||
|
|
||||||
|
public Stack(int capacity) {
|
||||||
|
this.stackContent = (E[]) new Object[capacity];
|
||||||
|
}
|
||||||
|
|
||||||
|
public void push(E data) {
|
||||||
|
System.out.println("In base stack push#");
|
||||||
|
if (total == stackContent.length) {
|
||||||
|
resize(2 * stackContent.length);
|
||||||
|
}
|
||||||
|
stackContent[total++] = data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public E pop() {
|
||||||
|
if (!isEmpty()) {
|
||||||
|
E datum = stackContent[total];
|
||||||
|
stackContent[total--] = null;
|
||||||
|
return datum;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void resize(int capacity) {
|
||||||
|
Arrays.copyOf(stackContent, capacity);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isEmpty() {
|
||||||
|
return total == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,21 @@
|
||||||
|
package com.baeldung.typeerasure;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class TypeErasureUnitTest {
|
||||||
|
|
||||||
|
@Test(expected = ClassCastException.class)
|
||||||
|
public void givenIntegerStack_whenStringPushedAndAssignPoppedValueToInteger_shouldFail() {
|
||||||
|
IntegerStack integerStack = new IntegerStack(5);
|
||||||
|
Stack stack = integerStack;
|
||||||
|
stack.push("Hello");
|
||||||
|
Integer data = integerStack.pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAnyArray_whenInvokedPrintArray_shouldSucceed() {
|
||||||
|
Integer[] scores = new Integer[] { 100, 200, 10, 99, 20 };
|
||||||
|
ArrayContentPrintUtil.printArray(scores);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue