-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(PropertyMappingStrategy): property mapping strategy that support…
…s concatenating collections with bit strings (GitHub #307)
- Loading branch information
1 parent
c6a7c76
commit 19bb167
Showing
17 changed files
with
143 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...rc/main/java/cn/crane4j/core/parser/handler/strategy/CollJoinAsStringMappingStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package cn.crane4j.core.parser.handler.strategy; | ||
|
||
import cn.crane4j.core.parser.PropertyMapping; | ||
import cn.crane4j.core.parser.operation.AssembleOperation; | ||
import cn.crane4j.core.util.StringUtils; | ||
import org.checkerframework.checker.nullness.qual.Nullable; | ||
|
||
import java.util.Collection; | ||
import java.util.function.Consumer; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Join as string mapping strategy. | ||
* | ||
* @author huangchengxing | ||
* @see AssembleOperation#getKeyDescription() | ||
* @since 2.9.0 | ||
*/ | ||
public class CollJoinAsStringMappingStrategy implements PropertyMappingStrategy { | ||
|
||
public static final CollJoinAsStringMappingStrategy INSTANCE = new CollJoinAsStringMappingStrategy(); | ||
private static final String DEFAULT_DELIMITER = ","; | ||
|
||
/** | ||
* Map {@code sourceValue} to reference fields in target. | ||
* | ||
* @param operation assemble operation | ||
* @param target target object | ||
* @param source source object | ||
* @param sourceValue source value | ||
* @param propertyMapping property mapping | ||
* @param mapping mapping action | ||
*/ | ||
@Override | ||
public void doMapping( | ||
AssembleOperation operation, | ||
Object target, Object source, @Nullable Object sourceValue, | ||
PropertyMapping propertyMapping, Consumer<Object> mapping) { | ||
if (sourceValue instanceof Collection) { | ||
String delimiter = StringUtils.emptyToDefault(operation.getKeyDescription(), DEFAULT_DELIMITER); | ||
sourceValue = ((Collection<?>)sourceValue).stream() | ||
.map(String::valueOf) | ||
.collect(Collectors.joining(delimiter)); | ||
} | ||
mapping.accept(sourceValue); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...est/java/cn/crane4j/core/parser/handler/strategy/CollJoinAsStringMappingStrategyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package cn.crane4j.core.parser.handler.strategy; | ||
|
||
import cn.crane4j.core.parser.PropertyMapping; | ||
import cn.crane4j.core.parser.SimplePropertyMapping; | ||
import cn.crane4j.core.parser.operation.SimpleAssembleOperation; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.util.Arrays; | ||
|
||
/** | ||
* test for {@link CollJoinAsStringMappingStrategy} | ||
* | ||
* @author huangchengxing | ||
*/ | ||
public class CollJoinAsStringMappingStrategyTest { | ||
|
||
@Test | ||
public void test() { | ||
CollJoinAsStringMappingStrategy strategy = CollJoinAsStringMappingStrategy.INSTANCE; | ||
PropertyMapping mapping = new SimplePropertyMapping("src", "ref"); | ||
strategy.doMapping( | ||
SimpleAssembleOperation.builder().keyDescription(" | ").build(), | ||
new Object(), new Object(), Arrays.asList(1, 2, 3), | ||
mapping, | ||
values -> Assert.assertEquals(values, "1 | 2 | 3") | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.