| 1 | package pro.verron.officestamper.preset.resolvers.objects; | |
| 2 | ||
| 3 | import org.jspecify.annotations.Nullable; | |
| 4 | import pro.verron.officestamper.api.DocxPart; | |
| 5 | import pro.verron.officestamper.api.Insert; | |
| 6 | import pro.verron.officestamper.api.ObjectResolver; | |
| 7 | ||
| 8 | import java.util.ArrayList; | |
| 9 | import java.util.List; | |
| 10 | ||
| 11 | import static pro.verron.officestamper.utils.wml.WmlFactory.*; | |
| 12 | ||
| 13 | /// This class is an implementation of the [ObjectResolver] interface that resolves objects by converting them to a | |
| 14 | /// string representation using the [Object#toString()] method and creating a new run with the resolved content. | |
| 15 | /// | |
| 16 | /// @author Joseph Verron | |
| 17 | /// @version ${version} | |
| 18 | /// @since 1.6.7 | |
| 19 | public class ToStringResolver | |
| 20 | implements ObjectResolver { | |
| 21 | ||
| 22 | private final String linebreakPlaceholder; | |
| 23 | ||
| 24 | ||
| 25 | /// Creates a new instance of the [ToStringResolver] class with the specified line break placeholder. | |
| 26 | /// | |
| 27 | /// @param linebreakPlaceholder the placeholder string used to identify line breaks within the string | |
| 28 | /// representation of an object. This placeholder will be replaced with actual line break elements in the | |
| 29 | /// resulting document. | |
| 30 | public ToStringResolver(String linebreakPlaceholder) { | |
| 31 | this.linebreakPlaceholder = linebreakPlaceholder; | |
| 32 | } | |
| 33 | ||
| 34 | @Override | |
| 35 | public Insert resolve(DocxPart part, String expression, @Nullable Object object) { | |
| 36 | var string = String.valueOf(object); | |
| 37 | ||
| 38 | var split = string.split(linebreakPlaceholder); | |
| 39 |
2
1. resolve : negated conditional → TIMED_OUT 2. resolve : replaced return value with null for pro/verron/officestamper/preset/resolvers/objects/ToStringResolver::resolve → TIMED_OUT |
if (split.length == 1) return new Insert(newRun(string)); |
| 40 | var elements = new ArrayList<>(); | |
| 41 |
3
1. resolve : negated conditional → TIMED_OUT 2. resolve : Replaced integer subtraction with addition → TIMED_OUT 3. resolve : changed conditional boundary → KILLED |
for (int i = 0; i < split.length - 1; i++) { |
| 42 | var line = split[i]; | |
| 43 | elements.add(newRun(List.of(newText(line), newBr()))); | |
| 44 | } | |
| 45 |
1
1. resolve : Replaced integer subtraction with addition → TIMED_OUT |
elements.add(newRun(split[split.length - 1])); |
| 46 |
1
1. resolve : replaced return value with null for pro/verron/officestamper/preset/resolvers/objects/ToStringResolver::resolve → TIMED_OUT |
return new Insert(elements); |
| 47 | } | |
| 48 | ||
| 49 | @Override | |
| 50 | public boolean canResolve(@Nullable Object object) { | |
| 51 |
2
1. canResolve : replaced boolean return with true for pro/verron/officestamper/preset/resolvers/objects/ToStringResolver::canResolve → TIMED_OUT 2. canResolve : negated conditional → TIMED_OUT |
return object != null; |
| 52 | } | |
| 53 | } | |
Mutations | ||
| 39 |
1.1 2.2 |
|
| 41 |
1.1 2.2 3.3 |
|
| 45 |
1.1 |
|
| 46 |
1.1 |
|
| 51 |
1.1 2.2 |