[JAVA-621] Flattened modules hierarchy

This commit is contained in:
dupirefr
2020-04-07 21:28:45 +02:00
parent 635ecae691
commit 42b0086080
210 changed files with 24 additions and 44 deletions

View File

@@ -0,0 +1,38 @@
package com.baeldung.generics;
import java.io.Serializable;
public class Entry {
private String data;
private int rank;
// non-generic constructor
public Entry(String data, int rank) {
this.data = data;
this.rank = rank;
}
// generic constructor
public <E extends Rankable & Serializable> Entry(E element) {
this.data = element.toString();
this.rank = element.getRank();
}
// getters and setters
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
}

View File

@@ -0,0 +1,53 @@
package com.baeldung.generics;
import java.io.Serializable;
import java.util.Optional;
public class GenericEntry<T> {
private T data;
private int rank;
// non-generic constructor
public GenericEntry(int rank) {
this.rank = rank;
}
// generic constructor
public GenericEntry(T data, int rank) {
this.data = data;
this.rank = rank;
}
// generic constructor with different type
public <E extends Rankable & Serializable> GenericEntry(E element) {
this.data = (T) element;
this.rank = element.getRank();
}
// generic constructor with different type and wild card
public GenericEntry(Optional<? extends Rankable> optional) {
if (optional.isPresent()) {
this.data = (T) optional.get();
this.rank = optional.get()
.getRank();
}
}
// getters and setters
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
}

View File

@@ -0,0 +1,34 @@
package com.baeldung.generics;
public class MapEntry<K, V> {
private K key;
private V value;
public MapEntry() {
super();
}
// generic constructor with two parameters
public MapEntry(K key, V value) {
this.key = key;
this.value = value;
}
// getters and setters
public K getKey() {
return key;
}
public void setKey(K key) {
this.key = key;
}
public V getValue() {
return value;
}
public void setValue(V value) {
this.value = value;
}
}

View File

@@ -0,0 +1,50 @@
package com.baeldung.generics;
import java.io.Serializable;
public class Product implements Rankable, Serializable {
private String name;
private double price;
private int sales;
public Product(String name, double price) {
this.name = name;
this.price = price;
}
@Override
public int getRank() {
return sales;
}
@Override
public String toString() {
return "Product [name=" + name + ", price=" + price + ", sales=" + sales + "]";
}
// getters and setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getSales() {
return sales;
}
public void setSales(int sales) {
this.sales = sales;
}
}

View File

@@ -0,0 +1,5 @@
package com.baeldung.generics;
public interface Rankable {
public int getRank();
}

View File

@@ -0,0 +1,25 @@
package com.baeldung.rawtype;
import java.util.ArrayList;
import java.util.List;
public class RawTypeDemo {
public static void main(String[] args) {
RawTypeDemo rawTypeDemo = new RawTypeDemo();
rawTypeDemo.methodA();
}
public void methodA() {
// parameterized type
List<String> listStr = new ArrayList<>();
listStr.add("Hello Folks!");
methodB(listStr);
String s = listStr.get(1); // ClassCastException at run time
}
public void methodB(List rawList) { // Inexpressive raw type
rawList.add(1); // Unsafe operation
}
}

View File

@@ -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);
}
}
}

View File

@@ -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;
}
}

View File

@@ -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);
}
}

View File

@@ -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;
}
}

View File

@@ -0,0 +1,76 @@
package com.baeldung.generics;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import java.io.Serializable;
import java.util.Optional;
import org.junit.Test;
public class GenericConstructorUnitTest {
@Test
public void givenNonGenericConstructor_whenCreateNonGenericEntry_thenOK() {
Entry entry = new Entry("sample", 1);
assertEquals("sample", entry.getData());
assertEquals(1, entry.getRank());
}
@Test
public void givenGenericConstructor_whenCreateNonGenericEntry_thenOK() {
Product product = new Product("milk", 2.5);
product.setSales(30);
Entry entry = new Entry(product);
assertEquals(product.toString(), entry.getData());
assertEquals(30, entry.getRank());
}
@Test
public void givenNonGenericConstructor_whenCreateGenericEntry_thenOK() {
GenericEntry<String> entry = new GenericEntry<String>(1);
assertNull(entry.getData());
assertEquals(1, entry.getRank());
}
@Test
public void givenGenericConstructor_whenCreateGenericEntry_thenOK() {
GenericEntry<String> entry = new GenericEntry<String>("sample", 1);
assertEquals("sample", entry.getData());
assertEquals(1, entry.getRank());
}
@Test
public void givenGenericConstructorWithDifferentType_whenCreateGenericEntry_thenOK() {
Product product = new Product("milk", 2.5);
product.setSales(30);
GenericEntry<Serializable> entry = new GenericEntry<Serializable>(product);
assertEquals(product, entry.getData());
assertEquals(30, entry.getRank());
}
@Test
public void givenGenericConstructorWithWildCard_whenCreateGenericEntry_thenOK() {
Product product = new Product("milk", 2.5);
product.setSales(30);
Optional<Product> optional = Optional.of(product);
GenericEntry<Serializable> entry = new GenericEntry<Serializable>(optional);
assertEquals(product, entry.getData());
assertEquals(30, entry.getRank());
}
@Test
public void givenGenericConstructor_whenCreateGenericEntryWithTwoTypes_thenOK() {
MapEntry<String, Integer> entry = new MapEntry<String, Integer>("sample", 1);
assertEquals("sample", entry.getKey());
assertEquals(1, entry.getValue()
.intValue());
}
}

View File

@@ -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);
}
}