All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.modelmapper.inheritance.InheritanceExample1 Maven / Gradle / Ivy

package org.modelmapper.inheritance;

import org.modelmapper.ModelMapper;
import org.modelmapper.Provider;
import org.modelmapper.TypeMap;

import java.util.Arrays;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;

public class InheritanceExample1 {

  public static void main(String... args) {
    org.modelmapper.inheritance.C c = new org.modelmapper.inheritance.C(Arrays.asList(new BaseSrcA(), new BaseSrcB()));


    ModelMapper modelMapper = new ModelMapper();

    TypeMap typeMap = modelMapper.createTypeMap(BaseSrc.class, BaseDest.class)
            .include(BaseSrcA.class, BaseDestA.class)
            .include(BaseSrcB.class, BaseDestB.class);

    modelMapper.typeMap(BaseSrcA.class, BaseDest.class).setProvider(new Provider() {
      public BaseDest get(ProvisionRequest request) {
        return new BaseDestA();
      }
    });
    modelMapper.typeMap(BaseSrcB.class, BaseDest.class).setProvider(new Provider() {
      public BaseDest get(ProvisionRequest request) {
        return new BaseDestB();
      }
    });

    CcDTO ccDTO = modelMapper.map(c, CcDTO.class);

    assertEquals(2, ccDTO.getBases().size());
    assertTrue(ccDTO.getBases().get(0) instanceof BaseDestA);
    assertTrue(ccDTO.getBases().get(1) instanceof BaseDestB);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy