简体   繁体   English

如何使用 Mapstruct 将 Enum 属性映射到另一种类型的 Enum - Java

[英]How to map Enum attribute into an Enum of another type with Mapstruct - Java

I have two similar enums used respectively in Domain and Entity class.我在 Domain 和 Entity 类中分别使用了两个类似的枚举。 Here is an Example :这是一个示例:

Classes课程

public class Entity {
   String name;
   State state;
}
public class Domain {
   String name;
   Status status;
}
public enum Status {
   PENDING,
   CANCELLED
}
public enum State {
   PENDING,
   CANCELLED
}

Mappers映射器

public interface StatusMapper {
   public Status statusToState(Status status);
}
public interface EntityMapper {
   public Domain EntityToDomain(Entity entity);
}

What do I have to do if I want to know how to map Entity to Domain so that :如果我想知道如何将实体映射到域,我该怎么做:

Entity { name: "Name", state: "PENDING" } 

becomes and maps into :变成并映射到:

Domain { name: "Name", status: "PENDING" }

You can see here that State and Status are the same.您可以在此处看到 State 和 Status 是相同的。 I have tested it but status becomes null when mapping Entity to Domain.我已经对其进行了测试,但是将实体映射到域时状态变为空。

Thanks for your answers.感谢您的回答。

You can get rid of StatusMapper and instead of EntityMapper you can using the following interface.您可以摆脱StatusMapper而不是EntityMapper您可以使用以下接口。

import com.package.domain.Domain;
import com.package.entity.Entity;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.springframework.core.convert.converter.Converter;

@Mapper(componentModel = "spring")
public interface EntityToDomainConverter extends Converter<Entity, Domain> {

    @Mapping(source = "state", target = "status")
    Domain convert(Entity source);
}

The generated class will look like生成的类看起来像

import com.package.domain.Domain;
import com.package.entity.Entity;
import com.package.State;
import com.package.Status;
import javax.annotation.Generated;
import org.springframework.stereotype.Component;

@Generated(
    value = "org.mapstruct.ap.MappingProcessor",
    date = "2022-06-30TXX:XX:XX+YYYY",
    comments = "version: 1.4.2.Final, compiler: javac, environment: Java 16.0.2 (Homebrew)"
)
@Component
public class EntityToDomainConverterImpl implements EntityToDomainConverter {

    @Override
    public Domain convert(Entity source) {
        if ( source == null ) {
            return null;
        }

        Domain domain = new Domain();

        domain.setStatus( stateToStatus( source.getState() ) );
        domain.setName( source.getName() );

        return domain;
    }

    protected Status stateToStatus(State state) {
        if ( state == null ) {
            return null;
        }

        Status status;

        switch ( state ) {
            case PENDING: status = Status.PENDING;
            break;
            case CANCELLED: status = Status.CANCELLED;
            break;
            default: throw new IllegalArgumentException( "Unexpected enum constant: " + state );
        }

        return status;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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