[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:
@@ -3,9 +3,7 @@
|
||||
This module contains articles about Object-oriented programming (OOP) in Java
|
||||
|
||||
### Relevant Articles:
|
||||
- [Generic Constructors in Java](https://www.baeldung.com/java-generic-constructors)
|
||||
- [Anonymous Classes in Java](https://www.baeldung.com/java-anonymous-classes)
|
||||
- [Raw Types in Java](https://www.baeldung.com/raw-types-java)
|
||||
- [Marker Interfaces in Java](https://www.baeldung.com/java-marker-interfaces)
|
||||
- [Java equals() and hashCode() Contracts](https://www.baeldung.com/java-equals-hashcode-contracts)
|
||||
- [Static and Default Methods in Interfaces in Java](https://www.baeldung.com/java-static-default-methods)
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package com.baeldung.generics;
|
||||
|
||||
public interface Rankable {
|
||||
public int getRank();
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
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
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,76 +0,0 @@
|
||||
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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user