Search code examples
javaspringpostgresqljava-8

How do I convert java.sql.Array to List<MyCustomEnum>?


I have rs.getArray("lang"); which is java.sql.Array and the lang field is character varying[]. I want to convert it to List<MyEnumLanguage>. As an example I have {fr_FR,en_US} and I have used the following code to convert that my IDE didn't show any errors

List<MyEnumLanguage> myEnumLanguageList = (List<MyEnumLanguage>) rs.getArray("lang");

but it throws an exception org.postgresql.jdbc.PgArray cannot be cast to java.util.List.

My MyEnumLanguage is like:

public enum MyEnumLanguage {
    en_US {
        public String getCode() { return "en_US" }
    },
    de_DE {
        public String getCode() { return "de_DE" }
    };

    private MyEnumLanguage() {
    }
}

Solution

  • You cannot cast an Array to List. Your IDE does not show any error because casting happens at runtime.

    You have to first convert your PgArray to a normal Java array. This can be done using PgArray.getArray() method, and then cast the returned object to an array of the pgArray's containing type. Once you have a normal Java array, you can use the Arrays.asList(array) method, which returns a List containing all the elements of the array.

    Note that if you want to map the elements of the array to another type, you could easily do that with streams. Example:

    List<MyEnumLanguage> myEnumLanguageList = Arrays.asList(rs.getArray("lang"))
                    .stream()
                    .map(arrayElement -> convertToMyEnumLanguage(arrayElement))
                    .collect(Collectors.toList());
    

    where convertToMyEnumLanguage() takes an element of your array and returns the corresponding MyEnumLanguage.

    Take a look at this Stackify post: A Guide to Java Streams in Java 8: In-Depth Tutorial With Examples

    Detailed example

    First of all you should define your enum like this:

    public enum MyEnumLanguage {
    
        en_US("en_US"),
        de_DE("de_DE");
    
        private final String code;
    
        private MyEnumLanguage(String code) {
            this.code = code;
        }
    
        public String getCode() {
            return code;
        }
    
        public static MyEnumLanguage getEnumByCode(String code) {
            if(code == null || code.isEmpty()) {
                return null;
            }
            for(MyEnumLanguage e : values()) {
                if(e.getCode().equals(code)) {
                    return e;
                }
            }
    
            return null;
        }
    }
    

    Then to map your PgArray to a List:

    Array pgArray = rs.getArray("lang");
    String[] langJavaArray = (String[]) pgArray.getArray(); //this returns an Object, so we cast it to a String array
    
    List<MyEnumLanguage> myEnumLanguageList =
            Arrays.stream(langJavaArray)
            .map(MyEnumLanguage::getEnumByCode)
            .collect(Collectors.toList())
    ;
    

    Please note that the mapping function does not check for nulls. So if a wrong code is passed, your list will contain nulls. If this is not the desired result, you have to perform the appropriate checks and handle null cases in your map function.