|
18 | 18 |
|
19 | 19 | package org.spacious_team.broker.report_parser.api; |
20 | 20 |
|
| 21 | +import lombok.AccessLevel; |
21 | 22 | import lombok.Getter; |
| 23 | +import lombok.RequiredArgsConstructor; |
22 | 24 |
|
23 | 25 | import java.util.ArrayList; |
24 | 26 | import java.util.Arrays; |
25 | 27 | import java.util.Collection; |
26 | 28 | import java.util.Collections; |
27 | 29 | import java.util.List; |
28 | 30 |
|
| 31 | +@RequiredArgsConstructor(access = AccessLevel.PRIVATE) |
29 | 32 | public class WrappingReportTable<RowType> implements ReportTable<RowType> { |
30 | | - @Getter |
31 | | - private final BrokerReport report; |
32 | | - private final List<RowType> data; |
| 33 | + private final ReportTable<RowType> reportTable; |
| 34 | + |
| 35 | + @SafeVarargs |
| 36 | + public static <T> WrappingReportTable<T> of(BrokerReport report, Collection<T>... dataset) { |
| 37 | + return new WrappingReportTable<>(new EagerWrappingReportTable<>(report, dataset)); |
| 38 | + } |
33 | 39 |
|
34 | 40 | @SafeVarargs |
35 | 41 | public static <T> WrappingReportTable<T> of(ReportTable<T>... tables) { |
36 | 42 | assertIsTrue(tables.length > 0, "Can't wrap, report tables not provided"); |
37 | 43 | BrokerReport report = tables[0].getReport(); |
38 | 44 | boolean isAllReportsIsSame = Arrays.stream(tables) |
39 | 45 | .map(ReportTable::getReport) |
40 | | - .filter(tableReport -> tableReport != report) |
41 | | - .findAny() |
42 | | - .isEmpty(); |
| 46 | + .allMatch(tableReport -> tableReport == report); |
43 | 47 | assertIsTrue(isAllReportsIsSame, "Wrapping report tables should be built for same broker report"); |
44 | | - return new WrappingReportTable<>(report, tables); |
45 | | - |
| 48 | + return new WrappingReportTable<>(new LazyWrappingReportTable<>(report, tables)); |
46 | 49 | } |
47 | 50 |
|
48 | | - @SafeVarargs |
49 | | - public WrappingReportTable(BrokerReport report, ReportTable<RowType>... tables) { |
50 | | - List<RowType> data = new ArrayList<>(); |
51 | | - for (ReportTable<RowType> table : tables) { |
52 | | - data.addAll(table.getData()); |
| 51 | + private static void assertIsTrue(boolean expression, String message) { |
| 52 | + if (!expression) { |
| 53 | + throw new IllegalArgumentException(message); |
53 | 54 | } |
54 | | - this.report = report; |
55 | | - this.data = data; |
56 | 55 | } |
57 | 56 |
|
58 | | - @SafeVarargs |
59 | | - public static <T> WrappingReportTable<T> of(BrokerReport report, Collection<T>... dataset) { |
60 | | - return new WrappingReportTable<>(report, dataset); |
| 57 | + @Override |
| 58 | + public BrokerReport getReport() { |
| 59 | + return reportTable.getReport(); |
61 | 60 | } |
62 | 61 |
|
63 | | - @SafeVarargs |
64 | | - public WrappingReportTable(BrokerReport report, Collection<RowType>... dataset) { |
65 | | - List<RowType> data = new ArrayList<>(); |
66 | | - for (Collection<RowType> d : dataset) { |
67 | | - data.addAll(d); |
68 | | - } |
69 | | - this.report = report; |
70 | | - this.data = data; |
| 62 | + @Override |
| 63 | + public List<RowType> getData() { |
| 64 | + return reportTable.getData(); |
71 | 65 | } |
72 | 66 |
|
73 | | - private static void assertIsTrue(boolean expression, String message) { |
74 | | - if (!expression) { |
75 | | - throw new IllegalArgumentException(message); |
| 67 | + @Getter |
| 68 | + private static class EagerWrappingReportTable<RowType> implements ReportTable<RowType> { |
| 69 | + private final BrokerReport report; |
| 70 | + private final List<RowType> data; |
| 71 | + |
| 72 | + @SafeVarargs |
| 73 | + public EagerWrappingReportTable(BrokerReport report, Collection<RowType>... dataset) { |
| 74 | + List<RowType> data = new ArrayList<>(); |
| 75 | + for (Collection<RowType> d : dataset) { |
| 76 | + data.addAll(d); |
| 77 | + } |
| 78 | + this.report = report; |
| 79 | + this.data = Collections.unmodifiableList(data); |
76 | 80 | } |
77 | 81 | } |
78 | 82 |
|
79 | | - @Override |
80 | | - public List<RowType> getData() { |
81 | | - return Collections.unmodifiableList(data); |
| 83 | + @RequiredArgsConstructor |
| 84 | + private static class LazyWrappingReportTable<RowType> implements ReportTable<RowType> { |
| 85 | + @Getter |
| 86 | + private final BrokerReport report; |
| 87 | + private final ReportTable<RowType>[] tables; |
| 88 | + |
| 89 | + @Override |
| 90 | + public List<RowType> getData() { |
| 91 | + List<RowType> data = new ArrayList<>(); |
| 92 | + for (ReportTable<RowType> table : tables) { |
| 93 | + data.addAll(table.getData()); |
| 94 | + } |
| 95 | + return Collections.unmodifiableList(data); |
| 96 | + } |
82 | 97 | } |
83 | 98 | } |
0 commit comments