|
36 | 36 | */ |
37 | 37 | public abstract class AbstractDocument implements Document { |
38 | 38 |
|
39 | | - private final Map<String, Object> properties; |
| 39 | + private final Map<String, Object> documentProperties; |
40 | 40 |
|
41 | 41 | protected AbstractDocument(Map<String, Object> properties) { |
42 | 42 | Objects.requireNonNull(properties, "properties map is required"); |
43 | | - this.properties = properties; |
| 43 | + this.documentProperties = properties; |
44 | 44 | } |
45 | 45 |
|
46 | 46 | @Override |
47 | 47 | public Void put(String key, Object value) { |
48 | | - properties.put(key, value); |
| 48 | + documentProperties.put(key, value); |
49 | 49 | return null; |
50 | 50 | } |
51 | 51 |
|
52 | 52 | @Override |
53 | 53 | public Object get(String key) { |
54 | | - return properties.get(key); |
| 54 | + return documentProperties.get(key); |
55 | 55 | } |
56 | 56 |
|
57 | 57 | @Override |
58 | | - public <T> Stream<T> children(String key, Function<Map<String, Object>, T> constructor) { |
| 58 | + public <T> Stream<T> children(String key, Function<Map<String, Object>, T> childConstructor) { |
59 | 59 | return Stream.ofNullable(get(key)) |
60 | | - .filter(Objects::nonNull) |
61 | | - .map(el -> (List<Map<String, Object>>) el) |
62 | | - .findAny() |
63 | | - .stream() |
64 | | - .flatMap(Collection::stream) |
65 | | - .map(constructor); |
| 60 | + .filter(Objects::nonNull) |
| 61 | + .map(el -> (List<Map<String, Object>>) el) |
| 62 | + .findAny() |
| 63 | + .stream() |
| 64 | + .flatMap(Collection::stream) |
| 65 | + .map(childConstructor); |
66 | 66 | } |
67 | 67 |
|
68 | 68 | @Override |
69 | 69 | public String toString() { |
| 70 | + return buildStringRepresentation(); |
| 71 | + } |
| 72 | + |
| 73 | + private String buildStringRepresentation() { |
70 | 74 | var builder = new StringBuilder(); |
71 | 75 | builder.append(getClass().getName()).append("["); |
72 | | - properties.forEach((key, value) -> builder.append("[").append(key).append(" : ").append(value) |
73 | | - .append("]")); |
| 76 | + |
| 77 | + // Explaining variable for document properties map |
| 78 | + Map<String, Object> documentProperties = this.documentProperties; |
| 79 | + |
| 80 | + // Explaining variable for the size of document properties map |
| 81 | + int numProperties = documentProperties.size(); |
| 82 | + |
| 83 | + // Explaining variable for tracking the current property index |
| 84 | + int currentPropertyIndex = 0; |
| 85 | + |
| 86 | + // Iterate over document properties map |
| 87 | + for (Map.Entry<String, Object> entry : documentProperties.entrySet()) { |
| 88 | + String key = entry.getKey(); |
| 89 | + Object value = entry.getValue(); |
| 90 | + |
| 91 | + // Append key-value pair |
| 92 | + builder.append("[").append(key).append(" : ").append(value).append("]"); |
| 93 | + |
| 94 | + // Add comma if not last property |
| 95 | + if (++currentPropertyIndex < numProperties) { |
| 96 | + builder.append(", "); |
| 97 | + } |
| 98 | + } |
| 99 | + |
74 | 100 | builder.append("]"); |
75 | 101 | return builder.toString(); |
76 | 102 | } |
|
0 commit comments