[JAVA-621] core-java-lang-oop-generics module

* Creation

* Moved code from https://www.baeldung.com/raw-types-java

* Moved code from https://www.baeldung.com/java-generic-constructors

* Moved code from https://www.baeldung.com/java-type-erasure

* Moved article references to the new README.md
This commit is contained in:
dupirefr
2020-04-04 09:29:47 +02:00
parent af08d3814b
commit 6baffe791f
16 changed files with 25 additions and 2 deletions

View File

@@ -1,16 +0,0 @@
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);
}
}
}

View File

@@ -1,37 +0,0 @@
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;
}
}

View File

@@ -1,13 +0,0 @@
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);
}
}

View File

@@ -1,39 +0,0 @@
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;
}
}

View File

@@ -1,21 +0,0 @@
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);
}
}