lundi 26 juillet 2021

How to optimize my code that uses different packages with the same class names?

I am working on a converter that converts one data model to another. Specifically, it is a Hafas model where the API sends me a Hafas response in XML format. It doesn't matter which model it is, but that it uses different pojo classes for different versions of this model.

I use two versions of Hafas (for now) and both versions have separate schemes, so the pojo classes that I generate are constructed differently. The problem is that both versions are different but still use the same or similar class names. The algorithm that reads them and translates them into another data model is almost the same. Over 900 lines of code with perhaps a few modified lines of code.

Examples

1st Version:

public void convertAndInsertHafasLegsInsideTriasTrip(TripStructure myTrip, LegList hafasLegs) {
        for (Leg hafasLeg :
                hafasLegs.getLeg()
        ) {
            TripLegStructure myTripLeg = convertToTriasTripLeg(hafasLeg);

            if (myTripLeg != null)
                myTrip.getTripLeg().add(myTripLeg);
            else
                throw new NullPointerException("ERROR: HAFAS TripResult conversion ended up with NULL object");
        }
    }

2nd version:

public void convertAndInsertHafasLegsInsideTriasTrip(TripStructure myTrip, LegList hafasLegs) {
        for (Leg hafasLeg :
                hafasLegs.getLeg()
        ) {
            TripLegStructure myTripLeg = convertToTriasTripLeg(hafasLeg);

            if (myTripLeg != null)
                myTrip.getTripLeg().add(myTripLeg);
            else
                throw new NullPointerException("ERROR: HAFAS TripResult conversion ended up with NULL object");
        }
    }

LegList class and Leg class in the first and second versions of the converter is different because both versions use different packages with pojo classes. So LegListand Leg come in different packages.

My problem is that I have dozens of methods that are exactly the same and maybe a few that are different because the class names are different in these two packages.

Another problem is if for example I have to implement a converter I have another version that is also similar to these, again I have to duplicate thousands of lines of code.

Aucun commentaire:

Enregistrer un commentaire