This discussion is archived
2 Replies Latest reply: Aug 10, 2010 12:25 PM by 843804 RSS

java.util.EnumSet.noneOf()

843804 Newbie
Currently Being Moderated
I'm struggling with this method in that I can't see where it would be used. I've just started looking at 5.0 so maybe when used with generics somehow it's more obvious. All the other EnumSet methods make sense. Has anyone used noneOf() in anger? If so a code fragment would be greatly appreciated.
  • 1. Re: java.util.EnumSet.noneOf()
    843804 Newbie
    Currently Being Moderated
    public static <E extends Enum<E>> EnumSet<E> noneOf(Class<E> elementType)
    /*
        Creates an empty enum set with the specified element type.
    */
  • 2. Re: java.util.EnumSet.noneOf()
    843804 Newbie
    Currently Being Moderated
    Well, the javaDoc reply was probably not useful to you. I know it wasn't useful to me when I was trying to figure out how to use EnumSet.noneOf().

    As an example, imagine a class that generates formatted output of some type. In my case, I was generating XML based upon some data. However, I needed the format of the output to vary slightly based upon other requirements. For instance, since this was an enhancement, I still needed to generate the older "legacy" format output if required. Long story short, I used an EnumSet of flags that affected the generated output.

    Specifying no flags should result in the "default" output. But it was difficult to specify the empty set in an EnumSet (as it is abstract). However, EnumSet.noneOf() is a convenient way to specify the empty set of enums.

    For instance:

    public class ImportExportXml {

    public static enum Style {
    LEGACY_SCHEDULES, // Print Schedules in legacy format.
    NO_ENCRYPTION, // Do not encrypt passwords in output (not recommended).
    ...
    }

    private final EnumSet<Style> flags;

    public ImportExportXml() {
    this(EnumSet.noneOf(Style.class));
    }

    public ImportExportXml(EnumSet<Style> flags) {
    this.flags = flags;
    }


    The key here is to specify the Class of the enums in the call: EnumSet.noneOf(Style.class);

    Yes, I perhaps could have achieved the same effect using style sheets and/or XML transforms,
    but this question was about use of EnumSet.noneOf().

    Edited by: brettmjohnson on Aug 10, 2010 12:24 PM

    Edited by: brettmjohnson on Aug 10, 2010 12:25 PM