![JAR search and dependency download from the Maven repository](/logo.png)
net.projectmonkey.ModelMapper Maven / Gradle / Ivy
/*
* Copyright 2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.projectmonkey;
import java.util.Collection;
import net.projectmonkey.config.Configuration;
import net.projectmonkey.internal.Errors;
import net.projectmonkey.internal.InheritingConfiguration;
import net.projectmonkey.internal.MappingEngineImpl;
import net.projectmonkey.internal.util.Assert;
import net.projectmonkey.internal.util.TypeResolver;
import net.projectmonkey.internal.util.Types;
/**
* ModelMapper framework entry point. Performs object mapping and contains mapping related
* configuration.
*
*
* - To perform object mapping call {@link #map(Object, Class) map}.
* - To configure the mapping of one type to another call {@link #createTypeMap(Class, Class)
* createTypeMap}.
* - To add mappings for specific properties call {@link #addMappings(PropertyMap) addMappings}
* supplying a {@link PropertyMap}.
*
*
* @author Jonathan Halterman
*/
public class ModelMapper {
private final InheritingConfiguration config;
private final MappingEngineImpl engine;
/**
* Creates a new ModelMapper.
*/
public ModelMapper() {
config = new InheritingConfiguration();
engine = new MappingEngineImpl(config);
}
/**
* Registers the {@code converter} to use when mapping instances of types {@code S} to {@code D}.
* The {@code converter} will be set against TypeMap corresponding to the {@code converter}'s type
* arguments {@code S} and {@code D}.
*
* @param source type
* @param destination type
* @param converter to register
* @throws IllegalArgumentException if {@code converter} is null or if type arguments {@code S}
* and {@code D} are not declared for the {@code converter}
* @see TypeMap#setConverter(Converter)
*/
@SuppressWarnings("unchecked")
public void addConverter(Converter converter) {
Assert.notNull(converter, "converter");
Class>[] typeArguments = TypeResolver.resolveArguments(converter.getClass(), Converter.class);
Assert
.notNull("Must declare source type argument and destination type argument for converter");
config.typeMapStore.getOrCreate((Class) typeArguments[0], (Class) typeArguments[1],
null, converter, engine);
}
/**
* Adds mappings from the {@code propertyMap} into the TypeMap corresponding to source type
* {@code S} and destination type {@code D}. Explicit mappings defined in the {@code propertyMap}
* will override any implicit mappings for the same properties.
*
* @param source type
* @param destination type
* @param propertyMap from which mappings should be loaded
* @return TypeMap corresponding to the {@code propertyMap}
* @throws ConfigurationException if an error occurred while reading mappings from the
* {@code propertyMap}
* @throws IllegalArgumentException if {@code propertyMap} is null
* @throws ConfigurationException if a configuration error occurs while adding mappings for the
* {@code propertyMap}
*/
public TypeMap addMappings(PropertyMap propertyMap) {
Assert.notNull(propertyMap, "propertyMap");
return config.typeMapStore.getOrCreate(propertyMap.sourceType, propertyMap.destinationType,
propertyMap, null, engine);
}
/**
* Creates a TypeMap for the {@code sourceType} and {@code destinationType} using the
* ModelMapper's configuration.
*
* @param source type
* @param destination type
* @param sourceType
* @param destinationType
* @throws IllegalStateException if a TypeMap already exists for {@code sourceType} and
* {@code destinationType}
* @throws ConfigurationException if the ModelMapper cannot create the TypeMap
* @see #getTypeMap(Class, Class)
*/
public TypeMap createTypeMap(Class sourceType, Class destinationType) {
return this.createTypeMap(sourceType, destinationType, config);
}
/**
* Creates a TypeMap for the {@code sourceType} and {@code destinationType} using the given
* {@code configuration}.
*
* @param source type
* @param destination type
* @param sourceType
* @param destinationType
* @param configuration
* @throws IllegalStateException if a TypeMap already exists for {@code sourceType} and
* {@code destinationType}
* @throws ConfigurationException if the ModelMapper cannot create the TypeMap
* @see #getTypeMap(Class, Class)
*/
public TypeMap createTypeMap(Class sourceType, Class destinationType,
Configuration configuration) {
synchronized (config.typeMapStore.lock()) {
Assert.state(config.typeMapStore.get(sourceType, destinationType) == null,
String.format("A TypeMap already exists for %s and %s", sourceType, destinationType));
return config.typeMapStore.create(sourceType, destinationType, configuration, engine);
}
}
/**
* Returns the ModelMapper's configuration.
*/
public Configuration getConfiguration() {
return config;
}
/**
* Returns the TypeMap for the {@code sourceType} and {@code destinationType}, else returns
* {@code null} if none exists.
*
* @param source type
* @param destination type
* @throws IllegalArgumentException is {@code sourceType} or {@code destinationType} are null
* @see #createTypeMap(Class, Class)
*/
public TypeMap getTypeMap(Class sourceType, Class destinationType) {
Assert.notNull(sourceType, "sourceType");
Assert.notNull(destinationType, "destinationType");
return config.typeMapStore.get(sourceType, destinationType);
}
/**
* Returns all TypeMaps for the ModelMapper.
*/
public Collection> getTypeMaps() {
return config.typeMapStore.get();
}
/**
* Maps {@code source} to an instance of {@code destinationType}. Mapping is performed according
* to the corresponding TypeMap. If no TypeMap exists for {@code source.getClass()} and
* {@code destinationType} then one is created.
*
* @param destination type
* @param source object to map from
* @param destinationType type to map to
* @return fully mapped instance of {@code destinationType}
* @throws IllegalArgumentException if {@code source} or {@code destinationType} are null
* @throws ConfigurationException if the ModelMapper cannot find or create the TypeMap
* @throws MappingException if a runtime error occurs while mapping
*/
public D map(Object source, Class destinationType) {
Assert.notNull(source, "source");
Assert.notNull(destinationType, "destinationType");
return engine.
© 2015 - 2025 Weber Informatics LLC | Privacy Policy