Merge pull request #6712 from Doha2012/master

add generic constructor examples
This commit is contained in:
Loredana Crusoveanu 2019-04-17 09:00:34 +03:00 committed by GitHub
commit ca93498c15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 256 additions and 0 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,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());
}
}