简体   繁体   中英

From string to enum using mapstruct

I want to convert String to enum using mapstruct

enum TestEnum {
   NO("no");
   String code;

   TestEnum(String code) {
     this.code = code
   }

   public String getCode() {
    return code;
   }
}

I have a code that I've got from service and I want to convert this code to Enum how to do it with easier way by mapstruct

Here is a solution with an abstract mapper, but if you want you can convert it with a default methode or a class

@Mapper
public abstract class TestMapper {

    abstract Source toSource(Target target);
    abstract Target totarget(Source source);

    String toString(TestEnum test){
        return test.getCode();
    }
    TestEnum toEnum(String code){
        for (TestEnum testEnum : TestEnum.values()) {
            if(testEnum.equals(code)){
                return testEnum;
            }
        }
        return null;
    }
}

public class Source {    
    String value;    
    public String getValue() {
        return value;
    }    
    public void setValue(String value) {
        this.value = value;
    }    
}

public class Target {
    TestEnum value;
    public TestEnum getValue() {
        return value;
    }
    public void setValue(TestEnum value) {
        this.value = value;
    }
}

The following code worked for me.

@Mappings({
        @Mapping(source = "genderDTO.name", target = "genderName")
})
GenderRecord dtoTogenderRecord(GenderDTO genderDTO);
  • "genderName" is the Enum
  • "genderDTO.name" is the String

The result was:

@Override
public GenderRecord dtoTogenderRecord(GenderDTO genderDTO) {
    if ( genderDTO == null ) {
        return null;
    }

    GenderRecord genderRecord = new GenderRecord();

    if ( genderDTO.getName() != null ) {
        genderRecord.setGenderName( Enum.valueOf( GenderType.class, genderDTO.getName() ) );
    }

    return genderRecord;
}

I also use the following at the interface level to ensure null checks:

@Mapper(nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS)

MapStruct only calls the "setProperty(PropertyType)" function, so simply use polymorphism in your setters

For DTO:

    public void setStatus(String status) {
        this.status = status;
    }

    public void setStatus(ProjectStatus status) {
        this.status = status.toString();
    }

For Entity:

    public void setStatus(ProjectStatus status) {
        this.status = status;
    }

    public void setStatus(String status) {

        switch (status) {
        case "PENDING_WITH_RM":
            this.status = ProjectStatus.PENDING;
            break;
        case "PENDING_WITH_OPS":
            this.status = ProjectStatus.PENDING;
            break;
        case "COMPLETED":
            this.status = ProjectStatus.COMPLETED;
            break;
        default:
            this.status = ProjectStatus.DRAFT;
        }
    }

In many cases there is a conversion between code and enum identifier possible:

enum TestEnum {
   NO("no"),
   _42("42"),
   MAYBE_YES("maybe yes");

   public final String code;

   public static TestEnum fromCode(String c) {
       c = c.toUpperCase().replace(' ', '_'); // Space to underscore.
       if (c.matches("\\d.*")) {
           c = "_" + c; // Starting digit prefixed by underscore.
       }
       return TestEnum.valueOf(c);
   }

   TestEnum(String code) {
     this.code = code
   }

   public String getCode() {
    return code;
   }
}

The field code no longer is necessary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM