HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaModerate

Enum with getting subset

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
withenumsubsetgetting

Problem

I just wrote this but I'm not sure if this is good practice. Can someone give me advice as to whether it's good or how I can do better or even it's bad code?

The point is that I need a sub List-array-set of the enum. The SubType.ELSE shall have another name in reality (I know that was a bad choice, but it's for pointing it out).

public enum ContentType {
    TITLE("$title$",SubType.MAIL),
    BODY("$body$",SubType.MAIL),
    MESSAGE("$message$",SubType.MAIL),
    EVENTS("$events$",SubType.ELSE);

    private final String replaceWord;
    private final SubType subType;

    private ContentType(String replaceWord, SubType subType ) {
        this.replaceWord = replaceWord;
        this.subType = subType;
    }

    public String getReplaceWord() {
        return replaceWord;
    }

    private SubType getSubType() {
        return subType;
    }

    public static List values(SubType subType) {
        List subList = new ArrayList();
        for (ContentType type : ContentType.values()) {
            if (type.getSubType() == subType) {
                subList.add(type);
            }
        }
        return subList;
    }
}

enum SubType {
    MAIL,
    ELSE;
}

Solution

I suggest two changes to values(SubType):

  • Return a Set instead of a List.



  • Prepare all the possible results at class-loading time, so that values(SubType) could be accomplished by a simple lookup. The code is slightly more complicated, but the runtime performance should be better.



private static final Map> BY_SUBTYPE;
static {
    BY_SUBTYPE = new HashMap>();
    for (SubType subType : SubType.values()) {
        BY_SUBTYPE.put(subType, new TreeSet());
    }
    for (ContentType type : ContentType.values()) {
        BY_SUBTYPE.get(type.getSubType()).add(type);
    }
    for (SubType subType : BY_SUBTYPE.keySet()) {
        // Make Set values immutable
        BY_SUBTYPE.put(subType, Collections.unmodifiableSet(BY_SUBTYPE.get(subType)));
    }
}

public static Set values(SubType subType) {
    // Returns an unmodifiable view of a SortedSet
    return BY_SUBTYPE.get(subType);
}

Code Snippets

private static final Map<SubType, Set<ContentType>> BY_SUBTYPE;
static {
    BY_SUBTYPE = new HashMap<SubType, Set<ContentType>>();
    for (SubType subType : SubType.values()) {
        BY_SUBTYPE.put(subType, new TreeSet<ContentType>());
    }
    for (ContentType type : ContentType.values()) {
        BY_SUBTYPE.get(type.getSubType()).add(type);
    }
    for (SubType subType : BY_SUBTYPE.keySet()) {
        // Make Set<ContentType> values immutable
        BY_SUBTYPE.put(subType, Collections.unmodifiableSet(BY_SUBTYPE.get(subType)));
    }
}

public static Set<ContentType> values(SubType subType) {
    // Returns an unmodifiable view of a SortedSet
    return BY_SUBTYPE.get(subType);
}

Context

StackExchange Code Review Q#47437, answer score: 11

Revisions (0)

No revisions yet.