From 1a73061761fa828017006d07145c058391cc6fc6 Mon Sep 17 00:00:00 2001 From: ocheja Date: Tue, 4 Jul 2017 06:39:12 +0900 Subject: [PATCH] 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 --- .../typeerasure/ArrayContentPrintUtil.java | 16 ++++++++ .../com/baeldung/typeerasure/BoundStack.java | 37 ++++++++++++++++++ .../baeldung/typeerasure/IntegerStack.java | 13 +++++++ .../java/com/baeldung/typeerasure/Stack.java | 39 +++++++++++++++++++ .../typeerasure/TypeErasureUnitTest.java | 21 ++++++++++ 5 files changed, 126 insertions(+) create mode 100644 core-java/src/main/java/com/baeldung/typeerasure/ArrayContentPrintUtil.java create mode 100644 core-java/src/main/java/com/baeldung/typeerasure/BoundStack.java create mode 100644 core-java/src/main/java/com/baeldung/typeerasure/IntegerStack.java create mode 100644 core-java/src/main/java/com/baeldung/typeerasure/Stack.java create mode 100644 core-java/src/test/java/com/baeldung/typeerasure/TypeErasureUnitTest.java 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); + } + +}