@@ -95,10 +95,25 @@ public sealed class OutputCollector<T> private constructor(
95
95
private val parent : DelegateOutputCollector ? = null ,
96
96
transformer : OutputErrorTransformer <Nothing > = NO_TRANSFORMATION ,
97
97
) : OutputCollector<Nothing>(parent, transformer) {
98
- private val reportedErrors = mutableListOf<ValidationError >()
98
+ private lateinit var reportedErrors: MutableList <ValidationError >
99
+
100
+ private fun addError (error : ValidationError ) {
101
+ if (! ::reportedErrors.isInitialized) {
102
+ reportedErrors = ArrayList (1 )
103
+ }
104
+ reportedErrors.add(error)
105
+ }
106
+
107
+ private fun addErrors (errors : MutableList <ValidationError >) {
108
+ if (::reportedErrors.isInitialized) {
109
+ reportedErrors.addAll(errors)
110
+ } else {
111
+ reportedErrors = errors
112
+ }
113
+ }
99
114
100
115
override fun onError (error : ValidationError ) {
101
- transformError(error)?.also (reportedErrors::add)
116
+ transformError(error)?.also { addError(it) }
102
117
}
103
118
104
119
override val output: Nothing
@@ -118,10 +133,10 @@ public sealed class OutputCollector<T> private constructor(
118
133
}
119
134
120
135
override fun reportErrors () {
121
- if (reportedErrors.isEmpty() ) {
136
+ if (! :: reportedErrors.isInitialized ) {
122
137
return
123
138
}
124
- parent?.also { it.reportedErrors.addAll (reportedErrors) }
139
+ parent?.also { it.addErrors (reportedErrors) }
125
140
? : reportedErrors.forEach(errorCollector::onError)
126
141
}
127
142
@@ -175,25 +190,43 @@ public sealed class OutputCollector<T> private constructor(
175
190
private val parent : Basic ? = null ,
176
191
transformer : OutputErrorTransformer <ValidationOutput .Basic > = NO_TRANSFORMATION ,
177
192
) : OutputCollector<ValidationOutput.Basic>(parent, transformer) {
178
- private val errors = mutableListOf<BasicError >()
193
+ private lateinit var errors: MutableSet <BasicError >
194
+
195
+ private fun addError (error : BasicError ) {
196
+ if (! ::errors.isInitialized) {
197
+ errors = linkedSetOf()
198
+ }
199
+ errors.add(error)
200
+ }
201
+
202
+ private fun addErrors (errors : MutableSet <BasicError >) {
203
+ if (::errors.isInitialized) {
204
+ this .errors.addAll(errors)
205
+ } else {
206
+ this .errors = errors
207
+ }
208
+ }
179
209
180
210
override fun onError (error : ValidationError ) {
181
211
val err = transformError(error) ? : return
182
- errors + =
212
+ addError(
183
213
BasicError (
184
214
keywordLocation = err.schemaPath,
185
215
instanceLocation = err.objectPath,
186
216
absoluteKeywordLocation = err.absoluteLocation,
187
217
error = err.message,
188
- )
218
+ ),
219
+ )
189
220
}
190
221
191
222
override val output: ValidationOutput .Basic
192
- get() =
193
- ValidationOutput .Basic (
223
+ get() {
224
+ val errors = if (::errors.isInitialized) errors else emptySet()
225
+ return ValidationOutput .Basic (
194
226
valid = errors.isEmpty(),
195
- errors = errors.toSet() ,
227
+ errors = errors,
196
228
)
229
+ }
197
230
198
231
override fun updateLocation (path : JsonPointer ): OutputCollector <ValidationOutput .Basic > = childCollector()
199
232
@@ -210,7 +243,10 @@ public sealed class OutputCollector<T> private constructor(
210
243
override fun childCollector (): OutputCollector <ValidationOutput .Basic > = Basic (this )
211
244
212
245
override fun reportErrors () {
213
- parent?.errors?.addAll(errors)
246
+ if (! ::errors.isInitialized) {
247
+ return
248
+ }
249
+ parent?.addErrors(errors)
214
250
}
215
251
}
216
252
0 commit comments