Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert null to Enum.NULL

Tags:

java

enums

I have an Enum like this:

public enum Type{
    CSV, EXCEL, PDF, URL, NULL
}

now I read a String from an xml-file and use Type.valueOf(string); to parse the value to the enum. if this part does not exist in the xml-file, the string is null and not "null". Is there a way to convert null to "null" or should change the return null; to return "null";? or should null not even be a part of the enum?

like image 275
XtremeBaumer Avatar asked May 29 '17 11:05

XtremeBaumer


People also ask

Can I pass null for an enum?

Enum types cannot be nullable.

Can enum parse return null?

If the parsed value is not equivalent to one of the Enum values, you could return a null and then check for null every where in the code before using this Enum.

Can an enum be null C++?

Yes, the only downside there is that the value for null would have to be the default value for value types — otherwise, values that are not explicitly initialized would have strange, non-default values rather than null.

Can enum be nullable laravel?

Enums can't be nullable.


2 Answers

What you should do is use something else than NULL and parse that case differently:

public enum Type {
  CSV, EXCEL, PDF, URL, NONE;
  public static Type from(String text) {
    if (text == null) {
      return NONE;
    } else {
      return valueOf(text.toUpperCase());
    }
  }
}

Or better yet, use optionals:

public enum Type {
  CSV, EXCEL, PDF, URL; // Note the absence of NULL/NONE/WHATEVER
  public static Optional<Type> from(String text) {
    return Optional.ofNullable(text)
      .map(String::toUpperCase)
      .map(Type::valueOf);
  }
}
like image 181
Olivier Grégoire Avatar answered Sep 30 '22 09:09

Olivier Grégoire


If you call Type.valueOf(null) you'll get a NullPointerException. So I would suggest two solutions:

  1. write a utility function which includes a nullcheck:

    public static Type getType(String name) {
        if (name == null)
            return Type.NULL;
        else
            return Type.valueOf(name.toUpperCase());
    }
    
  2. create a map from the name to the field (except for NULL) and add a getter to your enum:

    public enum Type {
        CSV, EXCEL, PDF, URL, NULL;
    
        private static final Map<String, Type> TYPE_BY_NAME = new HashMap<>();
        static {
            TYPE_BY_NAME.put(CSV.name(), CSV);
            TYPE_BY_NAME.put(EXCEL.name(), EXCEL);
            TYPE_BY_NAME.put(PDF.name(), PDF);
            TYPE_BY_NAME.put(URL.name(), URL);
            TYPE_BY_NAME.put(null, NULL);
        }
    
        public static Type getType(String name) {
            String s = name == null ? null : name.toUpperCase();
            return TYPE_BY_NAME.get(s);
        }
    

The second solution needs a bit more typing but you don't get an exception if the name does not exist. If you're sure that only the fields of your enum are given I would prefer the first solution.

like image 41
Stefan Warminski Avatar answered Sep 30 '22 10:09

Stefan Warminski