|
1 | 1 | package org.approvaltests.core; |
2 | 2 |
|
3 | | -import java.util.Optional; |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map; |
4 | 5 |
|
5 | 6 | import org.approvaltests.Approvals; |
6 | 7 | import org.approvaltests.ReporterFactory; |
7 | 8 | import org.approvaltests.namer.ApprovalNamer; |
8 | 9 | import org.approvaltests.namer.NamerWrapper; |
9 | 10 | import org.approvaltests.scrubbers.NoOpScrubber; |
10 | 11 |
|
| 12 | +import com.spun.util.ArrayUtils; |
| 13 | + |
11 | 14 | public class Options |
12 | 15 | { |
13 | | - private Scrubber scrubber = NoOpScrubber.INSTANCE; |
14 | | - private Optional<ApprovalFailureReporter> reporter = Optional.empty(); |
15 | | - private FileOptions fileOptions = new FileOptions(); |
| 16 | + private enum Fields { |
| 17 | + SCRUBBER, REPORTER, FILE_OPTIONS_FILE_EXTENSION, FILE_OPTIONS_NAMER; |
| 18 | + } |
| 19 | + private final Map<Fields, Object> fields = new HashMap<>(); |
16 | 20 | public Options() |
17 | 21 | { |
18 | 22 | } |
19 | 23 | public Options(Scrubber scrubber) |
20 | 24 | { |
21 | | - this.scrubber = scrubber; |
| 25 | + fields.put(Fields.SCRUBBER, scrubber); |
22 | 26 | } |
23 | 27 | public Options(ApprovalFailureReporter reporter) |
24 | 28 | { |
25 | | - this.reporter = Optional.ofNullable(reporter); |
26 | | - } |
27 | | - private Options(Scrubber scrubber, Optional<ApprovalFailureReporter> reporter, FileOptions fileOptions) |
28 | | - { |
29 | | - this.scrubber = scrubber; |
30 | | - this.reporter = reporter; |
31 | | - this.fileOptions = fileOptions; |
| 29 | + fields.put(Fields.REPORTER, reporter); |
32 | 30 | } |
33 | | - public Options(Options parent, FileOptions fileOptions) |
| 31 | + private Options(Map<Fields, Object> fields, Fields key, Object value) |
34 | 32 | { |
35 | | - this(parent.scrubber, parent.reporter, fileOptions); |
| 33 | + this.fields.putAll(fields); |
| 34 | + this.fields.put(key, value); |
36 | 35 | } |
37 | 36 | public ApprovalFailureReporter getReporter() |
38 | 37 | { |
39 | | - return this.reporter.orElseGet(ReporterFactory::get); |
| 38 | + return ArrayUtils.getOrElse(fields, Fields.REPORTER, ReporterFactory::get); |
40 | 39 | } |
41 | 40 | public Options withReporter(ApprovalFailureReporter reporter) |
42 | 41 | { |
43 | | - return new Options(this.scrubber, Optional.ofNullable(reporter), fileOptions); |
| 42 | + return new Options(fields, Fields.REPORTER, reporter); |
44 | 43 | } |
45 | 44 | public Options withScrubber(Scrubber scrubber) |
46 | 45 | { |
47 | | - return new Options(scrubber, this.reporter, fileOptions); |
| 46 | + return new Options(fields, Fields.SCRUBBER, scrubber); |
48 | 47 | } |
49 | 48 | public String scrub(String input) |
50 | 49 | { |
51 | | - return scrubber.scrub(input); |
| 50 | + return getScrubber().scrub(input); |
| 51 | + } |
| 52 | + private Scrubber getScrubber() |
| 53 | + { |
| 54 | + return ArrayUtils.getOrElse(fields, Fields.SCRUBBER, () -> NoOpScrubber.INSTANCE); |
52 | 55 | } |
53 | 56 | public FileOptions forFile() |
54 | 57 | { |
55 | | - fileOptions.setParent(this); |
56 | | - return fileOptions; |
| 58 | + return new FileOptions(this.fields); |
57 | 59 | } |
58 | 60 | public static class FileOptions |
59 | 61 | { |
60 | | - private String fileExtension = ".txt"; |
61 | | - protected NamerWrapper approvalNamer = null; |
62 | | - protected Options parent; |
63 | | - public FileOptions() |
64 | | - { |
65 | | - } |
66 | | - public FileOptions(String fileExtension) |
67 | | - { |
68 | | - this.fileExtension = fileExtension; |
69 | | - } |
70 | | - public FileOptions(NamerWrapper approvalNamer, String fileExtension) |
71 | | - { |
72 | | - this.approvalNamer = approvalNamer; |
73 | | - this.fileExtension = fileExtension; |
74 | | - } |
75 | | - public void setParent(Options parent) |
| 62 | + private final Map<Fields, Object> fields; |
| 63 | + public FileOptions(Map<Fields, Object> fields) |
76 | 64 | { |
77 | | - this.parent = parent; |
| 65 | + this.fields = fields; |
78 | 66 | } |
79 | 67 | public Options withExtension(String fileExtensionWithDot) |
80 | 68 | { |
81 | 69 | if (!fileExtensionWithDot.startsWith(".")) |
82 | 70 | { |
83 | 71 | fileExtensionWithDot = "." + fileExtensionWithDot; |
84 | 72 | } |
85 | | - FileOptions f = new FileOptions(this.approvalNamer, fileExtensionWithDot); |
86 | | - return new Options(parent, f); |
| 73 | + return new Options(fields, Fields.FILE_OPTIONS_FILE_EXTENSION, fileExtensionWithDot); |
87 | 74 | } |
88 | 75 | public ApprovalNamer getNamer() |
89 | 76 | { |
90 | | - return approvalNamer == null ? Approvals.createApprovalNamer() : approvalNamer; |
| 77 | + return ArrayUtils.getOrElse(fields, Fields.FILE_OPTIONS_NAMER, Approvals::createApprovalNamer); |
91 | 78 | } |
92 | 79 | public String getFileExtension() |
93 | 80 | { |
94 | | - return fileExtension; |
| 81 | + return ArrayUtils.getOrElse(fields, Fields.FILE_OPTIONS_FILE_EXTENSION, () -> ".txt"); |
95 | 82 | } |
96 | 83 | public Options withBaseName(String fileBaseName) |
97 | 84 | { |
98 | 85 | NamerWrapper approvalNamer = new NamerWrapper(() -> fileBaseName, getNamer()); |
99 | | - FileOptions f = new FileOptions(approvalNamer, this.fileExtension); |
100 | | - return new Options(parent, f); |
| 86 | + return new Options(fields, Fields.FILE_OPTIONS_NAMER, approvalNamer); |
101 | 87 | } |
102 | 88 | public Options withName(String fileBaseName, String extension) |
103 | 89 | { |
104 | 90 | NamerWrapper approvalNamer = new NamerWrapper(() -> fileBaseName, getNamer()); |
105 | | - FileOptions f = new FileOptions(approvalNamer, extension); |
106 | | - return new Options(parent, f); |
| 91 | + HashMap<Fields, Object> newFields = new HashMap<>(fields); |
| 92 | + newFields.put(Fields.FILE_OPTIONS_FILE_EXTENSION, extension); |
| 93 | + return new Options(newFields, Fields.FILE_OPTIONS_NAMER, approvalNamer); |
107 | 94 | } |
108 | 95 | } |
109 | 96 | } |
0 commit comments