diff --git a/core-java/src/main/java/com/baeldung/typeerasure/ArrayContentPrintUtil.java b/core-java/src/main/java/com/baeldung/typeerasure/ArrayContentPrintUtil.java new file mode 100644 index 0000000000..79c18916c2 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/typeerasure/ArrayContentPrintUtil.java @@ -0,0 +1,16 @@ +package com.baeldung.typeerasure; + +public class ArrayContentPrintUtil { + + public static void printArray(E[] array) { + for (E element : array) { + System.out.printf("%s ", element); + } + } + + public static > void printArray(E[] array) { + for (E element : array) { + System.out.printf("%s ", element); + } + } +} diff --git a/core-java/src/main/java/com/baeldung/typeerasure/BoundStack.java b/core-java/src/main/java/com/baeldung/typeerasure/BoundStack.java new file mode 100644 index 0000000000..6d158fda77 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/typeerasure/BoundStack.java @@ -0,0 +1,37 @@ +package com.baeldung.typeerasure; + +import java.util.Arrays; + +public class BoundStack> { + + 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; + } +} diff --git a/core-java/src/main/java/com/baeldung/typeerasure/IntegerStack.java b/core-java/src/main/java/com/baeldung/typeerasure/IntegerStack.java new file mode 100644 index 0000000000..ad1da1239d --- /dev/null +++ b/core-java/src/main/java/com/baeldung/typeerasure/IntegerStack.java @@ -0,0 +1,13 @@ +package com.baeldung.typeerasure; + +public class IntegerStack extends Stack { + + public IntegerStack(int capacity) { + super(capacity); + } + + public void push(Integer value) { + System.out.println("Pushing into my integerStack"); + super.push(value); + } +} diff --git a/core-java/src/main/java/com/baeldung/typeerasure/Stack.java b/core-java/src/main/java/com/baeldung/typeerasure/Stack.java new file mode 100644 index 0000000000..b88ef6ad8b --- /dev/null +++ b/core-java/src/main/java/com/baeldung/typeerasure/Stack.java @@ -0,0 +1,39 @@ +package com.baeldung.typeerasure; + +import java.util.Arrays; + +public class Stack { + + 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; + } + +} diff --git a/core-java/src/test/java/com/baeldung/typeerasure/TypeErasureUnitTest.java b/core-java/src/test/java/com/baeldung/typeerasure/TypeErasureUnitTest.java new file mode 100644 index 0000000000..060f177265 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/typeerasure/TypeErasureUnitTest.java @@ -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); + } + +}