Creating an EnumMap with a Common Enum Type

If I have a bunch of classes that all contain Enum and EnumMap, and I want to create a superclass for these classes.

public interface ColorEnum { } class ColorMarbles extends Toy { enum MARBLE implements ColorEnum { BLUE, GREEN } EnumMap<MARBLE, String> names = new EnumMap<MARBLE, String>(MARBLE.class); //stuff // fields public void populate(ArrayList<String> designer) { int i = 0; for(MARBLE marble : MARBLE.values()) { marble.name = designer.get(i); i++; } } } class ColorBalloons extends Toy { enum BALLOON implements ColorEnum { YELLOW, RED } EnumMap<BALLOON, String> names = new EnumMap<BALLOON, String>(BALLOON.class); //stuff // fields public void populate(ArrayList<String> designer) { int i = 0; for(BALLOON balloon : BALLOON.values()) { balloon.name = designer.get(i); i++; } } } 

How to create a superclass to create a generic EnumMap that contains an enumeration of type ColorEnum like this?

 public abstract class Toy { EnumMap<ColorEnum, String> names; } 

eidt: I understand that I was too vague with my example. Dogs are probably a bad example. I exchange it for something, hoping more clear.

I have a bunch of classes with methods such as padding that populates EnumMap. Names are in a predefined order. Instead of defining the fill in each class, I hope that I can bring it to the Toy superclass so that I do not have to copy into each new type of type Toy.

Hope this will explain more of what I'm looking for.

+4
source share
2 answers

I have the feeling that your design is getting too complicated.

With transfers

If you do not require class inheritance, you can work with enumerations directly, as with top-level classes.

 public interface Animal {} public enum Dog implements Animal { HUSKY("Husky"), LAB("Labrador"); private final String name; Dog(String name) { this.name = name; } public String getName() { return name; } } 

Enums can declare fields, methods, and implement interfaces like any other Java class. Their only limitation is that their direct superclass is always java.lang.Enum , and they cannot be extended.

However, each enumerator constant can have its own set of unique data passed to its constructor. It is even possible that each of the constants can override the general method of this enumeration with its unique implementation.

A good tutorial explaining more about the full power of listings: http://javarevisited.blogspot.cz/2011/08/enum-in-java-example-tutorial.html


No transfers

If you need the actual inheritance of the class to share some common methods (for example, from the Animal superclass), I still refuse the approach to the map and rather try something more OOP-oriented:

 public class Animal { } public abstract class Dog extends Animal { public abstract String getName(); public static class Husky extends Dog { @Override public String getName() { return "husky"; } } public static class Lab extends Dog { @Override public String getName() { return "labrador"; } } } 
+5
source

One of the mechanisms I used for something like this is to extend the base base class, which has a common parameter that allows you to pass Enum details to it.

This example defines the base class Table for database tables:

 public class Table<Column extends Enum<? extends Column>> { // Name of the table. protected final String tableName; // All of the columns in the table. This is actually an EnumSet so very efficient. protected final Set<Column> columns; /** * The base interface for all Column enums. */ public interface Columns { // What type does it have in the database? public Type getType(); } // Small list of database types. public enum Type { String, Number, Date; } public Table(String tableName, Set<Column> columns) { this.tableName = tableName; this.columns = columns; } } 

Now you can subclass this:

 public class VersionTable extends Table<VersionTable.Column> { public enum Column implements Table.Columns { Version(Table.Type.String), ReleaseDate(Table.Type.Date); // Sadly all of this must be in ALL of your enums but most of the work can be pushed up to `Table` final Table.Type type; Column(Table.Type type) { this.type = type; } @Override public Type getType() { return type; } } public VersionTable() { super("Versions", EnumSet.allOf(Column.class)); } } 

and use the functionality in the parent class that processes your enumeration.

Note that I am EnumSet to the Table constructor. I am sure you can change this to suit your EnumMap requirements if you decide that EnumSet not enough.

+1
source

All Articles