What is the recommended practice for taking a Java enum that has say 1300 values and putting it into organized groups? I know you can't just extends the enum group, so are there other good alternatives?
4 Answers
I would use an interface which these instances all share, then you can use any number of enums or load them from another data source such as a file or database.

- 525,659
- 79
- 751
- 1,130
-
I wholeheartedly agree. Load them from a file or the database. However, if there are method implementations within a few of the enums, consider creating an interface, and a separate implementation for each enum that has a method defined, and then one default implementation that receives the values from the database or file. If there are no methods defined, then simply create a POJO. – Saish Dec 29 '11 at 20:38
1300 values? Good god, who thought that was a good idea? Did it not occur to someone after 100 that it was getting too big?
There's no good way around it that I can see. Get a shovel and start combining them into more cohesive sub-enums.
Here's a question: How are they used? If they are part of your application configuration, I'd recommend moving them to a database rather than keeping them in code. They'll be more flexible that way.

- 305,152
- 44
- 369
- 561
-
2They are actually in the database already. These enums are the primary keys for a list of error codes. Example, the 'NO_DATA_FOUND' enum key points to 'Sorry, but we are unable to locate your request' in the database. – yd39 Jan 03 '12 at 16:55
you can break them up like:
import java.util.*;
interface Children {
Set<Enum<?>> children();
}
enum Dog implements Children {
myDog,yourDog;
Dog() {
this(null);
}
Dog(Set<Enum<?>> children) {
this.children=children;
}
@Override public Set<Enum<?>> children() {
return children!=null?Collections.unmodifiableSet(children):null;
}
Set<Enum<?>> children;
}
enum Animal implements Children {
cat,dog(EnumSet.allOf(Dog.class));
Animal() {
this(null);
}
Animal(Set children) {
this.children=children;
}
@Override public Set<Enum<?>> children() {
return children!=null?Collections.unmodifiableSet(children):null;
}
Set<Enum<?>> children;
}
enum Thing implements Children {
animal(EnumSet.allOf(Animal.class)),vegetable,mineral;
Thing() {
this(null);
}
Thing(Set children) {
this.children=children;
}
@Override public Set<Enum<?>> children() {
return children!=null?Collections.unmodifiableSet(children):null;
}
Set<Enum<?>> children;
}
public class So8671088 {
static void visit(Class<?> clazz) {
Object[] enumConstants = clazz.getEnumConstants();
if (enumConstants[0] instanceof Children) for (Object o : enumConstants)
visit((Children) o, clazz.getName());
}
static void visit(Children children, String prefix) {
if (children instanceof Enum) {
System.out.println(prefix + ' ' + children);
if (children.children() != null) for (Object o : children.children())
visit((Children) o, prefix + ' ' + children);
} else
System.out.println("other " + children.getClass());
}
public static void main(String[] args) {
visit(Thing.animal," ");
visit(Thing.class);
}
}

- 9,841
- 8
- 50
- 90
From an oops perspective it would be a good idea to break them into cohesive units. So if I were you, I would evaluate which of these enums have a cohesion towards a responsibility.

- 3,938
- 24
- 37