Issue 17848

VocabularyUtils.lookupEnum should probably never return nulls

17848
Reporter: cgendreau
Assignee: cgendreau
Type: Improvement
Summary: VocabularyUtils.lookupEnum should probably never return nulls
Priority: Minor
Status: Open
Created: 2015-09-25 13:58:10.655
Updated: 2016-07-11 15:58:04.459
        
Description: Currently, if we call DatasetType.fromString(null) or DatasetType.fromString("") it returns a null value back. If we use a non-existing value (e.g. DatasetType.fromString("xyz)) an IllegalArgumentException is thrown.

Considering Java Enum class will throw a NPE for nulls and an IAE for other incorrect values, we should probably implement the same behaviour to handle null and empty values.

The main advantage is to ensure we cannot get an invalid value (null) out of the fromString(String) method. The disadvantage is that exceptions need to be handled and since they are RuntimeException, it may be error prone.]]>
    


Author: cgendreau
Comment: This will require a LOT of refactoring but the best solution is probably to return an Optional<T>.
Created: 2015-10-19 15:26:13.463
Updated: 2015-10-19 15:26:13.463


Author: cgendreau
Created: 2015-12-08 14:36:36.145
Updated: 2015-12-08 14:38:10.161
        
Alternative method VocabularyUtils.lookup is now provided:
https://github.com/gbif/gbif-api/commit/8de2316a2bf2bcfcc02baa16d692bb0e05f3cf1e
    


Author: cgendreau
Comment: To ensure it will work properly everywhere, this method needs to use Java 8 Optional instead of the Guava implementation.
Created: 2016-07-11 15:58:04.459
Updated: 2016-07-11 15:58:04.459