-3

I'm trying to map from a String value to an enum that does not match that string value directly (i.e. String value is "I" and I want to map that to enum value Industry.CREATOR) I'm not seeing anything in mapstruct to do something like this.

I'd like it generate something like the following

switch (entityInd) {
            case "I":
                return Industry.CREATOR;
            case "E":
                return Industry.CONSUMER;
            default:
                return null;
        }
Alex Trautman
  • 46
  • 1
  • 8

1 Answers1

1

Have the Industry enum enriched with a field for the code and add a static method which iterates through the enum values and returns the enum value having the given code

enum Industry {

    CREATOR("I")/*, here comes more values of the enum*/;

    private String code;

    Industry(String code) {
        this.code = code;
    }

    public static Industry forCode(String code) {

        return Arrays.stream(Industry.values())
                     .filter(industry -> industry.code.equals(code))
                     .findAny()
                     .orElse(null);
    }
}

For usage a Mapper should to be defined and in the mapper the 'Industry#forCode` method should be called

Industry industry = Industry.forCode("I");

Section 6 of Quick Guide to MapStruct article details how a Mapper could be used

  • The Industry enum will have nothing to do with this value other than the one time I need to map to it, so adding it to the enum is troublesome and might be extraneous data for other people using this domain object. – Alex Trautman Mar 29 '19 at 17:40
  • The static method doesn't need to be set on the enum. You can have that method in your mapper or other util class – Filip Mar 29 '19 at 17:59
  • @Filip But then that rids me of the need of an annotation to do the same thing, I was hoping for something similar to ValueMapper that takes a source string and a target enum value to return. – Alex Trautman Mar 29 '19 at 18:09
  • @AlexTrautman the string needs to be associated with the enum value, there are several solution to implement this mapping, the two straight forward for me are (1) use the `code` field as in the example from the answer (2) have a variable of type `Map` in the `Mapper` class or in other class from which is accessible to the `Mapper` –  Mar 29 '19 at 18:36
  • @Filip you are right the static method could be implemented in the mapper or in an util class. I chose to add it in the enum as with the details at hand in the enum I think is easier to be found by other developers using the enum –  Mar 29 '19 at 19:01
  • 1
    @AlexTrautman yes you would not need to write an annotation for that, but what is the difference? It is even easier and more concise if you write code like Valentin proposed. – Filip Mar 29 '19 at 19:38
  • @ValentinCarnu my comment was mostly for Alex. In case he doesn't want to add a static method on the enum, you still can use the same logic that you did but in a different place. I personally would also prefer to have that static on my enum – Filip Mar 29 '19 at 19:40
  • @Filip I've added the details about implementing the method in the enum to shed some light on how I chose where to place the method –  Mar 29 '19 at 19:46