本文整理匯總了Java中org.modelmapper.ModelMapper類的典型用法代碼示例。如果您正苦於以下問題:Java ModelMapper類的具體用法?Java ModelMapper怎麽用?Java ModelMapper使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
ModelMapper類屬於org.modelmapper包,在下文中一共展示了ModelMapper類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: DtoAssembler
import org.modelmapper.ModelMapper; //導入依賴的package包/類
public DtoAssembler() {
this.mapper = new ModelMapper();
/*
TODO: This (more elegant) solution doesn't seem to be working:
this.mapper.addConverter((MappingContext<Rider, RiderDto> context) -> {
Rider rider = context.getSource();
RiderDto riderDto = context.getDestination();
riderDto.setStageWins(rider.getStageResults().size());
return riderDto;
});*/
this.mapper.addMappings(new PropertyMap<Rider, RiderDto>() {
@Override
protected void configure() {
map().setStageWins(source.getStageResults().size());
}
});
}
示例2: OrderMapper
import org.modelmapper.ModelMapper; //導入依賴的package包/類
public OrderMapper() {
mapper = new ModelMapper();
// use strict to prevent over eager matching (happens with ID fields)
mapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
// create PropertyMap<source,target> here and add to mapper
mapper.addMappings(new PropertyMap<OrderForm, OrderDto>() {
@Override
protected void configure() {
}
});
mapper.addMappings(new PropertyMap<OrderDto, OrderForm>() {
@Override
protected void configure() {
}
});
mapper.addMappings(new PropertyMap<OrderItemDto, OrderItem>() {
@Override
protected void configure() {
}
});
// need to add the parent link to jpa order items (as these don't exist in dto form)
mapper.getTypeMap(OrderDto.class, OrderForm.class)
.setPostConverter(context -> {
OrderForm target = context.getDestination();
log.info("post-converter fixing OrderItem parent links {}", target);
if (target.getItems() != null) {
target.getItems().stream().forEach((item) -> item.setOrder(target));
}
return target;
});
}
示例3: newInstance
import org.modelmapper.ModelMapper; //導入依賴的package包/類
public static ModelMapper newInstance(PropertyMap<?, ?> orderMap) {
final ModelMapper modelMapper = new ModelMapper();
modelMapper.getConfiguration()
.setFieldMatchingEnabled(true)
.setFieldAccessLevel(PRIVATE);
modelMapper.addMappings(orderMap);
return modelMapper;
}
示例4: main
import org.modelmapper.ModelMapper; //導入依賴的package包/類
public static void main(String[] args) throws Exception {
ModelMapper mapper = new ModelMapper();
Map<String, Object> result = new HashMap<>();
//[{jobName=Test_Anomaly_Task, jobId=1, workerId=1, taskType=MONITOR, id=1, taskInfo=clob2: '{"jobExecutionId":1,"monitorType":"UPDATE","expireDaysAgo":0}', lastModified=2016-08-24 17:25:53.258, version=0, taskStartTime=1470356753227, status=RUNNING, taskEndTime=1471220753227}]
result.put("jobName", "Test_Anomaly_Task");
result.put("jobId", 1L);
result.put("taskType", "MONITOR");
result.put("id", 1L);
result.put("taskInfo",
"clob2: '{\"jobExecutionId\":1,\"monitorType\":\"UPDATE\",\"expireDaysAgo\":0}'");
result.put("taskType", "MONITOR");
result.put("lastModified", "2016-08-24 17:25:53.258");
result.put("status", "RUNNING");
result.put("lastModified", "2016-08-24 17:25:53.258");
TaskDTO taskSpec1 = mapper.map(result, TaskDTO.class);
System.out.println(taskSpec1);
//INPUT 2
ObjectInputStream ois =
new ObjectInputStream(new FileInputStream(new File("/tmp/map.out.1472093046128")));
Map<String, Object> inputMap = (Map<String, Object>) ois.readObject();
TaskDTO taskSpec2 = mapper.map(inputMap, TaskDTO.class);
System.out.println(taskSpec2);
}
示例5: main
import org.modelmapper.ModelMapper; //導入依賴的package包/類
public static void main(final String args[]) {
final ModelMapper mapper = new ModelMapper();
mapper.addMappings(new MyPropertyMap().get());
final SourceBean incoming = new SourceBean();
incoming.setAge(35);
incoming.setFirstName("Antonio Maria");
incoming.setLastName("Sanchez");
final TargetBean result = mapper.map(incoming, TargetBean.class);
System.out.println("and the winner is: " + result);
}
示例6: testMap_when_no_LegalRep
import org.modelmapper.ModelMapper; //導入依賴的package包/類
@Test
public void testMap_when_no_LegalRep() {
// Arrange
Consent consent = new Consent();
modelMapper = new ModelMapper();
sut.setModelMapper(modelMapper);
Patient patient = new Patient();
patient.setFirstName("test");
consent.setPatient(patient);
// Act
ConsentDto consentDto = sut.map(consent);
// Assert
assertEquals(consent.getPatient().getFirstName(), consentDto
.getPatientDto().getFirstName());
assertNull(consentDto.getLegalRepresentative());
}
示例7: newModelMapper
import org.modelmapper.ModelMapper; //導入依賴的package包/類
private static ModelMapper newModelMapper() {
final ModelMapper mapper = new ModelMapper();
Configuration config =
mapper
.getConfiguration()
.copy()
.setFieldMatchingEnabled(true)
.setPropertyCondition(notDefault)
.setSourceNameTokenizer(NameTokenizers.UNDERSCORE);
createTypeMap(mapper, MainEncodingOptions.class, FFmpegOutputBuilder.class, config);
createTypeMap(mapper, AudioWrapper.class, FFmpegOutputBuilder.class, config);
createTypeMap(mapper, VideoWrapper.class, FFmpegOutputBuilder.class, config);
return mapper;
}
示例8: ensureMapper
import org.modelmapper.ModelMapper; //導入依賴的package包/類
private ModelMapper ensureMapper() {
if (mapper == null) {
synchronized (this) {
if (mapper == null) {
mapper = newMapper();
if (matchingStrategy != null) {
final Configuration configuration = mapper.getConfiguration();
try {
configuration.setMatchingStrategy(MatchingStrategy.class.cast(
MatchingStrategies.class.getDeclaredField(matchingStrategy.toUpperCase(Locale.ENGLISH)).get(null)));
} catch (final Exception e) {
try {
configuration.setMatchingStrategy(MatchingStrategy.class.cast(currentLoader().loadClass(matchingStrategy)));
} catch (final Exception e1) {
if (RuntimeException.class.isInstance(e)) {
throw RuntimeException.class.cast(e);
}
throw new IllegalStateException(e);
}
}
}
}
}
}
return mapper;
}
示例9: getProductAdaptor
import org.modelmapper.ModelMapper; //導入依賴的package包/類
@Bean
@Primary
ProductAdaptor getProductAdaptor() {
ProductAdaptor mock = mock(ProductAdaptor.class);
when(mock.getAllProducts())
.thenReturn(new ArrayList<>(mapProducts.values()));
when(mock.getProduct(anyString()))
.then((inv) -> mapProducts.get(inv.getArgumentAt(0, String.class)));
when(mock.upsertProduct(anyObject()))
.then((inv) -> {
ProductDto product = inv.getArgumentAt(0, ProductDto.class);
ProductDto newProduct = new ModelMapper().map(product, ProductDto.class);
if(newProduct.getId()==null)
newProduct.setId(UUID.randomUUID().toString());
mapProducts.put(newProduct.getId(),newProduct);
log.info("upsertProduct({}) : {}", product, newProduct);
return newProduct;
});
when(mock.deleteProduct(anyString()))
.then((inv) -> {
String productId = inv.getArgumentAt(0, String.class);
ProductDto removedProduct = mapProducts.remove(productId);
log.info("deleteProduct({}) : {}", productId, removedProduct);
return removedProduct;
});
return mock;
}
示例10: configureImpl
import org.modelmapper.ModelMapper; //導入依賴的package包/類
@SuppressWarnings("unchecked")
void configureImpl(ModelMapper mapper) {
Class<?>[] typeArguments = TypeResolver.resolveRawArguments(TypeMapConfigurer.class, getClass());
String typeMapName = getTypeMapName();
Configuration configuration = getConfiguration();
if (typeMapName == null && configuration == null) {
configure(mapper.createTypeMap((Class<S>) typeArguments[0], (Class<D>) typeArguments[1]));
} else if (typeMapName == null) {
configure(mapper.createTypeMap((Class<S>) typeArguments[0], (Class<D>) typeArguments[1], configuration));
} else if (configuration == null) {
configure(mapper.createTypeMap((Class<S>) typeArguments[0], (Class<D>) typeArguments[1], typeMapName));
} else {
configure(mapper.createTypeMap((Class<S>) typeArguments[0], (Class<D>) typeArguments[1], typeMapName, configuration));
}
}
示例11: getObject
import org.modelmapper.ModelMapper; //導入依賴的package包/類
/**
* {@inheritDoc}
*/
@Override
public ModelMapper getObject() throws Exception {
final ModelMapper modelMapper = new ModelMapper();
configure(modelMapper);
return modelMapper;
}
示例12: configure
import org.modelmapper.ModelMapper; //導入依賴的package包/類
/**
* Configures the model mapper instance be delegating toward registered {@link TypeMapConfigurer} instances.
*
* @param modelMapper the model mapper
*/
private void configure(ModelMapper modelMapper) {
if (mapperConfigurer != null) {
mapperConfigurer.configureImpl(modelMapper);
}
if (converters != null) {
converters.forEach(typeMapConfigurer -> typeMapConfigurer.configureImpl(modelMapper));
}
if (configurers != null) {
configurers.forEach(typeMapConfigurer -> typeMapConfigurer.configureImpl(modelMapper));
}
}
示例13: createWebServer
import org.modelmapper.ModelMapper; //導入依賴的package包/類
@Override
public WebServer createWebServer(final WebServer webServer, final String createdBy) {
try {
final JpaWebServer jpaWebServer = new JpaWebServer();
jpaWebServer.setName(webServer.getName());
jpaWebServer.setHost(webServer.getHost());
jpaWebServer.setPort(webServer.getPort());
jpaWebServer.setHttpsPort(webServer.getHttpsPort());
jpaWebServer.setStatusPath(webServer.getStatusPath().getPath());
jpaWebServer.setCreateBy(createdBy);
jpaWebServer.setState(webServer.getState());
jpaWebServer.setApacheHttpdMedia(webServer.getApacheHttpdMedia() == null ? null : new ModelMapper()
.map(webServer.getApacheHttpdMedia(), JpaMedia.class));
return webServerFrom(create(jpaWebServer));
} catch (final EntityExistsException eee) {
LOGGER.error("Error creating web server {}", webServer, eee);
throw new EntityExistsException("Web server with name already exists: " + webServer,
eee);
}
}
示例14: build
import org.modelmapper.ModelMapper; //導入依賴的package包/類
public WebServer build() {
List<JpaGroup> jpaGroups = webServer.getGroups();
List<Group> groups;
if (jpaGroups != null) {
groups = new ArrayList<>(webServer.getGroups().size());
for (JpaGroup gid : webServer.getGroups()) {
groups.add(new JpaGroupBuilder(gid).build());
}
} else {
groups = Collections.emptyList();
}
final Media apacheHttpdMedia = webServer.getApacheHttpdMedia() == null ? null :
new ModelMapper().map(webServer.getApacheHttpdMedia(), Media.class);
return new WebServer(new Identifier<WebServer>(webServer.getId()), groups, webServer.getName(), webServer.getHost(),
webServer.getPort(), webServer.getHttpsPort(), new Path(webServer.getStatusPath()), webServer.getState(),
apacheHttpdMedia );
}
示例15: testMediaEntityToMediaDto
import org.modelmapper.ModelMapper; //導入依賴的package包/類
@Test
public void testMediaEntityToMediaDto() throws Exception {
final JpaMedia jpaMedia = new JpaMedia();
jpaMedia.setId(1L);
jpaMedia.setName("JDK Lite");
jpaMedia.setType(MediaType.JDK);
jpaMedia.setLocalPath(Paths.get("localPath"));
jpaMedia.setRootDir(Paths.get("mediaDir"));
jpaMedia.setRemoteDir(Paths.get("remoteDir"));
final Media media = new ModelMapper().map(jpaMedia, Media.class);
assertEquals(1L, media.getId().longValue());
assertEquals("JDK Lite", media.getName());
assertEquals(MediaType.JDK, media.getType());
assertEquals("localPath", media.getLocalPath().toString());
assertEquals("mediaDir", media.getRootDir().toString());
assertEquals("remoteDir", media.getRemoteDir().toString());
}