Skip to content

Commit

Permalink
Merge pull request #54 from 42BV/MapToDynamicClass
Browse files Browse the repository at this point in the history
Refactoring dynamic class
  • Loading branch information
robert-bor authored Jun 21, 2016
2 parents d948064 + c72dce8 commit 73ea916
Show file tree
Hide file tree
Showing 29 changed files with 538 additions and 354 deletions.
22 changes: 2 additions & 20 deletions src/main/java/io/beanmapper/BeanMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import io.beanmapper.config.Configuration;
import io.beanmapper.config.CoreConfiguration;
import io.beanmapper.core.constructor.BeanInitializer;
import io.beanmapper.core.rule.MappableFields;
import io.beanmapper.exceptions.BeanMappingException;
import io.beanmapper.strategy.MapStrategyType;

Expand Down Expand Up @@ -149,24 +148,6 @@ public <S, T> T map(S source, T target) {
.build()
.map(source);
}

/**
* Copies the values from the source object to an existing target instance
* @param source source instance of the properties
* @param target target instance for the properties
* @param <S> the instance from which the properties get copied.
* @param <T> the instance to which the properties get copied
* @return the original target instance containing all applicable properties
* @throws BeanMappingException
*/
public <S, T> T map(S source, T target, MappableFields fieldsToMap) {

return (T) config()
.setTarget(target)
.setMappableFields(fieldsToMap)
.build()
.map(source);
}

public final Configuration getConfiguration() {
return configuration;
Expand All @@ -175,7 +156,8 @@ public final Configuration getConfiguration() {
public BeanMapperBuilder clear() {
return BeanMapperBuilder
.config(configuration)
.setIncludeFields(null)
.downsizeSource(null)
.downsizeTarget(null)
.setCollectionClass(null)
.setTargetClass(null)
.setTarget(null);
Expand Down
15 changes: 7 additions & 8 deletions src/main/java/io/beanmapper/config/BeanMapperBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import io.beanmapper.core.converter.collections.CollectionMapConverter;
import io.beanmapper.core.converter.collections.CollectionSetConverter;
import io.beanmapper.core.converter.impl.*;
import io.beanmapper.core.rule.MappableFields;
import io.beanmapper.core.unproxy.BeanUnproxy;

import java.util.ArrayList;
Expand Down Expand Up @@ -76,8 +75,13 @@ public BeanMapperBuilder setBeanUnproxy(BeanUnproxy beanUnproxy) {
return this;
}

public BeanMapperBuilder setIncludeFields(List<String> includeFields) {
this.configuration.setIncludeFields(includeFields);
public BeanMapperBuilder downsizeSource(List<String> includeFields) {
this.configuration.downsizeSource(includeFields);
return this;
}

public BeanMapperBuilder downsizeTarget(List<String> includeFields) {
this.configuration.downsizeTarget(includeFields);
return this;
}

Expand All @@ -96,11 +100,6 @@ public BeanMapperBuilder setTarget(Object target) {
return this;
}

public BeanMapperBuilder setMappableFields(MappableFields mappableFields) {
this.configuration.setMappableFields(mappableFields);
return this;
}

public BeanMapperBuilder setConverterChoosable(boolean converterChoosable) {
this.configuration.setConverterChoosable(converterChoosable);
return this;
Expand Down
34 changes: 26 additions & 8 deletions src/main/java/io/beanmapper/config/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import io.beanmapper.core.BeanMatchStore;
import io.beanmapper.core.constructor.BeanInitializer;
import io.beanmapper.core.converter.BeanConverter;
import io.beanmapper.core.rule.MappableFields;
import io.beanmapper.core.unproxy.BeanUnproxy;
import io.beanmapper.core.unproxy.SkippingBeanUnproxy;
import io.beanmapper.dynclass.ClassStore;

import java.util.List;

Expand All @@ -18,7 +18,16 @@ public interface Configuration {
* Include fields never refer to the parent configuration.
* @return the fields to include in the target
*/
List<String> getIncludeFields();
List<String> getDownsizeTarget();

/**
* When include fields are passed, BeanMapper will assume that the generation (or reuse)
* of a dynamic class is required. For this, DynamicBeanMapper is used. Note that
* include fields is a marker field which impact the selection of the mapping strategy.
* Include fields never refer to the parent configuration.
* @return the fields to include in the source
*/
List<String> getDownsizeSource();

/**
* The class that represents the collection itself. Used to instantiate a collection.
Expand All @@ -43,8 +52,6 @@ public interface Configuration {
*/
Object getTarget();

MappableFields getMappableFields();

BeanInitializer getBeanInitializer();

SkippingBeanUnproxy getBeanUnproxy();
Expand All @@ -55,6 +62,12 @@ public interface Configuration {
*/
BeanMatchStore getBeanMatchStore();

/**
* Always use the CoreConfiguration class store
* @return the one class store
*/
ClassStore getClassStore();

List<String> getPackagePrefixes();

List<BeanConverter> getBeanConverters();
Expand Down Expand Up @@ -103,17 +116,22 @@ public interface Configuration {

boolean isAddDefaultConverters();

void setMappableFields(MappableFields mappableFields);

void setConverterChoosable(boolean converterChoosable);

/**
* Sets the field to downsize the source class.
* THe limited source class is mapped over the given target class.
* @param includeFields The fields to be mapped to the target class.
*/
void downsizeSource(List<String> includeFields);

/**
* Sets the only fields that are allowed in the target class. If the include fields are set,
* it impacts the usage of the mapping strategy. Note that getting this field never refers to
* the parent configuration.
* @param includeFields
* @param includeFields The field that are allowed in the target class.
*/
void setIncludeFields(List<String> includeFields);
void downsizeTarget(List<String> includeFields);

/**
* Sets the collection class of the collection. Used to instantiate the collection. If the
Expand Down
33 changes: 21 additions & 12 deletions src/main/java/io/beanmapper/config/CoreConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import io.beanmapper.core.constructor.BeanInitializer;
import io.beanmapper.core.constructor.DefaultBeanInitializer;
import io.beanmapper.core.converter.BeanConverter;
import io.beanmapper.core.rule.MappableFields;
import io.beanmapper.core.unproxy.BeanUnproxy;
import io.beanmapper.core.unproxy.DefaultBeanUnproxy;
import io.beanmapper.core.unproxy.SkippingBeanUnproxy;
import io.beanmapper.dynclass.ClassStore;
import io.beanmapper.exceptions.BeanConfigurationOperationNotAllowedException;

import java.util.ArrayList;
Expand All @@ -31,6 +31,12 @@ public class CoreConfiguration implements Configuration {
*/
private BeanMatchStore beanMatchStore = new BeanMatchStore();

/**
* Contains a store of classes that are generated by using the MapToDynamicClassStrategy.
* Every generated class is a downsized source class or a downsized target class.
*/
private ClassStore classStore = new ClassStore();

/**
* The list of packages (and subpackages) containing classes which are eligible for mapping.
*/
Expand All @@ -44,7 +50,10 @@ public class CoreConfiguration implements Configuration {
private boolean addDefaultConverters = true;

@Override
public List<String> getIncludeFields() { return null; }
public List<String> getDownsizeTarget() { return null; }

@Override
public List<String> getDownsizeSource() { return null; }

@Override
public Class getTargetClass() {
Expand All @@ -61,11 +70,6 @@ public Class getCollectionClass() {
return null;
}

@Override
public MappableFields getMappableFields() {
return null;
}

@Override
public BeanInitializer getBeanInitializer() {
return this.beanInitializer;
Expand All @@ -81,6 +85,11 @@ public BeanMatchStore getBeanMatchStore() {
return this.beanMatchStore;
}

@Override
public ClassStore getClassStore() {
return this.classStore;
}

@Override
public List<String> getPackagePrefixes() {
return this.packagePrefixes;
Expand Down Expand Up @@ -138,19 +147,19 @@ public boolean isAddDefaultConverters() {
}

@Override
public void setMappableFields(MappableFields mappableFields) {
public void setConverterChoosable(boolean converterChoosable) {
throw new BeanConfigurationOperationNotAllowedException(
"Illegal to set mappable fields on the Core configuration, works only for override configurations");
"Illegal to set whether the converter is choosable on the Core configuration, works only for override configurations.");
}

@Override
public void setConverterChoosable(boolean converterChoosable) {
public void downsizeSource(List<String> includeFields) {
throw new BeanConfigurationOperationNotAllowedException(
"Illegal to set whether the converter is choosable on the Core configuration, works only for override configurations.");
"Illegal to set a include fields on the Core configuration, works only for override configurations");
}

@Override
public void setIncludeFields(List<String> includeFields) {
public void downsizeTarget(List<String> includeFields) {
throw new BeanConfigurationOperationNotAllowedException(
"Illegal to set a include fields on the Core configuration, works only for override configurations");
}
Expand Down
41 changes: 23 additions & 18 deletions src/main/java/io/beanmapper/config/OverrideConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import io.beanmapper.core.BeanMatchStore;
import io.beanmapper.core.constructor.BeanInitializer;
import io.beanmapper.core.converter.BeanConverter;
import io.beanmapper.core.rule.MappableFields;
import io.beanmapper.core.unproxy.BeanUnproxy;
import io.beanmapper.core.unproxy.SkippingBeanUnproxy;
import io.beanmapper.dynclass.ClassStore;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -22,16 +22,16 @@ public class OverrideConfiguration implements Configuration {

private List<BeanConverter> beanConverters = new ArrayList<BeanConverter>();

private List<String> includeFields;
private List<String> downsizeSourceFields;

private List<String> downsizeTargetFields;

private Class targetClass;

private Object target;

private Class collectionClass;

private MappableFields mappableFields;

private boolean converterChoosable = true;

public OverrideConfiguration(Configuration configuration) {
Expand All @@ -42,8 +42,13 @@ public OverrideConfiguration(Configuration configuration) {
}

@Override
public List<String> getIncludeFields() {
return includeFields;
public List<String> getDownsizeSource() {
return downsizeSourceFields;
}

@Override
public List<String> getDownsizeTarget() {
return downsizeTargetFields;
}

@Override
Expand All @@ -61,11 +66,6 @@ public Object getTarget() {
return target;
}

@Override
public MappableFields getMappableFields() {
return mappableFields == null ? parentConfiguration.getMappableFields() : mappableFields;
}

@Override
public BeanInitializer getBeanInitializer() {
return beanInitializer == null ? parentConfiguration.getBeanInitializer() : beanInitializer;
Expand All @@ -81,6 +81,11 @@ public BeanMatchStore getBeanMatchStore() {
return parentConfiguration.getBeanMatchStore();
}

@Override
public ClassStore getClassStore() {
return parentConfiguration.getClassStore();
}

@Override
public List<String> getPackagePrefixes() {
return packagePrefixes == null ? parentConfiguration.getPackagePrefixes() : packagePrefixes;
Expand Down Expand Up @@ -140,8 +145,13 @@ public boolean isAddDefaultConverters() {
}

@Override
public void setIncludeFields(List<String> includeFields) {
this.includeFields = includeFields;
public void downsizeSource(List<String> includeFields) {
this.downsizeSourceFields = includeFields;
}

@Override
public void downsizeTarget(List<String> includeFields) {
this.downsizeTargetFields = includeFields;
}

@Override
Expand All @@ -159,11 +169,6 @@ public void setTarget(Object target) {
this.target = target;
}

@Override
public void setMappableFields(MappableFields mappableFields) {
this.mappableFields = mappableFields;
}

@Override
public void setConverterChoosable(boolean converterChoosable) {
this.converterChoosable = converterChoosable;
Expand Down
13 changes: 0 additions & 13 deletions src/main/java/io/beanmapper/core/BeanMatch.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.beanmapper.core;

import io.beanmapper.core.rule.MappableFields;

import java.util.Map;

public class BeanMatch {
Expand All @@ -16,19 +14,12 @@ public class BeanMatch {

private final Map<String, BeanField> aliases;

private final MappableFields mappableFields;

public BeanMatch(Class<?> sourceClass, Class<?> targetClass, Map<String, BeanField> sourceNode, Map<String, BeanField> targetNode, Map<String, BeanField> aliases) {
this(sourceClass, targetClass, sourceNode, targetNode, aliases, null);
}

public BeanMatch(Class<?> sourceClass, Class<?> targetClass, Map<String, BeanField> sourceNode, Map<String, BeanField> targetNode, Map<String, BeanField> aliases, MappableFields mappableFields) {
this.sourceClass = sourceClass;
this.targetClass = targetClass;
this.sourceNode = sourceNode;
this.targetNode = targetNode;
this.aliases = aliases;
this.mappableFields = mappableFields;
}

public Class<?> getSourceClass() {
Expand All @@ -50,8 +41,4 @@ public Map<String, BeanField> getTargetNode() {
public Map<String, BeanField> getAliases() {
return aliases;
}

public MappableFields getMappableFields() {
return mappableFields;
}
}
Loading

0 comments on commit 73ea916

Please sign in to comment.