@@ -263,12 +263,23 @@ public sealed class OutputCollector<T> private constructor(
263
263
private val collapse : Boolean = true ,
264
264
transformer : OutputErrorTransformer <ValidationOutput .Detailed > = NO_TRANSFORMATION ,
265
265
) : OutputCollector<ValidationOutput.Detailed>(parent, transformer) {
266
- private val errors: MutableList <ValidationOutput .Detailed > = mutableListOf ()
266
+ private lateinit var results: MutableSet <ValidationOutput .Detailed >
267
+
268
+ private fun addResult (result : ValidationOutput .Detailed ) {
269
+ if (result.valid) {
270
+ // do not add valid
271
+ return
272
+ }
273
+ if (! ::results.isInitialized) {
274
+ results = linkedSetOf()
275
+ }
276
+ results.add(result)
277
+ }
267
278
268
279
override val output: ValidationOutput .Detailed
269
280
get() {
270
- val valid = errors.none { ! it.valid }
271
- if (valid) {
281
+ if ( ! ::results.isInitialized) {
282
+ // variable is uninitialized only if all results are valid
272
283
return Detailed (
273
284
valid = true ,
274
285
keywordLocation = keywordLocation,
@@ -277,7 +288,7 @@ public sealed class OutputCollector<T> private constructor(
277
288
errors = emptySet(),
278
289
)
279
290
}
280
- val failed = errors.filterTo(hashSetOf()) { it.error != null || it.errors.isNotEmpty() }
291
+ val failed = results
281
292
return if (failed.size == 1 && collapse) {
282
293
failed.single()
283
294
} else {
@@ -331,12 +342,12 @@ public sealed class OutputCollector<T> private constructor(
331
342
Detailed (location, keywordLocation, parent, absoluteLocation, collapse, transformer = transformer)
332
343
333
344
override fun reportErrors () {
334
- parent?.errors?.add (output)
345
+ parent?.addResult (output)
335
346
}
336
347
337
348
override fun onError (error : ValidationError ) {
338
349
val err = transformError(error) ? : return
339
- errors.add (
350
+ addResult (
340
351
Detailed (
341
352
valid = false ,
342
353
instanceLocation = err.objectPath,
@@ -355,14 +366,20 @@ public sealed class OutputCollector<T> private constructor(
355
366
private val absoluteLocation : AbsoluteLocation ? = null ,
356
367
transformer : OutputErrorTransformer <ValidationOutput .Verbose > = NO_TRANSFORMATION ,
357
368
) : OutputCollector<ValidationOutput.Verbose>(parent, transformer) {
358
- private val errors: MutableList <ValidationOutput .Verbose > = mutableListOf ()
369
+ private val results: MutableList <ValidationOutput .Verbose > = ArrayList (1 )
370
+
371
+ private fun addResult (result : ValidationOutput .Verbose ) {
372
+ // init hashCode to reduce overhead in future
373
+ result.hashCode()
374
+ results.add(result)
375
+ }
359
376
360
377
override val output: ValidationOutput .Verbose
361
378
get() {
362
- if (errors .size == 1 ) {
379
+ if (results .size == 1 ) {
363
380
// when this is a leaf we should return the reported error
364
381
// instead of creating a new node
365
- val childError = errors .single()
382
+ val childError = results .single()
366
383
if (
367
384
childError.errors.isEmpty() &&
368
385
childError.let {
@@ -373,11 +390,11 @@ public sealed class OutputCollector<T> private constructor(
373
390
}
374
391
}
375
392
return Verbose (
376
- valid = errors .none { ! it.valid },
393
+ valid = results .none { ! it.valid },
377
394
keywordLocation = keywordLocation,
378
395
absoluteKeywordLocation = absoluteLocation,
379
396
instanceLocation = location,
380
- errors = errors .toSet(),
397
+ errors = results .toSet(),
381
398
)
382
399
}
383
400
@@ -422,12 +439,12 @@ public sealed class OutputCollector<T> private constructor(
422
439
Verbose (location, keywordLocation, parent, absoluteLocation, transformer)
423
440
424
441
override fun reportErrors () {
425
- parent?.errors?.add (output)
442
+ parent?.addResult (output)
426
443
}
427
444
428
445
override fun onError (error : ValidationError ) {
429
446
val err = transformError(error) ? : return
430
- errors.add (
447
+ addResult (
431
448
Verbose (
432
449
valid = false ,
433
450
instanceLocation = err.objectPath,
0 commit comments