0

just wondering if anyone can help me out with something.

Basically I have two objects from different places for example two different car classes. I want to map the properties of class carA to the properties of class carB using a mapper I have created. This is simple enough in most scenarios as can be seen below.

public carB carMapper(carA car){

    carB carb = new carB();

    carb.weight = car.weight
    carb.size = car.size


}

However, there are some scenarios where class CarA stores the corresponding values in a slightly different format to that of CarB, ie, for a model of car which is a Ford carA stores this as Frd but carB stores it as Ford. Is there an easy way (perhaps using enums or something) that I can map these correctly? I know I could do

 if (carA.model = frd)
   then carB.model = Ford 

etc but I dont want to do this for every scenario.... I know the possible values of both carA and carB, could I use an enum in some way to help me out here?

Thanks.

1
  • What is the type of model variable in CarA and CarB now? Are they already enum type or String? If they are already enum type, why not use the same enum for both CarA or CarB? Commented Apr 12, 2016 at 14:59

2 Answers 2

1

It appears that you are looking for something like a mapping constructor. There is already a well-known concept called a copy constructor which helps you create a copy of an object of a given class. In your case, you want to create an instance of some class given an instance of another class.

I do not think enums can be (or should be) used for this purpose, mainly because they are not designed for this purpose.

It's not very clear if such a construct is immensely useful (and generic). For instance, would you want to create a Turtle given a Car? There isn't much common between them, so providing such a facility seems to be of little value. You are taking an example of CarA and CarB as classes, but since they are two different classes, they are to be assumed entirely different things, any commonality should be assumed purely coincidental.

If you do want to externalize this operation, then you may consider using some kind of serialization for your mapping in a format like JSON or XML and then one can imagine generic processing of that format to create objects of classes involved:

<map from="CarA" to="CarB">
  <property>model</property>
  <override>Ford</override>
</map>

In this case, you create an instance of CarB and reflectively set the fields on it based on the mappings. You can make an assumption that all the other fields are copied verbatim. Of course, you will need to flush out all the details of your specification for it to be generically useful.

Otherwise, I don't see anything wrong with the if-else construct you have alluded to, since its usefulness is decidedly limited.

Sign up to request clarification or add additional context in comments.

Comments

0

Thanks for the feedback guys, appreciate the responses, sorry I haven't had a chance to reply sooner. I have found a solution to my problem using an enum. It suits exactly what I was looking for, maybe for some reason it isnt ideal but if anyone can think of some reason I shouldn't be using an enum in this way I am more than happy to hear it. Anyway, my solution is below and allows me to add multiple values etc:

public enum ModelMapper
{
Ford("Ford", "frd"),
Renault("Renault", "rnlt");

private final String carAValue;
private final String carBValue;

ModelMapper(String carA_value, String carB_value) 
{
    carAValue = carA_value; 
    carBValue = carB_value;       
}

public static String getCarAValue(String carB_value)
{
    for (ModelMapper m: ModelMapper.values()) {
        if (m.carBValue.equals(carB_value)) {
            return m.carAValue;
        }  
    }
    throw new IllegalArgumentException(carB_value);
}
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.