32
32
import org .apiguardian .api .API ;
33
33
import org .apiguardian .api .API .Status ;
34
34
35
+ import com .navercorp .fixturemonkey .annotation .Order ;
35
36
import com .navercorp .fixturemonkey .api .constraint .JavaConstraintGenerator ;
36
37
import com .navercorp .fixturemonkey .api .container .DecomposedContainerValueFactory ;
37
38
import com .navercorp .fixturemonkey .api .context .MonkeyContext ;
62
63
import com .navercorp .fixturemonkey .buildergroup .ArbitraryBuilderCandidate ;
63
64
import com .navercorp .fixturemonkey .buildergroup .ArbitraryBuilderGroup ;
64
65
import com .navercorp .fixturemonkey .customizer .MonkeyManipulatorFactory ;
66
+ import com .navercorp .fixturemonkey .customizer .PriorityMatcherOperator ;
65
67
import com .navercorp .fixturemonkey .expression .ArbitraryExpressionFactory ;
66
68
import com .navercorp .fixturemonkey .expression .MonkeyExpressionFactory ;
67
69
import com .navercorp .fixturemonkey .resolver .ManipulatorOptimizer ;
72
74
@ SuppressWarnings ("unused" )
73
75
@ API (since = "0.4.0" , status = Status .MAINTAINED )
74
76
public final class FixtureMonkeyBuilder {
77
+ private static final int DEFAULT_PRIORITY = Integer .MAX_VALUE ;
78
+
75
79
private final FixtureMonkeyOptionsBuilder fixtureMonkeyOptionsBuilder = FixtureMonkeyOptions .builder ();
76
- private final List <MatcherOperator <Function <FixtureMonkey , ? extends ArbitraryBuilder <?>>>>
77
- registeredArbitraryBuilders = new ArrayList <>();
80
+ private final List <PriorityMatcherOperator <Function <FixtureMonkey , ? extends ArbitraryBuilder <?>>>>
81
+ registeredArbitraryBuildersWithPriority = new ArrayList <>();
78
82
private ManipulatorOptimizer manipulatorOptimizer = new NoneManipulatorOptimizer ();
79
83
private MonkeyExpressionFactory monkeyExpressionFactory = new ArbitraryExpressionFactory ();
80
84
private final MonkeyContextBuilder monkeyContextBuilder = MonkeyContext .builder ();
81
- private final Map <String , MatcherOperator <Function <FixtureMonkey , ? extends ArbitraryBuilder <?>>>>
82
- registeredArbitraryListByRegisteredName = new HashMap <>();
85
+ private final Map <String , PriorityMatcherOperator <Function <FixtureMonkey , ? extends ArbitraryBuilder <?>>>>
86
+ registeredPriorityMatchersByName = new HashMap <>();
83
87
private long seed = System .nanoTime ();
84
88
85
89
// The default plugins are listed below.
@@ -310,11 +314,37 @@ public FixtureMonkeyBuilder addExceptGeneratePackages(String... exceptGeneratePa
310
314
return this ;
311
315
}
312
316
317
+ /**
318
+ * Registers an ArbitraryBuilder with the DEFAULT priority (Integer.MAX_VALUE).
319
+ *
320
+ * @param registeredArbitraryBuilder the MatcherOperator containing the matcher
321
+ * and the ArbitraryBuilder to be registered
322
+ * @return the current instance of FixtureMonkeyBuilder for method chaining
323
+ */
313
324
public FixtureMonkeyBuilder register (
314
325
Class <?> type ,
315
326
Function <FixtureMonkey , ? extends ArbitraryBuilder <?>> registeredArbitraryBuilder
316
327
) {
317
- return this .register (MatcherOperator .assignableTypeMatchOperator (type , registeredArbitraryBuilder ));
328
+ return this .register (type , registeredArbitraryBuilder , DEFAULT_PRIORITY );
329
+ }
330
+
331
+ /**
332
+ * Registers an ArbitraryBuilder with a specified priority.
333
+ *
334
+ * @param registeredArbitraryBuilder the MatcherOperator containing the matcher
335
+ * and the ArbitraryBuilder to be registered
336
+ * @param priority the priority of the ArbitraryBuilder; higher values indicate lower priority
337
+ * @return the current instance of FixtureMonkeyBuilder for method chaining
338
+ * @throws IllegalArgumentException if the priority is less than 0
339
+ *
340
+ * If multiple ArbitraryBuilders have the same priority, one of them will be selected randomly.
341
+ */
342
+ public FixtureMonkeyBuilder register (
343
+ Class <?> type ,
344
+ Function <FixtureMonkey , ? extends ArbitraryBuilder <?>> registeredArbitraryBuilder ,
345
+ int priority
346
+ ) {
347
+ return this .register (MatcherOperator .assignableTypeMatchOperator (type , registeredArbitraryBuilder ), priority );
318
348
}
319
349
320
350
public FixtureMonkeyBuilder registerExactType (
@@ -334,7 +364,18 @@ public FixtureMonkeyBuilder registerAssignableType(
334
364
public FixtureMonkeyBuilder register (
335
365
MatcherOperator <Function <FixtureMonkey , ? extends ArbitraryBuilder <?>>> registeredArbitraryBuilder
336
366
) {
337
- this .registeredArbitraryBuilders .add (registeredArbitraryBuilder );
367
+ return this .register (registeredArbitraryBuilder , DEFAULT_PRIORITY );
368
+ }
369
+
370
+ public FixtureMonkeyBuilder register (
371
+ MatcherOperator <Function <FixtureMonkey , ? extends ArbitraryBuilder <?>>> registeredArbitraryBuilder ,
372
+ int priority
373
+ ) {
374
+ this .registeredArbitraryBuildersWithPriority .add (
375
+ new PriorityMatcherOperator <>(
376
+ registeredArbitraryBuilder .getMatcher (), registeredArbitraryBuilder .getOperator (), priority
377
+ )
378
+ );
338
379
return this ;
339
380
}
340
381
@@ -361,6 +402,11 @@ public FixtureMonkeyBuilder registerGroup(Class<?>... arbitraryBuilderGroups) {
361
402
throw new RuntimeException (ex );
362
403
}
363
404
};
405
+
406
+ if (arbitraryBuilderGroup .isAnnotationPresent (Order .class )) {
407
+ Order order = arbitraryBuilderGroup .getAnnotation (Order .class );
408
+ this .register (actualType , registerArbitraryBuilder , order .value ());
409
+ }
364
410
this .register (actualType , registerArbitraryBuilder );
365
411
} catch (Exception ex ) {
366
412
// ignored
@@ -378,7 +424,8 @@ public FixtureMonkeyBuilder registerGroup(ArbitraryBuilderGroup... arbitraryBuil
378
424
for (ArbitraryBuilderCandidate <?> candidate : candidates ) {
379
425
this .register (
380
426
candidate .getClassType (),
381
- candidate .getArbitraryBuilderRegisterer ()
427
+ candidate .getArbitraryBuilderRegisterer (),
428
+ DEFAULT_PRIORITY
382
429
);
383
430
}
384
431
}
@@ -390,13 +437,26 @@ public FixtureMonkeyBuilder registeredName(
390
437
Class <?> type ,
391
438
Function <FixtureMonkey , ? extends ArbitraryBuilder <?>> arbitraryBuilder
392
439
) {
393
- if (registeredArbitraryListByRegisteredName .containsKey (registeredName )) {
440
+ return this .registeredName (registeredName , type , arbitraryBuilder , DEFAULT_PRIORITY );
441
+ }
442
+
443
+ public FixtureMonkeyBuilder registeredName (
444
+ String registeredName ,
445
+ Class <?> type ,
446
+ Function <FixtureMonkey , ? extends ArbitraryBuilder <?>> arbitraryBuilder ,
447
+ int priority
448
+ ) {
449
+ if (registeredPriorityMatchersByName .containsKey (registeredName )) {
394
450
throw new IllegalArgumentException ("Duplicated ArbitraryBuilder name: " + registeredName );
395
451
}
396
452
MatcherOperator <Function <FixtureMonkey , ? extends ArbitraryBuilder <?>>> matcherOperator =
397
453
MatcherOperator .assignableTypeMatchOperator (type , arbitraryBuilder );
398
454
399
- this .registeredArbitraryListByRegisteredName .put (registeredName , matcherOperator );
455
+ this .registeredPriorityMatchersByName .put (
456
+ registeredName , new PriorityMatcherOperator <>(
457
+ matcherOperator .getMatcher (), matcherOperator .getOperator (), priority
458
+ )
459
+ );
400
460
return this ;
401
461
}
402
462
@@ -570,9 +630,9 @@ public FixtureMonkey build() {
570
630
traverser ,
571
631
manipulatorOptimizer ,
572
632
monkeyContext ,
573
- registeredArbitraryBuilders ,
633
+ registeredArbitraryBuildersWithPriority ,
574
634
monkeyManipulatorFactory ,
575
- registeredArbitraryListByRegisteredName
635
+ registeredPriorityMatchersByName
576
636
);
577
637
}
578
638
}
0 commit comments