Reputation: 98
I have three different types of enums (itemsA, itemsB and itemsC) and a Class "Product". So far, I'm passing one enum as a parameter for my product constructor, having three constructors:
static Product actualProduct;
public static void setActualProduct(itemsA currentProduct) {
actualProduct = new Product(currentProduct);
public static void setActualProduct(itemsB currentProduct) {
actualProduct = new Product(currentProduct);
public static void setActualProduct(itemsC currentProduct) {
actualProduct = new Product(currentProduct);
class Product
public class Product {
private String productId;
private String name;
public Product(itemsA item) {
this.productId = item.getId();
this.name = item.getProductName();
}
public Product(itemsB item) {
this.productId = item.getId();
this.name = item.getProductName();
this.category = item.getCategory();
}
public Product(itemsC item) {
this.productId = item.getId();
this.name = item.getProductName();
}
enum example: itemsA
public enum itemsA{
item1("00001", "itemName", BuySections.TOYS);
private final String id;
private final String productName;
private final BuySections section;
item1(String id, String name, BuySections section){
this.id = id;
this.productName = name;
this.section = section;
}
public String getProductName(){
return productName;
}
So, I think should be a way to implement this using some generics. Something like this:
public static void setActualProduct(Enum<?> itemType) {
actualProduct = new Product<?>(itemType);
Any help would be very apreciated.
Upvotes: 0
Views: 1616
Reputation: 7795
You can let your enums implement a common interface:
interface CommonInterface {
public String getId();
public String getProductName();
public BuySection getBuySection();
}
enum FstEnum implements CommonInterface {}
enum SndEnum implements CommonInterface {}
class Product<E extends Enum<E> & CommonInterface> {
E type;
Product(E type) {
this.type = type;
}
}
public static <E extends Enum<E> & CommonInterface> void setActualProduct(E itemType) {
actualProduct = new Product<E>(itemType);
}
A few remarks wrt. naming conventions: class names should be singular (BuySection
, not BuySections
), enums should begin with uppercase letters (as should all types, e.g. ItemsA
, not itemsA
)
Upvotes: 1
Reputation: 136
Yes, it is possible. you can make enum
implements a common interface
and create a constructor using the interface
. following is the example
interface GenericEnum{}
enum Item1 implements GenericEnum {
TOYS1;
}
enum Item2 implements GenericEnum {
TOYS2;
}
class Test {
public GenericEnum genericEnum;
public Test(GenericEnum genericEnum) {
this.genericEnum = genericEnum;
}
}
Upvotes: 2
Reputation: 1
I don't understand why you define different enums containing just single values, instead of defining one single enum containing different values. Something like this:
public enum Item {
item1("00001", "itemName", BuySections.TOYS),
item2("00002", "itemName2", BuySections.TOYS),
item3("00003", "itemName3", BuySections.TOYS);
private final String id;
private final String productName;
private final BuySections section;
Item(String id, String name, BuySections section){
this.id = id;
this.productName = name;
this.section = section;
}
public String getProductName(){
return productName;
}
}
Upvotes: 0