This commit is contained in:
Keith Donald
2009-10-17 04:03:56 +00:00
parent f55b54ec3b
commit f63c3d5313
9 changed files with 108 additions and 32 deletions

View File

@@ -45,6 +45,10 @@ final class FieldToFieldMapping implements SpelMapping {
return this.targetField.getExpressionString();
}
public boolean mapsField(String field) {
return getSourceField().equals(field);
}
@SuppressWarnings("unchecked")
public void map(SpelMappingContext context) {
try {

View File

@@ -38,6 +38,10 @@ final class FieldToMultiFieldMapping implements SpelMapping {
return this.sourceField.getExpressionString();
}
public boolean mapsField(String field) {
return getSourceField().equals(field);
}
@SuppressWarnings("unchecked")
public void map(SpelMappingContext context) {
try {

View File

@@ -101,10 +101,11 @@ public interface MapperBuilder<S, T> {
* Register a mapping between multiple source fields and a single target field.
* For example, calling <code>addMapping(dateAndTimeFieldsToDateTimeFieldMapper)</code> might register a mapping that maps the <code>date</code> and <code>time</code> fields on the source to the <code>dateTime</code> field on the target.
* The provided {@link Mapper} will be passed the source object S for its source and the target object T for its target.
* @param fields the source field mapping expressions
* @param mapper the fields to field mapper
* @return this, for configuring additional field mapping options fluently
*/
MapperBuilder<S, T> addMapping(Mapper<S, T> mapper);
MapperBuilder<S, T> addMapping(String[] fields, Mapper<S, T> mapper);
/**
* Register a Mapper that will be used to map between nested source and target fields of a specific sourceType/targetType pair.

View File

@@ -15,6 +15,7 @@
*/
package org.springframework.mapping.support;
import org.springframework.core.style.StylerUtils;
import org.springframework.mapping.Mapper;
/**
@@ -23,13 +24,29 @@ import org.springframework.mapping.Mapper;
*/
final class MultiFieldToFieldMapping implements SpelMapping {
private String[] fields;
@SuppressWarnings("unchecked")
private final Mapper multiFieldMapper;
public MultiFieldToFieldMapping(Mapper<?, ?> multiFieldMapper) {
public MultiFieldToFieldMapping(String[] fields, Mapper<?, ?> multiFieldMapper) {
this.fields = fields;
this.multiFieldMapper = multiFieldMapper;
}
public String[] getSourceFields() {
return fields;
}
public boolean mapsField(String field) {
for (String f : this.fields) {
if (f.equals(field)) {
return true;
}
}
return false;
}
@SuppressWarnings("unchecked")
public void map(SpelMappingContext context) {
try {
@@ -52,7 +69,7 @@ final class MultiFieldToFieldMapping implements SpelMapping {
}
public String toString() {
return "[MultiFieldToFieldMapping<" + this.multiFieldMapper + ">]";
return "[MultiFieldToFieldMapping<" + StylerUtils.style(this.fields) + " -> " + this.multiFieldMapper + ">]";
}
}

View File

@@ -78,12 +78,12 @@ final class SpelMapper implements Mapper<Object, Object> {
this.mappings.add(mapping);
}
public void addMapping(String field, Mapper mapper) {
public void addMapping(String field, Mapper<?, ?> mapper) {
this.mappings.add(new FieldToMultiFieldMapping(parseSourceField(field), mapper));
}
public void addMapping(Mapper mapper) {
this.mappings.add(new MultiFieldToFieldMapping(mapper));
public void addMapping(String[] fields, Mapper<?, ?> mapper) {
this.mappings.add(new MultiFieldToFieldMapping(fields, mapper));
}
/**
@@ -240,8 +240,7 @@ final class SpelMapper implements Mapper<Object, Object> {
private boolean explicitlyMapped(String field) {
for (SpelMapping mapping : this.mappings) {
if (mapping instanceof FieldToFieldMapping
&& ((FieldToFieldMapping) mapping).getSourceField().startsWith(field)) {
if (mapping.mapsField(field)) {
return true;
}
}

View File

@@ -64,8 +64,8 @@ final class SpelMapperBuilder<S, T> implements MapperBuilder<S, T> {
return this;
}
public MapperBuilder<S, T> addMapping(Mapper<S, T> mapper) {
this.mapper.addMapping(mapper);
public MapperBuilder<S, T> addMapping(String[] fields, Mapper<S, T> mapper) {
this.mapper.addMapping(fields, mapper);
return this;
}

View File

@@ -21,6 +21,11 @@ package org.springframework.mapping.support;
*/
interface SpelMapping {
/**
* Return true if this maps the source field.
*/
boolean mapsField(String field);
/**
* Execute this mapping.
* @param context the mapping context