vendredi 4 décembre 2020

Is there a way another way to change an object into a wrapper class

I'm curious if i'm missing a pattern which help me with the following case:

This piece of code eventually writes data from a database to an excelsheet using Apache POI

                    if (o instanceof Double) {
                        cell.setCellValue((Double) o);
                    } else if (o instanceof Integer) {
                        cell.setCellValue((Integer) o);
                    } else if (o instanceof Long) {
                        cell.setCellValue((Long) o);
                    } else if (o instanceof BigDecimal) {
                        cell.setCellValue(((BigDecimal) o).doubleValue());
                    } else {
                        cell.setCellValue(o.toString());
                    }

This works like it is supposed to, but i'm looking for another solution of determening the type of the object. Now I read about using a interface here : Alternative to instanceof and if-else statements for iterating over/de-referencing a list of mixed object types

But that means I have to implement the interface in the Wrapper classes which is hard / not possible (?)

Is there another solution in this case?

Thanks in advance

Aucun commentaire:

Enregistrer un commentaire