vendredi 24 avril 2020

dynamically select a field to update in java

I have a class in Java 8 like:

class state
{
   LocalDateTime date1;
   LocalDateTime date2;
   LocalDateTime date3;
   LocalDateTime date4;
   LocalDateTime date5; 
}

Based on certain condition, i will have to select one of the date field to update it with utc time. I have to create a instance of this class by updating 1 field at a time rest of the fields will be empty.

I can do like below but I am wondering if there is any elegant way to achieve this. here status is an enum.

   switch case(status)
   case "1"
     new state(UTC.now, null,null, null, null);
   case "2"
     new state(null, UTC.now, null, null, null);

Aucun commentaire:

Enregistrer un commentaire