|
| 1 | +package pl.wavesoftware.utils.mapstruct.jpa; |
| 2 | + |
| 3 | +import org.mapstruct.BeforeMapping; |
| 4 | +import org.mapstruct.MappingTarget; |
| 5 | +import org.mapstruct.TargetType; |
| 6 | + |
| 7 | +import javax.annotation.Nullable; |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.Arrays; |
| 10 | +import java.util.Collections; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Optional; |
| 13 | + |
| 14 | +/** |
| 15 | + * A composite mapping context that can utilize multiple mapping contexts. |
| 16 | + * |
| 17 | + * @author <a href="mailto:krzysztof.suszynski@wavesoftware.pl">Krzysztof Suszynski</a> |
| 18 | + * @since 02.05.18 |
| 19 | + */ |
| 20 | +public final class CompositeContext implements MapStructContext { |
| 21 | + private final List<MappingContext> mappingContexts = new ArrayList<>(); |
| 22 | + |
| 23 | + /** |
| 24 | + * You can pass multiple mapping contexts to be used in this composite mapping context |
| 25 | + * |
| 26 | + * @param mappingContexts a array of mapping contexts |
| 27 | + */ |
| 28 | + public CompositeContext(MappingContext... mappingContexts) { |
| 29 | + Collections.addAll(this.mappingContexts, mappingContexts); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * A builder interface for {@link CompositeContext}. |
| 34 | + * |
| 35 | + * @return a builder interface |
| 36 | + */ |
| 37 | + public static CompositeContextBuilder builder() { |
| 38 | + return new CompositeContextBuilderImpl(); |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + @BeforeMapping |
| 43 | + public void storeMappedInstance(Object source, |
| 44 | + @MappingTarget Object target) { |
| 45 | + for (MappingContext mappingContext : mappingContexts) { |
| 46 | + if (mappingContext instanceof StoringMappingContext) { |
| 47 | + StoringMappingContext.class.cast(mappingContext) |
| 48 | + .storeMappedInstance(source, target); |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + @Nullable |
| 55 | + @BeforeMapping |
| 56 | + public <T> T getMappedInstance(Object source, |
| 57 | + @TargetType Class<T> targetType) { |
| 58 | + return getMappedInstanceOptional(source, targetType) |
| 59 | + .orElse(null); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public <T> Optional<T> getMappedInstanceOptional(Object source, |
| 64 | + Class<T> targetType) { |
| 65 | + for (MappingContext mappingContext : mappingContexts) { |
| 66 | + Optional<T> instance = mappingContext.getMappedInstanceOptional(source, targetType); |
| 67 | + if (instance.isPresent()) { |
| 68 | + return instance; |
| 69 | + } |
| 70 | + } |
| 71 | + return Optional.empty(); |
| 72 | + } |
| 73 | + |
| 74 | + private static final class CompositeContextBuilderImpl implements CompositeContextBuilder { |
| 75 | + private final List<MappingContext> mappingContexts = new ArrayList<>(); |
| 76 | + |
| 77 | + @Override |
| 78 | + public CompositeContextBuilderImpl addContext(MappingContext... mappingContexts) { |
| 79 | + this.mappingContexts.addAll( |
| 80 | + Arrays.asList(mappingContexts) |
| 81 | + ); |
| 82 | + return this; |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public CompositeContext build() { |
| 87 | + return new CompositeContext( |
| 88 | + mappingContexts.toArray(new MappingContext[0]) |
| 89 | + ); |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments