From 00d6406ef9b343c3f64868b657e0350b77194f73 Mon Sep 17 00:00:00 2001 From: DOHA Date: Fri, 12 Apr 2019 16:31:22 +0200 Subject: [PATCH] add generic constructor examples --- .../java/com/baeldung/generics/Entry.java | 38 ++++++++++ .../com/baeldung/generics/GenericEntry.java | 53 +++++++++++++ .../java/com/baeldung/generics/MapEntry.java | 34 +++++++++ .../java/com/baeldung/generics/Product.java | 50 ++++++++++++ .../java/com/baeldung/generics/Rankable.java | 5 ++ .../generics/GenericConstructorUnitTest.java | 76 +++++++++++++++++++ 6 files changed, 256 insertions(+) create mode 100644 core-java-lang-syntax/src/main/java/com/baeldung/generics/Entry.java create mode 100644 core-java-lang-syntax/src/main/java/com/baeldung/generics/GenericEntry.java create mode 100644 core-java-lang-syntax/src/main/java/com/baeldung/generics/MapEntry.java create mode 100644 core-java-lang-syntax/src/main/java/com/baeldung/generics/Product.java create mode 100644 core-java-lang-syntax/src/main/java/com/baeldung/generics/Rankable.java create mode 100644 core-java-lang-syntax/src/test/java/com/baeldung/generics/GenericConstructorUnitTest.java diff --git a/core-java-lang-syntax/src/main/java/com/baeldung/generics/Entry.java b/core-java-lang-syntax/src/main/java/com/baeldung/generics/Entry.java new file mode 100644 index 0000000000..07759ca9ba --- /dev/null +++ b/core-java-lang-syntax/src/main/java/com/baeldung/generics/Entry.java @@ -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 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; + } + +} diff --git a/core-java-lang-syntax/src/main/java/com/baeldung/generics/GenericEntry.java b/core-java-lang-syntax/src/main/java/com/baeldung/generics/GenericEntry.java new file mode 100644 index 0000000000..27d3a44069 --- /dev/null +++ b/core-java-lang-syntax/src/main/java/com/baeldung/generics/GenericEntry.java @@ -0,0 +1,53 @@ +package com.baeldung.generics; + +import java.io.Serializable; +import java.util.Optional; + +public class GenericEntry { + 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 GenericEntry(E element) { + this.data = (T) element; + this.rank = element.getRank(); + } + + // generic constructor with different type and wild card + public GenericEntry(Optional 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; + } + +} diff --git a/core-java-lang-syntax/src/main/java/com/baeldung/generics/MapEntry.java b/core-java-lang-syntax/src/main/java/com/baeldung/generics/MapEntry.java new file mode 100644 index 0000000000..3d626b2fa5 --- /dev/null +++ b/core-java-lang-syntax/src/main/java/com/baeldung/generics/MapEntry.java @@ -0,0 +1,34 @@ +package com.baeldung.generics; + +public class MapEntry { + 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; + } + +} diff --git a/core-java-lang-syntax/src/main/java/com/baeldung/generics/Product.java b/core-java-lang-syntax/src/main/java/com/baeldung/generics/Product.java new file mode 100644 index 0000000000..bfc9f63071 --- /dev/null +++ b/core-java-lang-syntax/src/main/java/com/baeldung/generics/Product.java @@ -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; + } + +} diff --git a/core-java-lang-syntax/src/main/java/com/baeldung/generics/Rankable.java b/core-java-lang-syntax/src/main/java/com/baeldung/generics/Rankable.java new file mode 100644 index 0000000000..72bda67d9d --- /dev/null +++ b/core-java-lang-syntax/src/main/java/com/baeldung/generics/Rankable.java @@ -0,0 +1,5 @@ +package com.baeldung.generics; + +public interface Rankable { + public int getRank(); +} diff --git a/core-java-lang-syntax/src/test/java/com/baeldung/generics/GenericConstructorUnitTest.java b/core-java-lang-syntax/src/test/java/com/baeldung/generics/GenericConstructorUnitTest.java new file mode 100644 index 0000000000..60907bbfd3 --- /dev/null +++ b/core-java-lang-syntax/src/test/java/com/baeldung/generics/GenericConstructorUnitTest.java @@ -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 entry = new GenericEntry(1); + + assertNull(entry.getData()); + assertEquals(1, entry.getRank()); + } + + @Test + public void givenGenericConstructor_whenCreateGenericEntry_thenOK() { + GenericEntry entry = new GenericEntry("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 entry = new GenericEntry(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 optional = Optional.of(product); + GenericEntry entry = new GenericEntry(optional); + + assertEquals(product, entry.getData()); + assertEquals(30, entry.getRank()); + } + + @Test + public void givenGenericConstructor_whenCreateGenericEntryWithTwoTypes_thenOK() { + MapEntry entry = new MapEntry("sample", 1); + + assertEquals("sample", entry.getKey()); + assertEquals(1, entry.getValue() + .intValue()); + } +}