samedi 13 juin 2020

How can I call setters method on an DTO object iterating on a Map and using the current key to build the specific setter method? (reflection?)

I have the following problem in Java and I am not sure if exist a solution (maybe using reflection but I am absolutly not sure).

I have a method like this:

@Override
public int insertTrend(String trendTableName, Map<String, Object> jsonTrendInfo) {

    for (Map.Entry<String,Object> entry : jsonTrendInfo.entrySet())  
        System.out.println("Key = " + entry.getKey() + 
                         ", Value = " + entry.getValue());

    return 0;
}

as you can see I am iterating over a Map extracting the key and the related value.

Basically, at the moment, I have an output like this:

Key = Sensor0001, Value = 39.2
Key = Sensor0002, Value = 32.5
Key = Sensor0003, Value = 21.7
Key = Sensor0004, Value = 47.3
Key = Sensor0005, Value = 33.4
..............................
..............................
..............................

Basically the key will contains the name of a sensor and the value contains the value retrieved by this sensor.

These sensor are a lot (in the hundreds). In my code I have a DTO class containing these sensors as fields, the DTO class will contain something like:

private Double sensor001;
private Double sensor002;
private Double sensor003;
private Double sensor004;
private Double sensor005;
.........................
.........................
.........................

my DTO class will contain also the related getter and setters method.

For example:

public void setSensor001(Double sensor001) {
    sensor001 = sensor001;
}

I know that to initialize all the fileds of my DTO object I can use a parametrized constructor but I am asking if there is a way to automatically build and call the setter method of the field related to the current element in my Map.

For example:

Iterating on the map the first element have key=Sensor0001 and value=39.2

So the setSensor001() method will be call on my DTO object taking the value 39.2 as parameter.

In this way iterating on my Map I will fully initizialize my DTO object avoiding to write a huge constructor.

Can it be done in some way? In case it is possible is it a smart solution or there are some drawbacks using this approach? (maybe in perfromance)





Aucun commentaire:

Enregistrer un commentaire