|
| 1 | +package com.github.jonpereiradev.diffobjects; |
| 2 | + |
| 3 | + |
| 4 | +import com.github.jonpereiradev.diffobjects.annotation.DiffOrder; |
| 5 | +import com.github.jonpereiradev.diffobjects.builder.DiffBuilderContext; |
| 6 | +import com.github.jonpereiradev.diffobjects.strategy.DiffMetadata; |
| 7 | + |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.Collections; |
| 10 | +import java.util.List; |
| 11 | +import java.util.Map; |
| 12 | + |
| 13 | + |
| 14 | +/** |
| 15 | + * Responsible for generate the configuration of the instance. |
| 16 | + * |
| 17 | + * @author Jonathan Pereira |
| 18 | + * @since 1.0.0 |
| 19 | + */ |
| 20 | + |
| 21 | +public final class DiffConfigImpl implements DiffConfig { |
| 22 | + |
| 23 | + private final DiffBuilderContext<?> context; |
| 24 | + private final List<DiffMetadata> metadataList; |
| 25 | + |
| 26 | + public DiffConfigImpl(DiffBuilderContext<?> context) { |
| 27 | + this.context = context; |
| 28 | + this.metadataList = new ArrayList<>(context.getMetadataMap().size()); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Gets the configuration for the instance. |
| 33 | + * |
| 34 | + * @return the metadata generated by the instance. |
| 35 | + */ |
| 36 | + @Override |
| 37 | + public List<DiffMetadata> build() { |
| 38 | + if (metadataList.isEmpty()) { |
| 39 | + boolean sortable = false; |
| 40 | + |
| 41 | + for (Map.Entry<String, DiffMetadata> entry : context.getMetadataMap().entrySet()) { |
| 42 | + DiffOrder annotation = entry.getValue().getMethod().getAnnotation(DiffOrder.class); |
| 43 | + |
| 44 | + if (annotation != null) { |
| 45 | + entry.getValue().setOrder(annotation.value()); |
| 46 | + sortable = true; |
| 47 | + } |
| 48 | + |
| 49 | + metadataList.add(entry.getValue()); |
| 50 | + } |
| 51 | + |
| 52 | + if (sortable) { |
| 53 | + Collections.sort(metadataList); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + return metadataList; |
| 58 | + } |
| 59 | + |
| 60 | +} |
0 commit comments